CampaignsLoadUpdated.php 1.86 KB
<?php

namespace App\Console\Commands;

use App\Models\Campaigns;
use App\Models\Tokens;
use App\Service\Requests\Direct\GetCampaigns;
use Illuminate\Console\Command;

class CampaignsLoadUpdated 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::forEnabled()->forUpdatedSelf()->get();

        if ($campaigns->count()) {

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

            $request = new GetCampaigns();
            $request->setToken($token)
                ->call([
                    'ids' => $campaigns->pluck('external_id')->toArray(),
                ]);

        }

        $tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf')
            ->with('dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf')
            ->where('type', '!=', Tokens::MAIN)
            ->get();

        foreach ($tokens as $token) {
            $request = new GetCampaigns();
            $request->setToken($token)
                ->call([
                    'ids' => $token->dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf
                        ->pluck('external_id')->toArray(),
                ]);
        }

        return 0;
    }
}