Completed
Push — master ( 6e2fde...312add )
by Dmitry
03:06
created

Client::connect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use Exception;
6
use Tarantool\Client\Connection\Connection;
7
use Tarantool\Client\Client as TarantoolClient;
8
use Tarantool\Client\Request\Request;
9
10
class Client extends TarantoolClient
11
{
12
    private $logging = false;
13
    private $log = [];
14 64
15
    private $disabledRequests = [];
16 64
17
    public function connect()
18 64
    {
19 64
        $start = microtime(1);
20
        parent::connect();
21 64
        $this->log($start, Connection::class);
22
    }
23
24 64
    public function disableRequest($class)
25
    {
26 64
        if (!is_subclass_of($class, Request::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Tarantool\Client\Request\Request::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
27 64
            throw new Exception("Invalid request type: " . $class);
28
        }
29 64
        $this->disabledRequests[] = $class;
30
    }
31 64
32
    public function resetDisabled()
33
    {
34 64
        $this->disabledRequests = [];
35
    }
36 64
37 64
    public function sendRequest(Request $request)
38
    {
39 64
        if (in_array(get_class($request), $this->disabledRequests)) {
40
            throw new Exception(get_class($request) . ' is disabled');
41 1
        }
42
        $start = microtime(1);
43 1
        $response = parent::sendRequest($request);
44 1
        $this->log($start, get_class($request), $request->getBody(), $response->getData());
0 ignored issues
show
Bug introduced by
It seems like $response->getData() targeting Tarantool\Client\Response::getData() can also be of type null; however, Tarantool\Mapper\Client::log() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
        return $response;
46 4
    }
47
48 4
    private function log($start, $class, $request = [], $response = [])
49
    {
50
        if ($this->logging) {
51
            $this->log[] = [microtime(1) - $start, $class, $request, $response];
52
        }
53
    }
54
55
    public function getTimeSummary()
56
    {
57
        $summary = 0;
58
        foreach ($this->log as $request) {
59
            $summary += $request[0];
60
        }
61
        return $summary;
62
    }
63
64
    public function setLogging($logging)
65
    {
66
        $this->logging = $logging;
67
    }
68
69
    public function getLog()
70
    {
71
        return $this->log;
72
    }
73
74
    public function resetLog()
75
    {
76
        $this->log = [];
77
    }
78
}
79