UpdateRetargetinglists.php 4.25 KB
<?php

namespace App\Service\Requests\Direct;

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

class UpdateRetargetinglists extends DirectRequest
{
    protected $max_count = 1000;

    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()['RetargetingLists']);
    }

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

        return $splinter;
    }

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

        foreach (DirectResponseHelper::getExternalIdsChunkByResult($response['result']['UpdateResults']) as $external_ids) {
            GoalRetargetinglist::whereIn('external_id', $external_ids)
                ->update([
                    'updated_need' => null,
                    'reserve_update_at' => null,
                    'updated_at' => Carbon::now(),
                ]);
        }

        foreach ($response['result']['UpdateResults'] as $key => $update_result) {

            if (isset($update_result['Id'])) {
                continue;
            }

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

            if (isset($update_result['Errors']) && count($update_result['Errors'])) {
                $goalRetargetinglist = GoalRetargetinglist::whereExternalId($data['Id'])->first();

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

            GoalRetargetinglist::where('external_id', $data['Id'])
                ->update([
                    'reserve_update_at' => null,
                ]);

        }
    }

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

    private function requestPrepare($params)
    {
        $this->setService('RetargetingLists');
        $this->setMethod('update');

        $variables = Variable::all();

        $lists = [];

        $this->setParams([
            'RetargetingLists' => $params['goalRetargetinglists']->map(function (GoalRetargetinglist $goalRetargetinglist) use ($variables, &$lists) {

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

                return [
                    'Id' => $goalRetargetinglist->external_id,
                    'Name' => StrReplaceByVariables::getInstance($goalRetargetinglist->retargetinglist->name, $list)->get(),
                    'Description' => StrReplaceByVariables::getInstance($goalRetargetinglist->retargetinglist->description, $list)->get(),
                    'Rules' => $goalRetargetinglist->retargetinglist->rules,
                ];

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

}