src/Suez/ReportImportBundle/Service/EventSubscriber/Error.php line 73

Open in your IDE?
  1. <?php
  2. /**
  3. * Copyright (C) SUEZ Smart Solutions - All Rights Reserved
  4. * On’Connect Gateway Management, 2018
  5. * Unauthorized copying of this file, via any medium is strictly prohibited
  6. * Proprietary and confidential
  7. * For the full copyright and license information, please report to the LICENSE CONTRACT
  8. */
  9. /**
  10.  * Created by PhpStorm.
  11.  * User: ivkou
  12.  * Date: 13/06/18
  13.  * Time: 14:32
  14.  */
  15. namespace Suez\ReportImportBundle\Service\EventSubscriber;
  16. use Psr\Log\LoggerInterface;
  17. use Psr\Log\LogLevel;
  18. use Suez\ReportImportBundle\Command\RunCommand;
  19. use Symfony\Component\Console\ConsoleEvents;
  20. use Symfony\Component\Console\Event\ConsoleExceptionEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. /**
  23.  * Class Error
  24.  * @package ReportImportBundle\Service\EventSubscriber
  25.  */
  26. class Error implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var LoggerInterface
  30.      */
  31.     private $logger;
  32.     /**
  33.      * Exception constructor.
  34.      * @param LoggerInterface $logger
  35.      */
  36.     public function __construct(LoggerInterface $logger)
  37.     {
  38.         $this->logger $logger;
  39.     }
  40.     /**
  41.      * Returns an array of event names this subscriber wants to listen to.
  42.      *
  43.      * The array keys are event names and the value can be:
  44.      *
  45.      *  * The method name to call (priority defaults to 0)
  46.      *  * An array composed of the method name to call and the priority
  47.      *  * An array of arrays composed of the method names to call and respective
  48.      *    priorities, or 0 if unset
  49.      *
  50.      * For instance:
  51.      *
  52.      *  * array('eventName' => 'methodName')
  53.      *  * array('eventName' => array('methodName', $priority))
  54.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  55.      *
  56.      * @return array The event names to listen to
  57.      */
  58.     public static function getSubscribedEvents()
  59.     {
  60.         return [
  61.             ConsoleEvents::EXCEPTION => 'onException'
  62.         ];
  63.     }
  64.     /**
  65.      * @param $event
  66.      */
  67.     public function onException(ConsoleExceptionEvent $event)
  68.     {
  69.         if(!$event->getCommand() instanceof RunCommand){
  70.             return;
  71.         }
  72.         $exception $event->getException();
  73.         $level LogLevel::ERROR;
  74.         //TODO Add possibility to modulate level (level on exception ?)
  75.         //TODO Add affected import
  76.         $this->logger->log($level$exception->getMessage());
  77.     }
  78. }