UnixConnector::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Thruster\Component\SocketClient;
4
5
use RuntimeException;
6
use Thruster\Component\EventLoop\EventLoopInterface;
7
use Thruster\Component\Promise\FulfilledPromise;
8
use Thruster\Component\Promise\PromiseInterface;
9
use Thruster\Component\Promise\RejectedPromise;
10
use Thruster\Component\Stream\Stream;
11
12
/**
13
 * Class UnixConnector
14
 *
15
 * @package Thruster\Component\SocketClient
16
 * @author  Aurimas Niekis <[email protected]>
17
 */
18
class UnixConnector implements ConnectorInterface
19
{
20
    private $loop;
21
22 2
    public function __construct(EventLoopInterface $loop)
23
    {
24 2
        $this->loop = $loop;
25 2
    }
26
27 2
    public function create(string $path, int $unusedPort = 0) : PromiseInterface
28
    {
29 2
        $resource = @stream_socket_client('unix://' . $path, $errNo, $errStr, 1.0);
30
31 2
        if (false === $resource) {
32 1
            return new RejectedPromise(
33 1
                new RuntimeException(
34 1
                    sprintf('Unable to connect to unix domain socket "%s": %s', $path, $errStr),
35
                    $errNo
36
                )
37
            );
38
        }
39
40 1
        return new FulfilledPromise(new Stream($resource, $this->loop));
41
    }
42
}
43