Completed
Pull Request — master (#37)
by Eugene
06:36
created

StreamConnection::close()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
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\CommunicationFailed;
17
use Tarantool\Client\Exception\ConnectionFailed;
18
use Tarantool\Client\IProto;
19
use Tarantool\Client\Packer\PackUtils;
20
21
final class StreamConnection implements Connection
22
{
23
    public const DEFAULT_URI = 'tcp://127.0.0.1:3301';
24
25
    private $stream;
26
    private $uri;
27
    private $options = [
28
        'connect_timeout' => 5,
29
        'socket_timeout' => 5,
30
        'tcp_nodelay' => true,
31
    ];
32
33 258
    public function __construct(string $uri = self::DEFAULT_URI, array $options = [])
34
    {
35 258
        $this->uri = $uri;
36
37 258
        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...
38 12
            $this->options = $options + $this->options;
39
        }
40 258
    }
41
42 258
    public function open() : string
43
    {
44 258
        $this->close();
45
46 258
        $stream = @\stream_socket_client(
47 258
            $this->uri,
48 258
            $errorCode,
49 258
            $errorMessage,
50 258
            (float) $this->options['connect_timeout'],
51 258
            \STREAM_CLIENT_CONNECT,
52 258
            \stream_context_create(['socket' => ['tcp_nodelay' => (bool) $this->options['tcp_nodelay']]])
53
        );
54
55 258
        if (false === $stream) {
56 6
            throw ConnectionFailed::fromUriAndReason($this->uri, $errorMessage);
57
        }
58
59 252
        $this->stream = $stream;
60 252
        \stream_set_timeout($this->stream, $this->options['socket_timeout']);
61
62 252
        $greeting = $this->read(IProto::GREETING_SIZE, 'Unable to read greeting.');
63
64 248
        return IProto::parseGreeting($greeting);
65
    }
66
67 258
    public function close() : void
68
    {
69 258
        if ($this->stream) {
70 4
            \fclose($this->stream);
71 4
            $this->stream = null;
72
        }
73 258
    }
74
75 254
    public function isClosed() : bool
76
    {
77 254
        return !\is_resource($this->stream);
78
    }
79
80 214
    public function send(string $data) : string
81
    {
82 214
        if (!\fwrite($this->stream, $data)) {
83
            throw new CommunicationFailed('Unable to write request.');
84
        }
85
86 214
        $length = $this->read(IProto::LENGTH_SIZE, 'Unable to read response length.');
87 208
        $length = PackUtils::unpackLength($length);
88
89 208
        return $this->read($length, 'Unable to read response.');
90
    }
91
92 252
    private function read(int $length, string $errorMessage) : string
93
    {
94 252
        if ($data = \stream_get_contents($this->stream, $length)) {
95 248
            return $data;
96
        }
97
98 14
        $meta = \stream_get_meta_data($this->stream);
99 14
        throw new CommunicationFailed($meta['timed_out'] ? 'Read timed out.' : $errorMessage);
100
    }
101
}
102