Completed
Push — master ( b90edc...95608f )
by Andreas
14:15 queued 11s
created

CommandLogger::logCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
    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)
63
    {
64
        $commandStartedEvent = $this->findAndRemoveCommandStartedEvent($event->getRequestId());
65
        if (! $commandStartedEvent) {
66
            return;
67
        }
68
69
        $this->logCommand(Command::createForFailedCommand($commandStartedEvent, $event));
70
    }
71
72
    public function clear() : void
73
    {
74
        $this->commands = [];
75
    }
76
77
    public function count() : int
78
    {
79
        return count($this->commands);
80
    }
81
82
    /**
83
     * @return Command[]
84
     */
85
    public function getAll() : array
86
    {
87
        return $this->commands;
88
    }
89
90
    private function findAndRemoveCommandStartedEvent(string $requestId) : ?CommandStartedEvent
91
    {
92
        $startedEvent = $this->startedCommands[$requestId] ?? null;
93
        unset($this->startedCommands[$requestId]);
94
95
        return $startedEvent;
96
    }
97
98
    private function logCommand(Command $command) : void
99
    {
100
        $this->commands[] = $command;
101
    }
102
}
103