AdvertisementsCheckArchive.php 1.51 KB
<?php

namespace App\Console\Commands;

use App\Models\Advertisement;
use App\Models\Tokens;
use App\Service\Requests\Direct\GetAds;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

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

    /**
     * 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()
    {
        $advertisements = Advertisement::forNotArchived()->notNeedArchived()->get();

        if ($advertisements->count()) {

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

            $ids = $advertisements->pluck('external_id')->toArray();

            $request = new GetAds();
            $request->setToken($token)
                ->call([
                    'Ids' => $ids,
                    'States' => [
                        Advertisement::STATE_ARCHIVED,
                    ],
                ]);

        }

        return 0;
    }
}