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

Command::getRequestId()   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 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\APM;
6
7
use LogicException;
8
use MongoDB\Driver\Monitoring\CommandFailedEvent;
9
use MongoDB\Driver\Monitoring\CommandStartedEvent;
10
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
11
use MongoDB\Driver\Server;
12
13
final class Command
14
{
15
    /** @var CommandStartedEvent */
16
    private $startedEvent;
17
18
    /** @var CommandSucceededEvent|CommandFailedEvent */
19
    private $finishedEvent;
20
21 28
    private function __construct()
22
    {
23 28
    }
24
25 28
    public static function createForSucceededCommand(CommandStartedEvent $startedEvent, CommandSucceededEvent $succeededEvent): self
26
    {
27 28
        self::checkRequestIds($startedEvent, $succeededEvent);
28
29 28
        $instance = new self();
30 28
        $instance->startedEvent = $startedEvent;
31 28
        $instance->finishedEvent = $succeededEvent;
32
33 28
        return $instance;
34
    }
35
36
    public static function createForFailedCommand(CommandStartedEvent $startedEvent, CommandFailedEvent $failedEvent): self
37
    {
38
        self::checkRequestIds($startedEvent, $failedEvent);
39
40
        $instance = new self();
41
        $instance->startedEvent = $startedEvent;
42
        $instance->finishedEvent = $failedEvent;
43
44
        return $instance;
45
    }
46
47
    /**
48
     * @param CommandSucceededEvent|CommandFailedEvent $finishedEvent
49
     */
50 28
    private static function checkRequestIds(CommandStartedEvent $startedEvent, $finishedEvent): void
51
    {
52 28
        if ($startedEvent->getRequestId() !== $finishedEvent->getRequestId()) {
53
            throw new LogicException('Cannot create APM command for events with different request IDs');
54
        }
55 28
    }
56
57 4
    public function getCommandName(): string
58
    {
59 4
        return $this->startedEvent->getCommandName();
60
    }
61
62 4
    public function getCommand(): object
63
    {
64 4
        return $this->startedEvent->getCommand();
65
    }
66
67
    public function getDurationMicros(): int
68
    {
69
        return $this->finishedEvent->getDurationMicros();
70
    }
71
72
    public function getRequestId(): string
73
    {
74
        return $this->startedEvent->getRequestId();
75
    }
76
77
    public function getServer(): Server
78
    {
79
        return $this->finishedEvent->getServer();
80
    }
81
82
    public function getReply(): object
83
    {
84
        return $this->finishedEvent->getReply();
85
    }
86
87
    public function getError(): ?\Throwable
88
    {
89
        return $this->finishedEvent instanceof CommandFailedEvent ? $this->finishedEvent->getError() : null;
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Monitoring\CommandFailedEvent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
90
    }
91
}
92