Completed
Push — master ( cc1862...4bc456 )
by Eugene
06:46
created

ClientBuilder::findOpenTcpPort()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
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\Tests\Integration;
15
16
use Tarantool\Client\Client;
17
use Tarantool\Client\Connection\Connection;
18
use Tarantool\Client\Connection\StreamConnection;
19
use Tarantool\Client\Handler\DefaultHandler;
20
use Tarantool\Client\Handler\MiddlewareHandler;
21
use Tarantool\Client\Middleware\AuthenticationMiddleware;
22
use Tarantool\Client\Middleware\RetryMiddleware;
23
use Tarantool\Client\Packer\Packer;
24
use Tarantool\Client\Packer\PeclPacker;
25
use Tarantool\Client\Packer\PurePacker;
26
27
final class ClientBuilder
28
{
29
    private const PACKER_PURE = 'pure';
30
    private const PACKER_PECL = 'pecl';
31
32
    private const DEFAULT_TCP_HOST = '127.0.0.1';
33
    private const DEFAULT_TCP_PORT = 3301;
34
35
    private $packerType;
36
    private $packerPureFactory;
37
    private $packerPeclFactory;
38
    private $uri;
39
    private $options = [];
40
    private $connectionOptions = [];
41
42
    public function setPackerType(string $packerType) : self
43
    {
44
        $this->packerType = $packerType;
45
46
        return $this;
47
    }
48
49
    public function setPackerPureFactory(\Closure $factory) : self
50
    {
51
        $this->packerPureFactory = $factory;
52
53
        return $this;
54
    }
55
56
    public function setPackerPeclFactory(\Closure $factory) : self
57
    {
58
        $this->packerPeclFactory = $factory;
59
60
        return $this;
61
    }
62
63
    public function setOptions(array $options) : self
64
    {
65
        $this->options = $options;
66
67
        return $this;
68
    }
69
70
    public function setConnectionOptions(array $options) : self
71
    {
72
        $this->connectionOptions = $options;
73
74
        return $this;
75
    }
76
77
    public function isTcpConnection() : bool
78
    {
79
        return 0 === strpos($this->uri, 'tcp:');
80
    }
81
82
    public function setHost(string $host) : self
83
    {
84
        $port = parse_url($this->uri, PHP_URL_PORT);
85
        $this->uri = sprintf('tcp://%s:%d', $host, $port ?: self::DEFAULT_TCP_PORT);
86
87
        return $this;
88
    }
89
90
    public function setPort(int $port) : self
91
    {
92
        $host = parse_url($this->uri, PHP_URL_HOST);
93
        $this->uri = sprintf('tcp://%s:%d', $host ?: self::DEFAULT_TCP_HOST, $port);
94
95
        return $this;
96
    }
97
98
    public function setUri(string $uri) : self
99
    {
100
        if (0 === strpos($uri, '/')) {
101
            $uri = 'unix://'.$uri;
102
        } elseif (0 === strpos($uri, 'unix/:')) {
103
            $uri = 'unix://'.substr($uri, 6);
104
        } elseif (!preg_match('/[\D]/', $uri)) {
105
            $uri = 'tcp://127.0.0.1:'.$uri;
106
        } elseif (0 !== strpos($uri, 'tcp://') && (0 !== strpos($uri, 'unix://'))) {
107
            $uri = 'tcp://'.$uri;
108
        }
109
110
        $this->uri = $uri;
111
112
        return $this;
113
    }
114
115
    public function getUri() : string
116
    {
117
        return $this->uri;
118
    }
119
120
    public function build() : Client
121
    {
122
        $connection = $this->createConnection();
123
        $packer = $this->createPacker();
124
        $handler = new DefaultHandler($connection, $packer);
125
126
        if (isset($this->options['max_retries'])) {
127
            $handler = MiddlewareHandler::create($handler, RetryMiddleware::linear($this->options['max_retries']));
128
        }
129
        if (isset($this->options['username'])) {
130
            $handler = MiddlewareHandler::create($handler, new AuthenticationMiddleware($this->options['username'], $this->options['password'] ?? ''));
131
        }
132
133
        return new Client($handler);
134
    }
135
136
    public static function createFromEnv() : self
137
    {
138
        return (new self())
139
            ->setPackerType(getenv('TNT_PACKER'))
140
            ->setUri(getenv('TNT_LISTEN_URI'));
141
    }
142
143
    public static function createForFakeServer() : self
144
    {
145
        $builder = self::createFromEnv();
146
147
        if ($builder->isTcpConnection()) {
148
            $builder->setHost('0.0.0.0');
149
            $builder->setPort(self::findAvailableTcpPort(8000));
150
        } else {
151
            $builder->setUri(sprintf('unix://%s/tnt_client_%s.sock', sys_get_temp_dir(), bin2hex(random_bytes(10))));
152
        }
153
154
        return $builder;
155
    }
156
157
    public function createConnection() : Connection
158
    {
159
        if (!$this->uri) {
160
            throw new \LogicException('Connection URI is not set');
161
        }
162
163
        return StreamConnection::create($this->uri, $this->connectionOptions);
164
    }
165
166
    public function createPacker() : Packer
167
    {
168
        if (self::PACKER_PURE === $this->packerType) {
169
            return $this->packerPureFactory ? ($this->packerPureFactory)() : new PurePacker();
170
        }
171
172
        if (self::PACKER_PECL === $this->packerType) {
173
            return $this->packerPeclFactory ? ($this->packerPeclFactory)() : new PeclPacker();
174
        }
175
176
        throw new \UnexpectedValueException(sprintf('"%s" packer is not supported', $this->packerType));
177
    }
178
179
    private static function findAvailableTcpPort(int $min) : int
180
    {
181
        $maxTries = 10;
182
        $try = 0;
183
184
        while (true) {
185
            $port = $min + $try * 500 + random_int(1, 500);
186
            if (!$fp = @stream_socket_client("tcp://127.0.0.1:$port", $errorCode, $errorMessage, 1)) {
187
                return $port;
188
            }
189
190
            fclose($fp);
191
192
            if (++$try === $maxTries) {
193
                throw new \RuntimeException('Failed to find open tcp port');
194
            }
195
        }
196
    }
197
}
198