<?php

namespace KupShop\MetricsBundle\EventSubscriber;

use KupShop\KupShopBundle\Event\CronEvent;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use Monolog\Logger;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CronListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        if (isAutoDeploy()) {
            return [
                CronEvent::RUN_EXPENSIVE => [
                    ['clearAPC', 3000],
                ],
            ];
        }

        return [];
    }

    public function clearAPC()
    {
        $result = file_get_contents('http://localhost/_prometheus/clear/');

        if ($result != 'OK') {
            /** @var Logger $logger */
            $logger = ServiceContainer::getService('logger');
            $logger->notice('ERROR clearing APCU cache: '.print_r($result, true));
        }
    }
}
