<?php

namespace External\SprintBundle\EventListener;

use External\SprintBundle\Synchronizers\ProductSynchronizer;
use External\SprintBundle\Util\HeliosUtil;
use KupShop\KupShopBundle\Event\CronEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CronListener implements EventSubscriberInterface
{
    /** @var HeliosUtil */
    private $heliosUtil;

    /** @var ProductSynchronizer */
    private $productSynchronizer;

    public static function getSubscribedEvents()
    {
        return [
            CronEvent::RUN_FREQUENT => [
                ['handleFrequentCron', 200],
            ],
            CronEvent::RUN_NORMAL => [
                ['handleNormalCron', 200],
            ],
            CronEvent::RUN_EXPENSIVE => [
                ['handleExpensiveCron', 200],
            ],
        ];
    }

    /**
     * @required
     */
    public function setHeliosUtil(HeliosUtil $heliosUtil): void
    {
        $this->heliosUtil = $heliosUtil;
    }

    /**
     * @required
     */
    public function setProductSynchronizer(ProductSynchronizer $productSynchronizer): void
    {
        $this->productSynchronizer = $productSynchronizer;
    }

    public function handleFrequentCron(): void
    {
        $this->heliosUtil->synchronize();
    }

    public function handleExpensiveCron(): void
    {
        $this->heliosUtil->synchronize(null, true);
    }

    public function handleNormalCron(): void
    {
        $this->heliosUtil->synchronizeNormal();
    }
}
