SuspendCampaigns.php 3 KB
<?php

namespace App\Service\Requests\Direct;

use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\DictionaryCampaign;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class SuspendCampaigns extends DirectRequest
{
    protected $max_count = 1000;

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

    public function getObjectsCount()
    {
        return count($this->getParams()['SelectionCriteria']['Ids']);
    }

    public function slice($maxObjects): ?APIRequest
    {
        return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);
    }

    public function handle($response)
    {
        foreach ($response['result']['SuspendResults'] as $key => $suspend_result) {

            $external_id = $this->getParams()['SelectionCriteria']['Ids'][$key];

            if (!isset($suspend_result['Id'])) {

                if (isset($suspend_result['Errors']) && count($suspend_result['Errors'])) {
                    $model = DictionaryCampaign::whereExternalId($external_id)->first();

                    if ($model) {
                        $model->errors()->create([
                            'token_id' => $this->getToken()->getKey(),
                            'service' => $this->getService(),
                            'method' => $this->getMethod(),
                            'params' => $external_id,
                            'errors' => $suspend_result['Errors'],
                        ]);
                    }
                } else {
                    Log::debug("SuspendCampaigns, empty Id, token_id {$this->getToken()->getKey()}");
                    Log::debug($suspend_result);
                    Log::debug($external_id);
                }

                DictionaryCampaign::where('external_id', $external_id)
                    ->update([
                        'reserve_suspend_at' => null,
                    ]);

                continue;
            }

            $external_id = (string)$suspend_result['Id'];

            DictionaryCampaign::where('external_id', $external_id)
                ->update([
                    'disabled_at' => Carbon::now(),
                    'reserve_suspend_at' => null,
                ]);
        }
    }

    public function failed()
    {
        foreach (array_chunk(array_column($this->getParams()['SelectionCriteria']['Ids'], 'Id'), 100) as $items) {
            DictionaryCampaign::whereIn('external_id', $items)
                ->update([
                    'reserve_suspend_at' => null,
                ]);
        }
    }

    private function requestPrepare($filter)
    {
        $this->setService('Campaigns');
        $this->setMethod('suspend');
        $params = [
            'SelectionCriteria' => [
                'Ids' => $filter['ids'],
            ],
        ];
        $this->setParams($params);
    }

}