Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
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
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
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<*,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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