LoadUpdatedCampaigns.php 1.55 KB
<?php

namespace App\Console\Commands;

use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Direct\CheckDictionaries;
use App\Service\Direct\GetCampaigns;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Bus;

class LoadUpdatedCampaigns extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'campaigns:loadUpdated';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Загрузка измененных РК';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $campaigns = Campaigns::forUpdated()->get();
        if (!$campaigns)
            return;

        $token = Tokens::where('type', Tokens::MAIN)->first();
        if (!$token){
            throw new \Exception('Не найден токен блин');
        }

        $factory = APIRequest::getInstance(API::YANDEX);
        $factory->setToken($token);

        $ids = [];
        foreach ($campaigns as $campaign){
            $ids[] = $campaign['external_id'];
        }

        if (!empty($ids)){
            $factory->getRequest('campaigns', 'get')->call(['ids' => $ids]);
        }

        return 0;
    }
}