Completed
Pull Request — master (#37)
by Eugene
03:08
created

StreamConnection::open()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 9.536
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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\Connection;
15
16
use Tarantool\Client\Exception\ConnectionException;
17
use Tarantool\Client\IProto;
18
use Tarantool\Client\Packer\PackUtils;
19
20
final class StreamConnection implements Connection
21
{
22
    private const DEFAULT_URI = 'tcp://127.0.0.1:3301';
23
24
    private $uri;
25
26
    private $options = [
27
        'connect_timeout' => 5,
28
        'socket_timeout' => 5,
29
        'tcp_nodelay' => true,
30
    ];
31
32
    private $stream;
33
34 120
    public function __construct(string $uri = self::DEFAULT_URI, array $options = [])
35
    {
36 120
        $this->uri = $uri;
37
38 120
        if ($options) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39 4
            $this->options = $options + $this->options;
40
        }
41 120
    }
42
43 120
    public function open() : string
44
    {
45 120
        $this->close();
46
47 120
        $stream = @\stream_socket_client(
48 120
            $this->uri,
49 120
            $errorCode,
50 120
            $errorMessage,
51 120
            (float) $this->options['connect_timeout'],
52 120
            \STREAM_CLIENT_CONNECT,
53 120
            \stream_context_create(['socket' => ['tcp_nodelay' => (bool) $this->options['tcp_nodelay']]])
54
        );
55
56 120
        if (false === $stream) {
57 3
            throw new ConnectionException(\sprintf('Unable to connect to %s: %s.', $this->uri, $errorMessage));
58
        }
59
60 117
        $this->stream = $stream;
61 117
        \stream_set_timeout($this->stream, $this->options['socket_timeout']);
62
63 117
        $greeting = $this->read(IProto::GREETING_SIZE, 'Unable to read greeting.');
64
65 114
        return IProto::parseGreeting($greeting);
66
    }
67
68 120
    public function close() : void
69
    {
70 120
        if ($this->stream) {
71 2
            \fclose($this->stream);
72 2
            $this->stream = null;
73
        }
74 120
    }
75
76 100
    public function isClosed() : bool
77
    {
78 100
        return !\is_resource($this->stream);
79
    }
80
81 99
    public function send(string $data) : string
82
    {
83 99
        if (!\fwrite($this->stream, $data)) {
84
            throw new ConnectionException('Unable to write request.');
85
        }
86
87 99
        $length = $this->read(IProto::LENGTH_SIZE, 'Unable to read response length.');
88 97
        $length = PackUtils::unpackLength($length);
89
90 97
        return $this->read($length, 'Unable to read response.');
91
    }
92
93 117
    private function read(int $length, string $errorMessage) : string
94
    {
95 117
        if ($data = \stream_get_contents($this->stream, $length)) {
96 114
            return $data;
97
        }
98
99 7
        $meta = \stream_get_meta_data($this->stream);
100 7
        throw new ConnectionException($meta['timed_out'] ? 'Read timed out.' : $errorMessage);
101
    }
102
}
103