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

EventListener/ConsoleExceptionListener.php (1 issue)

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\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
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