Passed
Push — master ( 33d5ac...775805 )
by Dmitriy
03:03
created

src/Collector/CommandCollector.php (1 issue)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
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
14
use function is_object;
15
16
final class CommandCollector implements CollectorInterface, IndexCollectorInterface
17
{
18
    use CollectorTrait;
19
20
    /**
21
     * Let -1 mean that it was not set during the process.
22
     */
23 2
    private const UNDEFINED_EXIT_CODE = -1;
24
    private array $commands = [];
25 2
26
    public function getCollected(): array
27
    {
28 2
        return $this->commands;
29
    }
30 2
31 1
    public function collect(ConsoleEvent|ConsoleErrorEvent|ConsoleTerminateEvent $event): void
32
    {
33
        if (!is_object($event) || !$this->isActive()) {
34 1
            return;
35 1
        }
36 1
37 1
        $command = $event->getCommand();
38 1
39 1
        if ($event instanceof ConsoleErrorEvent) {
40 1
            $this->commands[$event::class] = [
41 1
                'name' => $event->getInput()->getFirstArgument() ?? '',
42
                'command' => $command,
43
                'input' => $event->getInput()->__toString(),
44 1
                'output' => $event->getOutput()->fetch(),
45
                'error' => $event->getError()->getMessage(),
46
                'exitCode' => $event->getExitCode(),
47 1
            ];
48 1
49 1
            return;
50 1
        }
51 1
52 1
        if ($event instanceof ConsoleTerminateEvent) {
53 1
            $this->commands[$event::class] = [
54
                'name' => $command->getName(),
55 1
                'command' => $command,
56
                'input' => $event->getInput()->__toString(),
57
                'output' => $event->getOutput()->fetch(),
58 1
                'exitCode' => $event->getExitCode(),
59 1
            ];
60 1
            return;
61 1
        }
62 1
63 1
        if ($event instanceof ConsoleEvent) {
64
            $this->commands[$event::class] = [
65
                'name' => $command->getName(),
66
                'command' => $command,
67
                'input' => $event->getInput()->__toString(),
68 1
                'output' => $event->getOutput()->fetch(),
69
                'arguments' => $command->getDefinition()->getArguments(),
70 1
                'options' => $command->getDefinition()->getOptions(),
71
            ];
72
        }
73
    }
74
75
    public function getIndexData(): array
76 1
    {
77 1
        $eventTypes = [
78 1
            ConsoleErrorEvent::class,
79
            ConsoleTerminateEvent::class,
80
            ConsoleCommandEvent::class,
81
        ];
82 1
83 1
        $commandEvent = null;
84
        foreach ($eventTypes as $eventType) {
85
            if (!array_key_exists($eventType, $this->commands)) {
86 1
                continue;
87
            }
88
89
            $commandEvent = $this->commands[$eventType];
90
            break;
91
        }
92
93
        if ($commandEvent === null) {
94
            $types = array_keys($this->commands);
95
            throw new RuntimeException(
96
                sprintf(
97
                    'Unsupported event type encountered among "%s". Supported only "%s"',
98
                    implode('", "', $types),
99 1
                    implode('", "', $eventTypes),
100 1
                )
101 1
            );
102
        }
103
104
        return [
105
            'command' => [
106 1
                'name' => $commandEvent['name'],
107
                'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null,
108 1
                'input' => $commandEvent['input'],
109
                'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE,
110
            ],
111
        ];
112
    }
113
114
    private function reset(): void
0 ignored issues
show
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...
115
    {
116
        $this->commands = [];
117
    }
118
}
119