Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

ConsoleExceptionListener::onConsoleException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 7
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Kunstmaan\AdminBundle\EventListener;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\Console\Event\ConsoleErrorEvent;
7
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
8
9
/**
10
 * Class ConsoleExceptionListener.
11
 *
12
 * @deprecated in KunstmaanAdminBundle 5.1 and will be removed in KunstmaanNodeBundle 6.0.
13
 */
14 View Code Duplication
class ConsoleExceptionListener
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...
15
{
16
    /** @var LoggerInterface */
17
    private $logger;
18
19
    /**
20
     * ConsoleExceptionListener constructor.
21
     *
22
     * @param LoggerInterface $logger
23
     */
24
    public function __construct(LoggerInterface $logger)
25
    {
26
        $this->logger = $logger;
27
    }
28
29
    /**
30
     * @param ConsoleExceptionEvent $event
31
     */
32
    public function onConsoleException(ConsoleExceptionEvent $event)
33
    {
34
        // if the newer error event exists, don't bother with the old exception, our subscriber will handle this
35
        if (class_exists(ConsoleErrorEvent::class)) {
36
            return;
37
        }
38
39
        $command = $event->getCommand();
40
        $exception = $event->getException();
41
42
        $this->logCommandError($command, $exception);
43
    }
44
45
    /**
46
     * @param $command
47
     * @param $error
48
     */
49
    private function logCommandError($command, $error)
50
    {
51
        $message = sprintf(
52
            '%s: %s (uncaught error) at %s line %s while running console command `%s`',
53
            \get_class($error),
54
            $error->getMessage(),
55
            $error->getFile(),
56
            $error->getLine(),
57
            $command->getName()
58
        );
59
        $this->logger->critical($message, ['error' => $error]);
60
    }
61
}
62