Passed
Push — master ( 01f538...95b550 )
by Igor
04:19 queued 59s
created

SocketConnector::wait()   A

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
 * @author Igor Lazarev <[email protected]>
17
 */
18
class SocketConnector
19
{
20
    /**
21
     * @throws ConnectionFailedException
22
     */
23 5
    public function open(string $url, int $connectionTimeoutUs, int $requestTimeoutUs): SocketConnection
24
    {
25 5
        $errorCode = null;
26 5
        $errorDescription = null;
27
28 5
        $stream = @stream_socket_client($url, $errorCode, $errorDescription, (float) $connectionTimeoutUs / 1000000);
29
30 5
        if (!is_resource($stream)) {
31
            throw new ConnectionFailedException($url, sprintf('%d: %s', $errorCode, $errorDescription));
32
        }
33
34 5
        if (false === stream_set_timeout($stream, 0, $requestTimeoutUs)) {
35
            throw new ConnectionFailedException($url, 'failed to set request timeout');
36
        }
37
38 5
        return new SocketConnection($stream, $url, $requestTimeoutUs);
39
    }
40
41
    public function wait(int $us): void
42
    {
43
        usleep($us);
44
    }
45
}
46