Passed
Push — master ( f7ef7a...65c26d )
by Dmitriy
02:46
created

CommandCollector::getSummary()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 35
rs 9.2408
cc 5
nc 9
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Console;
6
7
use RuntimeException;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Event\ConsoleCommandEvent;
10
use Symfony\Component\Console\Event\ConsoleErrorEvent;
11
use Symfony\Component\Console\Event\ConsoleEvent;
12
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput;
16
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
17
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
18
19
use function is_object;
20
21
final class CommandCollector implements SummaryCollectorInterface
22
{
23
    use CollectorTrait;
24
25
    /**
26
     * Let -1 mean that it was not set during the process.
27
     */
28
    private const UNDEFINED_EXIT_CODE = -1;
29
    private array $commands = [];
30
31
    public function getCollected(): array
32
    {
33
        return $this->commands;
34
    }
35
36
    public function collect(ConsoleEvent|ConsoleErrorEvent|ConsoleTerminateEvent $event): void
37
    {
38
        if (!is_object($event) || !$this->isActive()) {
39
            return;
40
        }
41
42
        $command = $event->getCommand();
43
44
        if ($event instanceof ConsoleErrorEvent) {
45
            $this->commands[$event::class] = [
46
                'name' => $event->getInput()->getFirstArgument() ?? '',
47
                'command' => $command,
48
                'input' => $this->castInputToString($event->getInput()),
49
                'output' => $this->fetchOutput($event->getOutput()),
50
                'error' => $event->getError()->getMessage(),
51
                'exitCode' => $event->getExitCode(),
52
            ];
53
54
            return;
55
        }
56
57
        if ($event instanceof ConsoleTerminateEvent) {
58
            $this->commands[$event::class] = [
59
                'name' => $command->getName(),
60
                'command' => $command,
61
                'input' => $this->castInputToString($event->getInput()),
62
                'output' => $this->fetchOutput($event->getOutput()),
63
                'exitCode' => $event->getExitCode(),
64
            ];
65
            return;
66
        }
67
68
        if ($event instanceof ConsoleEvent) {
0 ignored issues
show
introduced by
$event is always a sub-type of Symfony\Component\Console\Event\ConsoleEvent.
Loading history...
69
            $this->commands[$event::class] = [
70
                'name' => $command->getName(),
71
                'command' => $command,
72
                'input' => $this->castInputToString($event->getInput()),
73
                'output' => $this->fetchOutput($event->getOutput()),
74
                'arguments' => $command->getDefinition()->getArguments(),
75
                'options' => $command->getDefinition()->getOptions(),
76
            ];
77
        }
78
    }
79
80
    public function getSummary(): array
81
    {
82
        $eventTypes = [
83
            ConsoleErrorEvent::class,
84
            ConsoleTerminateEvent::class,
85
            ConsoleCommandEvent::class,
86
        ];
87
88
        $commandEvent = null;
89
        foreach ($eventTypes as $eventType) {
90
            if (!array_key_exists($eventType, $this->commands)) {
91
                continue;
92
            }
93
94
            $commandEvent = $this->commands[$eventType];
95
            break;
96
        }
97
98
        if ($commandEvent === null) {
99
            $types = array_keys($this->commands);
100
            throw new RuntimeException(
101
                sprintf(
102
                    'Unsupported event type encountered among "%s". Supported only "%s"',
103
                    implode('", "', $types),
104
                    implode('", "', $eventTypes),
105
                )
106
            );
107
        }
108
109
        return [
110
            'command' => [
111
                'name' => $commandEvent['name'],
112
                'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null,
113
                'input' => $commandEvent['input'],
114
                'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE,
115
            ],
116
        ];
117
    }
118
119
    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...
120
    {
121
        $this->commands = [];
122
    }
123
124
    private function fetchOutput(OutputInterface $output): ?string
125
    {
126
        return $output instanceof ConsoleBufferedOutput ? $output->fetch() : null;
127
    }
128
129
    private function castInputToString(InputInterface $input): ?string
130
    {
131
        return method_exists($input, '__toString') ? $input->__toString() : null;
132
    }
133
}
134