Completed
Push — master ( d06bab...02e31a )
by Eugene
03:46
created

Client::call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Tarantool\Client;
4
5
use Tarantool\Client\Connection\Connection;
6
use Tarantool\Client\Exception\Exception;
7
use Tarantool\Client\Packer\Packer;
8
use Tarantool\Client\Packer\PeclPacker;
9
use Tarantool\Client\Request\AuthenticateRequest;
10
use Tarantool\Client\Request\CallRequest;
11
use Tarantool\Client\Request\EvaluateRequest;
12
use Tarantool\Client\Request\PingRequest;
13
use Tarantool\Client\Request\Request;
14
use Tarantool\Client\Schema\Index;
15
use Tarantool\Client\Schema\Space;
16
17
class Client
18
{
19
    private $connection;
20
    private $packer;
21
    private $salt;
22
    private $username;
23
    private $password;
24
    private $spaces = [];
25
26
    /**
27
     * @param Connection  $connection
28
     * @param Packer|null $packer
29
     */
30 53
    public function __construct(Connection $connection, Packer $packer = null)
31
    {
32 53
        $this->connection = $connection;
33 53
        $this->packer = $packer ?: new PeclPacker();
34 53
    }
35
36 3
    public function getConnection()
37
    {
38 3
        return $this->connection;
39
    }
40
41 50
    public function connect()
42
    {
43 50
        $this->salt = $this->connection->open();
44
45 29
        if ($this->username) {
46 2
            $this->authenticate($this->username, $this->password);
47
        }
48 29
    }
49
50 50
    public function disconnect()
51
    {
52 50
        $this->connection->close();
53 50
        $this->salt = null;
54 50
    }
55
56 17
    public function isDisconnected()
57
    {
58 17
        return $this->connection->isClosed() || !$this->salt;
59
    }
60
61 17
    public function authenticate($username, $password = null)
62
    {
63 17
        if ($this->isDisconnected()) {
64 15
            $this->salt = $this->connection->open();
65
        }
66
67 17
        $request = new AuthenticateRequest($this->salt, $username, $password);
68 17
        $response = $this->sendRequest($request);
69
70 10
        $this->username = $username;
71 10
        $this->password = $password;
72
73 10
        $this->flushSpaces();
74
75 10
        return $response;
76
    }
77
78 6
    public function ping()
79
    {
80 6
        $request = new PingRequest();
81
82 6
        return $this->sendRequest($request);
83
    }
84
85
    /**
86
     * @param string|int $space
87
     *
88
     * @return Space
89
     */
90 62
    public function getSpace($space)
91
    {
92 62
        if (isset($this->spaces[$space])) {
93 56
            return $this->spaces[$space];
94
        }
95
96 15
        if (!is_string($space)) {
97 10
            return $this->spaces[$space] = new Space($this, $space);
98
        }
99
100 13
        $spaceId = $this->getSpaceIdByName($space);
101
102 11
        return $this->spaces[$space] = $this->spaces[$spaceId] = new Space($this, $spaceId);
103
    }
104
105 7
    public function call($funcName, array $args = [])
106
    {
107 7
        $request = new CallRequest($funcName, $args);
108
109 7
        return $this->sendRequest($request);
110
    }
111
112 25
    public function evaluate($expr, array $args = [])
113
    {
114 25
        $request = new EvaluateRequest($expr, $args);
115
116 25
        return $this->sendRequest($request);
117
    }
118
119 12
    public function flushSpaces()
120
    {
121 12
        $this->spaces = [];
122 12
    }
123
124 110
    public function sendRequest(Request $request)
125
    {
126 110
        if ($this->connection->isClosed()) {
127 26
            $this->connect();
128
        }
129
130 108
        $data = $this->packer->pack($request);
131 108
        $data = $this->connection->send($data);
132
133 108
        return $this->packer->unpack($data);
134
    }
135
136 13
    private function getSpaceIdByName($spaceName)
137
    {
138 13
        $schema = $this->getSpace(Space::VSPACE);
139 13
        $response = $schema->select([$spaceName], Index::SPACE_NAME);
140 13
        $data = $response->getData();
141
142 13
        if (empty($data)) {
143 3
            throw new Exception("Space '$spaceName' does not exist");
144
        }
145
146 11
        return $data[0][0];
147
    }
148
}
149