<?php
/**
* Copyright (C) SUEZ Smart Solutions - All Rights Reserved
* On’Connect Gateway Management, 2018
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* For the full copyright and license information, please report to the LICENSE CONTRACT
*/
/**
* Created by PhpStorm.
* User: ivkou
* Date: 13/06/18
* Time: 14:32
*/
namespace Suez\ReportImportBundle\Service\EventSubscriber;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Suez\ReportImportBundle\Command\RunCommand;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class Error
* @package ReportImportBundle\Service\EventSubscriber
*/
class Error implements EventSubscriberInterface
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* Exception constructor.
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
ConsoleEvents::EXCEPTION => 'onException'
];
}
/**
* @param $event
*/
public function onException(ConsoleExceptionEvent $event)
{
if(!$event->getCommand() instanceof RunCommand){
return;
}
$exception = $event->getException();
$level = LogLevel::ERROR;
//TODO Add possibility to modulate level (level on exception ?)
//TODO Add affected import
$this->logger->log($level, $exception->getMessage());
}
}