CampaignsLoadGroups.php 1.41 KB
<?php

namespace App\Console\Commands;

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

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

    /**
     * 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
     * @throws \Exception
     */
    public function handle()
    {
        $campaigns = Campaigns::forManaged()->forEnabled()->forGroupsLoadable()->get();

        if (!$campaigns->count()) {
            return 0;
        }

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

        foreach ($campaigns->pluck('external_id')->chunk(10)->toArray() as $campaign_ids) {
            $factory = new GetAdGroups();
            $factory->setToken($token)
                ->call([
                    'CampaignIds' => $campaign_ids,
                ]);
        }

        return 0;
    }
}