Completed
Push — master ( 1db3cd...632e40 )
by Jeroen
24:52 queued 11:31
created

EventListener/ConsoleExceptionSubscriber.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\EventListener;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\Console\ConsoleEvents;
7
use Symfony\Component\Console\Event\ConsoleErrorEvent;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
/**
11
 * Class ConsoleExceptionSubscriber.
12
 */
13 View Code Duplication
final class ConsoleExceptionSubscriber implements EventSubscriberInterface
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /** @var LoggerInterface */
16
    private $logger;
17
18
    /**
19
     * ConsoleExceptionListener constructor.
20
     *
21
     * @param LoggerInterface $logger
22
     */
23
    public function __construct(LoggerInterface $logger)
24
    {
25
        $this->logger = $logger;
26
    }
27
28
    /**
29
     * @return array
30
     */
31
    public static function getSubscribedEvents()
32
    {
33
        return [
34
            ConsoleEvents::ERROR => 'onConsoleError',
35
        ];
36
    }
37
38
    /**
39
     * @param ConsoleErrorEvent $event
40
     */
41
    public function onConsoleError(ConsoleErrorEvent $event)
42
    {
43
        $command = $event->getCommand();
44
        $error = $event->getError();
45
46
        if (null !== $command) {
47
            $this->logCommandError($command, $error);
48
        }
49
    }
50
51
    /**
52
     * @param $command
53
     * @param $error
54
     */
55
    private function logCommandError($command, $error)
56
    {
57
        $message = sprintf(
58
            '%s: %s (uncaught error) at %s line %s while running console command `%s`',
59
            get_class($error),
60
            $error->getMessage(),
61
            $error->getFile(),
62
            $error->getLine(),
63
            $command->getName()
64
        );
65
        $this->logger->error($message, ['error' => $error]);
66
    }
67
}
68