Completed
Push — master ( 95608f...d78a07 )
by Andreas
14s queued 10s
created

CommandLogger::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 42
    public function register() : void
28
    {
29 42
        if ($this->registered) {
30
            return;
31
        }
32
33 42
        $this->registered = true;
34 42
        addSubscriber($this);
35 42
    }
36
37 42
    public function unregister() : void
38
    {
39 42
        if (! $this->registered) {
40
            return;
41
        }
42
43 42
        removeSubscriber($this);
44 42
        $this->registered = false;
45 42
    }
46
47 42
    public function commandStarted(CommandStartedEvent $event)
48
    {
49 42
        $this->startedCommands[$event->getRequestId()] = $event;
50 42
    }
51
52 42
    public function commandSucceeded(CommandSucceededEvent $event)
53
    {
54 42
        $commandStartedEvent = $this->findAndRemoveCommandStartedEvent($event->getRequestId());
55 42
        if (! $commandStartedEvent) {
56
            return;
57
        }
58
59 42
        $this->logCommand(Command::createForSucceededCommand($commandStartedEvent, $event));
60 42
    }
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 24
    public function clear() : void
73
    {
74 24
        $this->commands = [];
75 24
    }
76
77 26
    public function count() : int
78
    {
79 26
        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 42
    private function findAndRemoveCommandStartedEvent(string $requestId) : ?CommandStartedEvent
91
    {
92 42
        $startedEvent = $this->startedCommands[$requestId] ?? null;
93 42
        unset($this->startedCommands[$requestId]);
94
95 42
        return $startedEvent;
96
    }
97
98 42
    private function logCommand(Command $command) : void
99
    {
100 42
        $this->commands[] = $command;
101 42
    }
102
}
103