1 | <?php |
||
16 | final class CommandLogger implements Countable, CommandSubscriber |
||
17 | { |
||
18 | /** @var Command[] */ |
||
19 | private $commands = []; |
||
20 | |||
21 | /** @var CommandStartedEvent[] */ |
||
22 | private $startedCommands = []; |
||
23 | |||
24 | /** @var bool */ |
||
25 | private $registered = false; |
||
26 | |||
27 | public function register() : void |
||
28 | { |
||
29 | if ($this->registered) { |
||
30 | return; |
||
31 | } |
||
32 | |||
33 | $this->registered = true; |
||
34 | addSubscriber($this); |
||
35 | } |
||
36 | |||
37 | public function unregister() : void |
||
38 | { |
||
39 | if (! $this->registered) { |
||
40 | return; |
||
41 | } |
||
42 | |||
43 | removeSubscriber($this); |
||
44 | $this->registered = false; |
||
45 | } |
||
46 | |||
47 | public function commandStarted(CommandStartedEvent $event) |
||
48 | { |
||
49 | $this->startedCommands[$event->getRequestId()] = $event; |
||
50 | } |
||
51 | |||
52 | public function commandSucceeded(CommandSucceededEvent $event) |
||
53 | { |
||
54 | $commandStartedEvent = $this->findAndRemoveCommandStartedEvent($event->getRequestId()); |
||
55 | if (! $commandStartedEvent) { |
||
56 | return; |
||
57 | } |
||
58 | |||
59 | $this->logCommand(Command::createForSucceededCommand($commandStartedEvent, $event)); |
||
60 | } |
||
61 | |||
62 | public function commandFailed(CommandFailedEvent $event) |
||
71 | |||
72 | public function clear() : void |
||
73 | { |
||
74 | $this->commands = []; |
||
75 | } |
||
76 | |||
77 | public function count() : int |
||
81 | |||
82 | /** |
||
83 | * @return Command[] |
||
84 | */ |
||
85 | public function getAll() : array |
||
89 | |||
90 | private function findAndRemoveCommandStartedEvent(string $requestId) : ?CommandStartedEvent |
||
97 | |||
98 | private function logCommand(Command $command) : void |
||
102 | } |
||
103 |