Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

ConsoleExceptionSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
Duplication introduced by
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 1
    public function __construct(LoggerInterface $logger)
24
    {
25 1
        $this->logger = $logger;
26 1
    }
27
28
    /**
29
     * @return array
0 ignored issues
show
Documentation introduced by
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