AddNegativeKeywordSharedSets.php 4.51 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 Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;

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

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

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

                if (!isset($add_result['Id'])) {
                    Log::debug("AddNegativeKeywordSharedSet, empty Id");
                    Log::debug($add_result);
                    Log::debug($this->getParams()['NegativeKeywordSharedSets'][$key]);

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

                    continue;
                }

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

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

            }
        } catch (\Exception $e) {
            Log::debug($e);
            throw $e;
        }
    }

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

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

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

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

}