Completed
Push — master ( 547b58...93d99e )
by Aurimas
02:33
created

SocketPair::useLeft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 11
loc 11
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Thruster\Component\Socket;
4
5
use Thruster\Component\EventEmitter\EventEmitterTrait;
6
use Thruster\Component\EventLoop\EventLoopInterface;
7
use Thruster\Component\Socket\Exception\ConnectionException;
8
9
/**
10
 * Class SocketPair
11
 *
12
 * @package Thruster\Component\Socket
13
 * @author  Aurimas Niekis <[email protected]>
14
 */
15
class SocketPair
16
{
17
    const LEFT_SIDE = true;
18
    const RIGHT_SIDE = false;
19
20
    /**
21
     * @var EventLoopInterface
22
     */
23
    protected $loop;
24
25
    /**
26
     * @var Connection
27
     */
28
    protected $connection;
29
30
    /**
31
     * @var array
32
     */
33
    protected $sockets;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $mode;
39
40
    public function __construct(EventLoopInterface $loop)
41
    {
42
        $this->loop = $loop;
43
    }
44
45
    public function create()
46
    {
47
        $this->sockets = @stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
48
49
        if (false === $this->sockets) {
50
            throw new ConnectionException('Could not create socket pair');
51
        }
52
    }
53
54 View Code Duplication
    public function useLeft() : Connection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        list($left, $right) = $this->sockets;
57
58
        fclose($right);
59
60
        $this->mode = self::LEFT_SIDE;
61
        $this->connection = new Connection($left, $this->loop);
62
63
        return $this->connection;
64
    }
65
66 View Code Duplication
    public function useRight() : Connection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        list($left, $right) = $this->sockets;
69
70
        fclose($left);
71
72
        $this->mode = self::RIGHT_SIDE;
73
        $this->connection = new Connection($right, $this->loop);
74
75
        return $this->connection;
76
    }
77
78
    public function shutdown()
79
    {
80
        $this->connection->close();
81
    }
82
83
    public function getConnection() : Connection
84
    {
85
        return $this->connection;
86
    }
87
}
88