<?php

namespace KupShop\MailerLiteBundle\EventListener;

use KupShop\KupShopBundle\Event\CronEvent;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\MailerLiteBundle\MailerLite;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CronListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            CronEvent::RUN_EXPENSIVE => [
                ['syncMailerLite', 200],
            ],
        ];
    }

    /**
     * @var CronEvent
     */
    public function syncMailerLite(CronEvent $cronEvent)
    {
        $mailerlite = ServiceContainer::getService(MailerLite::class);
        if (empty($mailerlite->error)) {
            $now = time();
            $filter_date = date('Y-m-d', $now - 7 * 24 * 60 * 60); // -7 days
            $mailerlite->Synchronize($filter_date);
        }
    }
}
