Completed
Push — l10n_master ( ff9163...a02a58 )
by Ruud
22:18 queued 07:25
created

ConsoleExceptionSubscriber   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 52
loc 52
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() 7 7 1
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 Symfony\Component\Console\ConsoleEvents;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\Console\Event\ConsoleErrorEvent;
8
use Psr\Log\LoggerInterface;
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
     * @param LoggerInterface $logger
21
     */
22
    public function __construct(LoggerInterface $logger)
23
    {
24
        $this->logger = $logger;
25
    }
26
27
    /**
28
     * @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...
29
     */
30
    public static function getSubscribedEvents()
31
    {
32
        return [
33
            ConsoleEvents::ERROR => 'onConsoleError'
34
        ];
35
    }
36
37
    /**
38
     * @param ConsoleErrorEvent $event
39
     */
40
    public function onConsoleError(ConsoleErrorEvent $event)
41
    {
42
        $command = $event->getCommand();
43
        $error = $event->getError();
44
45
        $this->logCommandError($command, $error);
46
    }
47
48
    /**
49
     * @param $command
50
     * @param $error
51
     */
52
    private function logCommandError($command, $error)
53
    {
54
        $message = sprintf(
55
            '%s: %s (uncaught error) at %s line %s while running console command `%s`',
56
            get_class($error),
57
            $error->getMessage(),
58
            $error->getFile(),
59
            $error->getLine(),
60
            $command->getName()
61
        );
62
        $this->logger->error($message, ['error' => $error]);
63
    }
64
}