CheckCampaignsChange.php 1.36 KB
<?php
namespace App\Service\Requests\Direct;

use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use App\Service\Requests\DirectRequest;

class CheckCampaignsChange extends DirectRequest {
    CONST SELF = 'SELF';
    CONST CHILDREN = 'CHILDREN';
    CONST STAT = 'STAT';

    protected $next;

    function call($params = null){
        $this->requestPrepare($params);
        $process = new ProcessCallLimitedAPI($this);
        dispatch($process)->onQueue('limits');
    }

    function handle($response){
        foreach ($response['result']['Campaigns'] as $campaign){
            if ($campaign['ChangesIn']==self::SELF || $campaign['ChangesIn']==self::CHILDREN){
                Campaigns::firstOrCreate([
                    'external_id' => $campaign['CampaignId']
                ],
                [
                    'updated' => $campaign['ChangesIn'],
                    'token'   =>  $this->getToken()->id
                ]);
            }
        }
        $this->getToken()->timestamp = $response['result']['Timestamp'];
        $this->getToken()->save();
    }

    private function requestPrepare($params){
        $this->setService('changes');
        $this->setMethod('checkDictionaries');
        $this->setParams([
            "Timestamp" =>  $this->getToken()->timestamp
        ]);
    }
}