SocketConnector   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 26
ccs 7
cts 11
cp 0.6364
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A wait() 0 3 1
A open() 0 16 3
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