Passed
Push — master ( bace86...bb4cad )
by Dmitriy
05:30 queued 02:57
created

CommandCollector::getSupportedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Console;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Event\ConsoleErrorEvent;
9
use Symfony\Component\Console\Event\ConsoleEvent;
10
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput;
14
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
15
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
16
17
final class CommandCollector implements SummaryCollectorInterface
18
{
19
    use CollectorTrait;
20
21
    /**
22
     * Let -1 mean that it was not set during the process.
23
     */
24
    private const UNDEFINED_EXIT_CODE = -1;
25
    private array $commands = [];
26
27
    public function getCollected(): array
28
    {
29
        return $this->commands;
30
    }
31
32
    public function collect(ConsoleEvent|ConsoleErrorEvent|ConsoleTerminateEvent $event): void
33
    {
34
        if (!$this->isActive()) {
35
            return;
36
        }
37
38
        $command = $event->getCommand();
39
40
        if ($event instanceof ConsoleErrorEvent) {
41
            $this->commands[$event::class] = [
42
                'name' => $event->getInput()->getFirstArgument() ?? '',
43
                'command' => $command,
44
                'input' => $this->castInputToString($event->getInput()),
45
                'output' => $this->fetchOutput($event->getOutput()),
46
                'error' => $event->getError()->getMessage(),
47
                'exitCode' => $event->getExitCode(),
48
            ];
49
50
            return;
51
        }
52
53
        if ($event instanceof ConsoleTerminateEvent) {
54
            $this->commands[$event::class] = [
55
                'name' => $command->getName(),
56
                'command' => $command,
57
                'input' => $this->castInputToString($event->getInput()),
58
                'output' => $this->fetchOutput($event->getOutput()),
59
                'exitCode' => $event->getExitCode(),
60
            ];
61
            return;
62
        }
63
64
        /** @noinspection PhpConditionAlreadyCheckedInspection */
65
        if ($event instanceof ConsoleEvent) {
0 ignored issues
show
introduced by
$event is always a sub-type of Symfony\Component\Console\Event\ConsoleEvent.
Loading history...
66
            $this->commands[$event::class] = [
67
                'name' => $command->getName(),
68
                'command' => $command,
69
                'input' => $this->castInputToString($event->getInput()),
70
                'output' => $this->fetchOutput($event->getOutput()),
71
                'arguments' => $command->getDefinition()->getArguments(),
72
                'options' => $command->getDefinition()->getOptions(),
73
            ];
74
        }
75
    }
76
77
    public function getSummary(): array
78
    {
79
        if (empty($this->commands)) {
80
            return [];
81
        }
82
83
        $eventTypes = $this->getSupportedEvents();
84
85
        $commandEvent = null;
86
        foreach ($eventTypes as $eventType) {
87
            if (!array_key_exists($eventType, $this->commands)) {
88
                continue;
89
            }
90
91
            $commandEvent = $this->commands[$eventType];
92
            break;
93
        }
94
95
        if ($commandEvent === null) {
96
            return [];
97
        }
98
99
        return [
100
            'command' => [
101
                'name' => $commandEvent['name'],
102
                'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null,
103
                'input' => $commandEvent['input'],
104
                'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE,
105
            ],
106
        ];
107
    }
108
109
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
110
    {
111
        $this->commands = [];
112
    }
113
114
    private function fetchOutput(OutputInterface $output): ?string
115
    {
116
        return $output instanceof ConsoleBufferedOutput ? $output->fetch() : null;
117
    }
118
119
    private function castInputToString(InputInterface $input): ?string
120
    {
121
        return method_exists($input, '__toString') ? $input->__toString() : null;
122
    }
123
124
    private function getSupportedEvents(): array
125
    {
126
        return [
127
            ConsoleErrorEvent::class,
128
            ConsoleTerminateEvent::class,
129
            ConsoleEvent::class,
130
        ];
131
    }
132
}
133