1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the tarantool/client package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Handler; |
15
|
|
|
|
16
|
|
|
use Tarantool\Client\Connection\Connection; |
17
|
|
|
use Tarantool\Client\Exception\RequestFailed; |
18
|
|
|
use Tarantool\Client\Exception\UnexpectedResponse; |
19
|
|
|
use Tarantool\Client\Packer\Packer; |
20
|
|
|
use Tarantool\Client\Request\Request; |
21
|
|
|
use Tarantool\Client\Response; |
22
|
|
|
|
23
|
|
|
final class DefaultHandler implements Handler |
24
|
|
|
{ |
25
|
|
|
private $connection; |
26
|
|
|
private $packer; |
27
|
|
|
|
28
|
955 |
|
public function __construct(Connection $connection, Packer $packer) |
29
|
|
|
{ |
30
|
955 |
|
$this->connection = $connection; |
31
|
955 |
|
$this->packer = $packer; |
32
|
|
|
} |
33
|
|
|
|
34
|
676 |
|
public function handle(Request $request) : Response |
35
|
|
|
{ |
36
|
676 |
|
$packet = $this->packer->pack($request, $sync = \mt_rand()); |
37
|
676 |
|
$this->connection->open(); |
38
|
617 |
|
$packet = $this->connection->send($packet); |
39
|
|
|
|
40
|
601 |
|
$response = $this->packer->unpack($packet); |
41
|
|
|
|
42
|
601 |
|
if ($sync !== $response->getSync()) { |
43
|
8 |
|
$this->connection->close(); |
44
|
8 |
|
throw UnexpectedResponse::outOfSync($sync, $response->getSync()); |
45
|
|
|
} |
46
|
|
|
|
47
|
593 |
|
if ($response->isError()) { |
48
|
96 |
|
throw RequestFailed::fromErrorResponse($response); |
49
|
|
|
} |
50
|
|
|
|
51
|
569 |
|
return $response; |
52
|
|
|
} |
53
|
|
|
|
54
|
162 |
|
public function getConnection() : Connection |
55
|
|
|
{ |
56
|
162 |
|
return $this->connection; |
57
|
|
|
} |
58
|
|
|
|
59
|
56 |
|
public function getPacker() : Packer |
60
|
|
|
{ |
61
|
56 |
|
return $this->packer; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|