<?php

namespace KupShop\AutopilotBundle\EventListener;

use KupShop\AutopilotBundle\Util\AutopilotUtil;
use KupShop\KupShopBundle\Event\CronEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CronListener implements EventSubscriberInterface
{
    /** @var AutopilotUtil */
    protected $autopilotUtil;

    /**
     * @required
     */
    public function setAutopilotUtil(AutopilotUtil $autopilotUtil)
    {
        $this->autopilotUtil = $autopilotUtil;
    }

    public static function getSubscribedEvents()
    {
        return [
            CronEvent::RUN_FREQUENT => [
                ['orderAutoprocess', 201],
            ],
        ];
    }

    public function orderAutoprocess()
    {
        $enable = \Settings::getDefault()['autopilot']['enable'] ?? 'N';
        if ($enable == 'Y') {
            $this->autopilotUtil->autopilot();
        }
    }
}
