ProfiledClientStorage   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 92
ccs 36
cts 36
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A registerCalls() 0 12 3
A registerResponse() 0 13 2
A getFullPairs() 0 9 1
A getUnmatchedRequestPairs() 0 9 1
A getUnmatchedResponsePairs() 0 4 1
A getClientName() 0 4 1
1
<?php
2
3
namespace ScayTrase\Api\Rpc\Decorators;
4
5
use ScayTrase\Api\Rpc\RpcRequestInterface;
6
use ScayTrase\Api\Rpc\RpcResponseInterface;
7
8
final class ProfiledClientStorage
9
{
10
    /** @var array[] */
11
    private $pairs = [];
12
    /** @var array[] */
13
    private $unmatchedResponse = [];
14
    /** @var  string */
15
    private $clientName;
16
17
    /**
18
     * RpcProfiler constructor.
19
     *
20
     * @param string $clientName
21
     */
22 2
    public function __construct($clientName)
23
    {
24 2
        $this->clientName = (string)$clientName;
25 2
    }
26
27
    /**
28
     * @param RpcRequestInterface|RpcRequestInterface[] $calls
29
     */
30 2
    public function registerCalls($calls)
31
    {
32 2
        if (!is_array($calls)) {
33 1
            $calls = [$calls];
34 1
        }
35
36 2
        $time = microtime(true);
37 2
        foreach ($calls as $call) {
38 2
            $this->pairs[spl_object_hash($call)]['start']   = $time;
39 2
            $this->pairs[spl_object_hash($call)]['request'] = $call;
40 2
        }
41 2
    }
42
43
    /**
44
     * @param RpcResponseInterface $response
45
     * @param RpcRequestInterface  $request
46
     */
47 2
    public function registerResponse(RpcResponseInterface $response, RpcRequestInterface $request = null)
48
    {
49 2
        $time = microtime(true);
50
51 2
        if (null === $request) {
52 1
            $this->unmatchedResponse[]['response'] = $response;
53
54 1
            return;
55
        }
56
57 1
        $this->pairs[spl_object_hash($request)]['stop']     = $time;
58 1
        $this->pairs[spl_object_hash($request)]['response'] = $response;
59 1
    }
60
61
    /**
62
     * @return array[]
63
     */
64 1
    public function getFullPairs()
65
    {
66 1
        return array_filter(
67 1
            $this->pairs,
68
            function (array $row) {
69 1
                return isset($row['response']);
70
            }
71 1
        );
72
    }
73
74 1
    public function getUnmatchedRequestPairs()
75
    {
76 1
        return array_filter(
77 1
            $this->pairs,
78 1
            function (array $row) {
79 1
                return !isset($row['response']);
80
            }
81 1
        );
82
    }
83
84
    /**
85
     * @return array[]
86
     */
87 1
    public function getUnmatchedResponsePairs()
88
    {
89 1
        return $this->unmatchedResponse;
90
    }
91
92
    /**
93
     * @return string
94
     */
95 1
    public function getClientName()
96
    {
97 1
        return $this->clientName;
98
    }
99
}
100