AddAdExtensions.php 4.21 KB
<?php

namespace App\Service\Requests\Direct;

use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalAdExtension;
use App\Models\Variable;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use App\Service\StrReplaceByVariables;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;

class AddAdExtensions extends DirectRequest
{
    protected $max_count = 1000;

    protected $timestamp;
    /* @var Collection|GoalAdExtension[] */
    protected $goalAdExtensions;

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

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

    public function slice($maxObjects): ?APIRequest
    {
        $splinter = $this->sliceByKey($maxObjects, 'AdExtensions');

        $splinter->putParams([
            'goalAdExtensions' => $this->goalAdExtensions->slice($maxObjects)->values(),
        ]);

        $this->putParams([
            'goalAdExtensions' => $this->goalAdExtensions->slice(0, $maxObjects),
        ]);

        return $splinter;
    }

    public function handle($response)
    {
        if (!isset($response['result']['AddResults'])) {
            return;
        }

        foreach ($response['result']['AddResults'] as $key => $add_result) {

            $goalAdExtension = $this->goalAdExtensions->get($key);

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

                $data = $this->getParams()['AdExtensions'][$key];

                if (isset($add_result['Errors']) && count($add_result['Errors'])) {
                    $goalAdExtension->errors()->create([
                        'token_id' => $this->getToken()->getKey(),
                        'service' => $this->getService(),
                        'method' => $this->getMethod(),
                        'params' => $data,
                        'errors' => $add_result['Errors'],
                    ]);
                } else {
                    Log::debug("AddAdExtension, empty Id, token_id {$this->getToken()->getKey()}");
                    Log::debug($add_result);
                    Log::debug($data);
                }

                GoalAdExtension::where('id', $goalAdExtension->getKey())
                    ->update([
                        'reserve_create_at' => null,
                    ]);

                continue;
            }

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

            GoalAdExtension::where('id', $goalAdExtension->getKey())
                ->update([
                    'external_id' => $external_id,
                    'external_upload_at' => Carbon::now(),
                    'reserve_create_at' => null,
                ]);

        }
    }

    public function failed()
    {
        GoalAdExtension::whereIn('id', $this->goalAdExtensions->pluck('id')->toArray())
            ->update([
                'reserve_create_at' => null,
            ]);
    }

    public function putParams($params)
    {
        $this->goalAdExtensions = $params['goalAdExtensions'];
    }

    private function requestPrepare($params)
    {
        $this->setService('AdExtensions');
        $this->setMethod('add');

        $this->putParams($params);

        $variables = Variable::all();

        $lists = [];

        $this->setParams([
            'AdExtensions' => $this->goalAdExtensions->map(function (GoalAdExtension $goalAdExtension) use ($variables, &$lists) {

                if (!isset($lists[$goalAdExtension->dictionary_campaign_id])) {
                    $list = Variable::getListVariablesByDictionaryCampaign($goalAdExtension->dictionary_campaign_id, $variables);
                    $lists[$goalAdExtension->dictionary_campaign_id] = $list;
                } else {
                    $list = $lists[$goalAdExtension->dictionary_campaign_id];
                }

                return [
                    'Callout' => [
                        'CalloutText' => StrReplaceByVariables::getInstance($goalAdExtension->adExtension->callout_text, $list)->get(),
                    ],
                ];

            })->toArray(),
        ]);
    }

}