Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
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 1
    public function __construct(LoggerInterface $logger)
24
    {
25 1
        $this->logger = $logger;
26 1
    }
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 3
    public static function getSubscribedEvents()
32
    {
33
        return [
34 3
            ConsoleEvents::ERROR => 'onConsoleError',
35
        ];
36
    }
37
38
    /**
39
     * @param ConsoleErrorEvent $event
40
     */
41 1
    public function onConsoleError(ConsoleErrorEvent $event)
42
    {
43 1
        $command = $event->getCommand();
44 1
        $error = $event->getError();
45
46 1
        if (null !== $command) {
47 1
            $this->logCommandError($command, $error);
48
        }
49 1
    }
50
51
    /**
52
     * @param $command
53
     * @param $error
54
     */
55 1
    private function logCommandError($command, $error)
56
    {
57 1
        $message = sprintf(
58 1
            '%s: %s (uncaught error) at %s line %s while running console command `%s`',
59 1
            \get_class($error),
60 1
            $error->getMessage(),
61 1
            $error->getFile(),
62 1
            $error->getLine(),
63 1
            $command->getName()
64
        );
65 1
        $this->logger->critical($message, ['error' => $error]);
66 1
    }
67
}
68