Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

EventListener/ConsoleExceptionSubscriber.php (2 issues)

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