SuspendAds.php 3.08 KB
<?php

namespace App\Service\Requests\Direct;

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

class SuspendAds extends DirectRequest
{
    protected $max_count = 10000;

    protected $timestamp;

    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)
    {
        if (!isset($response['result']['SuspendResults'])) {
            return;
        }

        foreach (DirectResponseHelper::getExternalIdsChunkByResult($response['result']['SuspendResults']) as $external_ids) {
            GoalAdvertisement::whereIn('external_id', $external_ids)
                ->update([
                    'suspended_at' => Carbon::now(),
                    'reserve_suspend_at' => null,
                ]);
        }

        foreach ($response['result']['SuspendResults'] as $key => $suspend_result) {
            if (isset($suspend_result['Id'])) {
                continue;
            }

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

            if (isset($suspend_result['Errors']) && count($suspend_result['Errors'])) {
                $model = GoalAdvertisement::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("SuspendAds, empty Id, token_id {$this->getToken()->getKey()}");
                Log::debug($suspend_result);
                Log::debug($external_id);
            }

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

        }
    }

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

    private function requestPrepare($params)
    {
        $this->setService('Ads');
        $this->setMethod('suspend');

        $this->setParams([
            'SelectionCriteria' => [
                'Ids' => $params['ids'],
            ],
        ]);
    }

}