SocketConnector::wait()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of JSON RPC Client.
4
 *
5
 * (c) Igor Lazarev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Strider2038\JsonRpcClient\Transport\Socket;
12
13
use Strider2038\JsonRpcClient\Exception\ConnectionFailedException;
14
15
/**
16
 * @internal
17
 *
18
 * @author Igor Lazarev <[email protected]>
19
 */
20
class SocketConnector
21
{
22
    /**
23
     * @throws ConnectionFailedException
24
     */
25 5
    public function open(string $url, int $connectionTimeoutUs, int $requestTimeoutUs): SocketConnection
26
    {
27 5
        $errorCode = null;
28 5
        $errorDescription = null;
29
30 5
        $stream = @stream_socket_client($url, $errorCode, $errorDescription, (float) $connectionTimeoutUs / 1000000);
31
32 5
        if (!is_resource($stream)) {
33
            throw new ConnectionFailedException($url, sprintf('%d: %s', $errorCode, $errorDescription));
34
        }
35
36 5
        if (false === stream_set_timeout($stream, 0, $requestTimeoutUs)) {
37
            throw new ConnectionFailedException($url, 'failed to set request timeout');
38
        }
39
40 5
        return new SocketConnection($stream, $url, $requestTimeoutUs);
41
    }
42
43
    public function wait(int $us): void
44
    {
45
        usleep($us);
46
    }
47
}
48