Code Duplication    Length = 48-55 lines in 2 locations

src/Kunstmaan/AdminBundle/EventListener/ConsoleExceptionListener.php 1 location

@@ 14-61 (lines=48) @@
11
 *
12
 * @deprecated in KunstmaanAdminBundle 5.1 and will be removed in KunstmaanNodeBundle 6.0.
13
 */
14
class ConsoleExceptionListener
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

src/Kunstmaan/AdminBundle/EventListener/ConsoleExceptionSubscriber.php 1 location

@@ 13-67 (lines=55) @@
10
/**
11
 * Class ConsoleExceptionSubscriber.
12
 */
13
final class ConsoleExceptionSubscriber implements EventSubscriberInterface
14
{
15
    /** @var LoggerInterface */
16
    private $logger;
17
18
    /**
19
     * ConsoleExceptionListener constructor.
20
     *
21
     * @param LoggerInterface $logger
22
     */
23
    public function __construct(LoggerInterface $logger)
24
    {
25
        $this->logger = $logger;
26
    }
27
28
    /**
29
     * @return array
30
     */
31
    public static function getSubscribedEvents()
32
    {
33
        return [
34
            ConsoleEvents::ERROR => 'onConsoleError',
35
        ];
36
    }
37
38
    /**
39
     * @param ConsoleErrorEvent $event
40
     */
41
    public function onConsoleError(ConsoleErrorEvent $event)
42
    {
43
        $command = $event->getCommand();
44
        $error = $event->getError();
45
46
        if (null !== $command) {
47
            $this->logCommandError($command, $error);
48
        }
49
    }
50
51
    /**
52
     * @param $command
53
     * @param $error
54
     */
55
    private function logCommandError($command, $error)
56
    {
57
        $message = sprintf(
58
            '%s: %s (uncaught error) at %s line %s while running console command `%s`',
59
            get_class($error),
60
            $error->getMessage(),
61
            $error->getFile(),
62
            $error->getLine(),
63
            $command->getName()
64
        );
65
        $this->logger->critical($message, ['error' => $error]);
66
    }
67
}
68