Passed
Push — master ( 52b741...899a70 )
by Alexander
74:33
created

ErrorListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 1
b 0
f 0
dl 0
loc 32
ccs 0
cts 11
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onError() 0 20 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Console;
6
7
use Psr\Log\LoggerInterface;
8
use Symfony\Component\Console\Event\ConsoleErrorEvent;
9
10
final class ErrorListener
11
{
12
    private LoggerInterface $logger;
13
14
    public function __construct(LoggerInterface $logger)
15
    {
16
        $this->logger = $logger;
17
    }
18
19
    /**
20
     * @psalm-suppress PossiblyNullArgument
21
     */
22
    public function onError(ConsoleErrorEvent $event): void
23
    {
24
        $exception = $event->getError();
25
        $command = $event->getCommand();
26
27
        if ($command !== null && $command->getName() !== null) {
28
            $commandName = $command->getName();
29
        } else {
30
            $commandName = 'unknown';
31
        }
32
33
        $message = sprintf(
34
            '%s: %s in %s:%s while running console command `%s`',
35
            get_class($exception),
36
            $exception->getMessage(),
37
            $exception->getFile(),
38
            $exception->getLine(),
39
            $commandName
40
        );
41
        $this->logger->error($message, ['exception' => $exception]);
42
    }
43
}
44