Completed
Push — master ( a8fe50...bce26f )
by Maciej
13s
created

CommandLogger::commandStarted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\APM;
6
7
use Countable;
8
use MongoDB\Driver\Monitoring\CommandFailedEvent;
9
use MongoDB\Driver\Monitoring\CommandStartedEvent;
10
use MongoDB\Driver\Monitoring\CommandSubscriber;
11
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
12
use function count;
13
use function MongoDB\Driver\Monitoring\addSubscriber;
14
use function MongoDB\Driver\Monitoring\removeSubscriber;
15
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 28
    public function register(): void
28
    {
29 28
        if ($this->registered) {
30
            return;
31
        }
32
33 28
        $this->registered = true;
34 28
        addSubscriber($this);
35 28
    }
36
37 28
    public function unregister(): void
38
    {
39 28
        if (! $this->registered) {
40
            return;
41
        }
42
43 28
        removeSubscriber($this);
44 28
        $this->registered = false;
45 28
    }
46
47 28
    public function commandStarted(CommandStartedEvent $event)
48
    {
49 28
        $this->startedCommands[$event->getRequestId()] = $event;
50 28
    }
51
52 28
    public function commandSucceeded(CommandSucceededEvent $event)
53
    {
54 28
        $commandStartedEvent = $this->findAndRemoveCommandStartedEvent($event->getRequestId());
55 28
        if (! $commandStartedEvent) {
56
            return;
57
        }
58
59 28
        $this->logCommand(Command::createForSucceededCommand($commandStartedEvent, $event));
60 28
    }
61
62
    public function commandFailed(CommandFailedEvent $event)
63
    {
64
        $commandStartedEvent = $this->findAndRemoveCommandStartedEvent($event->getRequestId());
65
        if (! $commandStartedEvent) {
66
            return;
67
        }
68
69
        $this->logCommand(Command::createForFailedCommand($commandStartedEvent, $event));
70
    }
71
72 13
    public function clear(): void
73
    {
74 13
        $this->commands = [];
75 13
    }
76
77 15
    public function count(): int
78
    {
79 15
        return count($this->commands);
80
    }
81
82
    /**
83
     * @return Command[]
84
     */
85 4
    public function getAll(): array
86
    {
87 4
        return $this->commands;
88
    }
89
90 28
    private function findAndRemoveCommandStartedEvent(string $requestId): ?CommandStartedEvent
91
    {
92 28
        $startedEvent = $this->startedCommands[$requestId] ?? null;
93 28
        unset($this->startedCommands[$requestId]);
94
95 28
        return $startedEvent;
96
    }
97
98 28
    private function logCommand(Command $command): void
99
    {
100 28
        $this->commands[] = $command;
101 28
    }
102
}
103