Code Duplication    Length = 47-55 lines in 2 locations

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

@@ 14-60 (lines=47) @@
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
     * @param LoggerInterface $logger
22
     */
23
    public function __construct(LoggerInterface $logger)
24
    {
25
        $this->logger = $logger;
26
    }
27
28
    /**
29
     * @param ConsoleExceptionEvent $event
30
     */
31
    public function onConsoleException(ConsoleExceptionEvent $event)
32
    {
33
        // if the newer error event exists, don't bother with the old exception, our subscriber will handle this
34
        if (class_exists(ConsoleErrorEvent::class)){
35
            return;
36
        }
37
38
        $command = $event->getCommand();
39
        $exception = $event->getException();
40
41
        $this->logCommandError($command, $exception);
42
    }
43
44
    /**
45
     * @param $command
46
     * @param $error
47
     */
48
    private function logCommandError($command, $error)
49
    {
50
        $message = sprintf(
51
            '%s: %s (uncaught error) at %s line %s while running console command `%s`',
52
            get_class($error),
53
            $error->getMessage(),
54
            $error->getFile(),
55
            $error->getLine(),
56
            $command->getName()
57
        );
58
        $this->logger->error($message, ['error' => $error]);
59
    }
60
}

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->error($message, ['error' => $error]);
66
    }
67
}
68