Completed
Pull Request — master (#37)
by Eugene
05:10
created

DefaultHandler::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Tarantool Client package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tarantool\Client\Handler;
15
16
use Tarantool\Client\Connection\Connection;
17
use Tarantool\Client\Exception\RequestFailed;
18
use Tarantool\Client\Packer\Packer;
19
use Tarantool\Client\Request\Request;
20
use Tarantool\Client\Response;
21
22
final class DefaultHandler implements Handler
23
{
24
    private $connection;
25
    private $packer;
26
27 250
    public function __construct(Connection $connection, Packer $packer)
28
    {
29 250
        $this->connection = $connection;
30 250
        $this->packer = $packer;
31 250
    }
32
33 226
    public function handle(Request $request) : Response
34
    {
35 226
        if ($this->connection->isClosed()) {
36 222
            $this->connection->open();
37
        }
38
39 186
        $data = $this->packer->pack($request);
40 186
        $data = $this->connection->send($data);
41
42 178
        $response = $this->packer->unpack($data);
43 178
        if (!$response->isError()) {
44 170
            return $response;
45
        }
46
47 26
        throw RequestFailed::fromErrorResponse($response);
48
    }
49
50 16
    public function getConnection() : Connection
51
    {
52 16
        return $this->connection;
53
    }
54
55 2
    public function getPacker() : Packer
56
    {
57 2
        return $this->packer;
58
    }
59
}
60