Completed
Pull Request — master (#37)
by Eugene
09:24
created

DefaultHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 38
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConnection() 0 4 1
A getPacker() 0 4 1
A handle() 0 16 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
    public function __construct(Connection $connection, Packer $packer)
28
    {
29
        $this->connection = $connection;
30
        $this->packer = $packer;
31
    }
32
33
    public function getConnection() : Connection
34
    {
35
        return $this->connection;
36
    }
37
38
    public function getPacker() : Packer
39
    {
40
        return $this->packer;
41
    }
42
43
    public function handle(Request $request) : Response
44
    {
45
        if ($this->connection->isClosed()) {
46
            $this->connection->open();
47
        }
48
49
        $data = $this->packer->pack($request);
50
        $data = $this->connection->send($data);
51
52
        $response = $this->packer->unpack($data);
53
        if (!$response->isError()) {
54
            return $response;
55
        }
56
57
        throw RequestFailed::fromErrorResponse($response);
58
    }
59
}
60