UpdateRetargetinglists.php 3.37 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\Requests\DirectRequest;
use App\Service\StrReplaceByVariables;
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 ($response['result']['UpdateResults'] as $key => $update_result) {

            if (!isset($update_result['Id'])) {
                Log::debug("AddRetargetinglist, empty Id, token_id {$this->getToken()->getKey()}");
                Log::debug($update_result);
                Log::debug($this->getParams()['RetargetingLists'][$key]);

                GoalRetargetinglist::where('external_id', $this->getParams()['RetargetingLists'][$key]['Id'])
                    ->update([
                        'reserve_update_at' => null,
                    ]);

                continue;
            }

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

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

        }
    }

    public function failed()
    {
        GoalRetargetinglist::whereIn('external_id', array_column($this->getParams()['RetargetingLists'], 'Id'))
            ->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(),
        ]);
    }

}