UpdateNegativeKeywordSharedSets.php 4.9 KB
<?php

namespace App\Service\Requests\Direct;

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

class UpdateNegativeKeywordSharedSets extends DirectRequest
{
    protected $max_count = 30;

    protected $timestamp;
    /* @var Collection|NegativeKeywordSharedSet[] */
    protected $goalNegativeKeywordSharedSets;

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

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

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

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

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

        return $splinter;
    }

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

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

            $goalNegativeKeywordSharedSet = $this->goalNegativeKeywordSharedSets->get($key);

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

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

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

                GoalNegativeKeywordSharedSet::where('id', $goalNegativeKeywordSharedSet->getKey())
                    ->update([
                        'reserve_update_at' => null,
                    ]);

                continue;
            }

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

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

        }
    }

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

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

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

        $this->putParams($params);

        $variables = Variable::all();

        $lists = [];

        $this->setParams([
            'NegativeKeywordSharedSets' => $this->goalNegativeKeywordSharedSets->map(function (GoalNegativeKeywordSharedSet $goalNegativeKeywordSharedSet) use ($variables, &$lists) {

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

                return [
                    'Id' => $goalNegativeKeywordSharedSet->external_id,
                    'Name' => StrReplaceByVariables::getInstance($goalNegativeKeywordSharedSet->negativeKeywordSharedSet->name, $list)->get(),
                    'NegativeKeywords' => array_map(function ($negative_keyword) use ($list) {
                        return StrReplaceByVariables::getInstance($negative_keyword, $list)->get();
                    }, $goalNegativeKeywordSharedSet->negativeKeywordSharedSet->negative_keywords),
                ];

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

}