Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

ConsoleExceptionSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 55
loc 55
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getSubscribedEvents() 6 6 1
A onConsoleError() 9 9 2
A logCommandError() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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