<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Actions;

use \ModulesGarden\HostingQuotaNotifications\App\Libs\Notifications\Manage as NotificationsManage;


/**
 * Description of SaveActions
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class SaveActions extends Base
{

    public function run()
    {
        if ($this->data['emailSend'] || $this->data['alreadyOpen'])
        {
            $this->insertData();
            return;
        }
        $this->updateData();
    }

    public function insertData()
    {
        NotificationsManage::insert([
            'groupid'         => $this->data['groupId'],
            'hostingid'       => $this->data['hostingId'],
            'ticketid'        => $this->data['openedTicketId'],
            'configurationid' => $this->data['id'],
            'kind'            => $this->data['kind'],
            'type'            => $this->data['type'],
            'over'            => $this->data['value'],
            'overUnit'        => $this->data['unit'],
            'date'            => date('Y-m-d H:i:s'),
        ]);
    }

    public function updateData()
    {
        $update = [
            'ticketid' => $this->data['openedTicketId'],
        ];
        NotificationsManage::update($this->data['hostingId'], $this->data['kind'], $this->data['type'], $this->data['groupId'], $update);
    }
}
