ZmqBuffer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
crap 2
1
<?php
2
3
namespace Dazzle\Zmq;
4
5
use Dazzle\Event\BaseEventEmitter;
6
use Dazzle\Loop\LoopInterface;
7
use ZMQ;
8
use ZMQSocket as RawZMQSocket;
9
use ZMQSocketException;
10
11
class ZmqBuffer extends BaseEventEmitter
12
{
13
    /**
14
     * @var RawZMQSocket
15
     */
16
    public $socket;
17
18
    /**
19
     * @var bool
20
     */
21
    public $closed = false;
22
23
    /**
24
     * @var bool
25
     */
26
    public $listening = false;
27
28
    /**
29
     * @var LoopInterface
30
     */
31
    private $loop;
32
33
    /**
34
     * @var resource
35
     */
36
    private $fd;
37
38
    /**
39
     * @var callable
40
     */
41
    private $writeListener;
42
43
    /**
44
     * @var string[]
45
     */
46
    private $messages = [];
47
48
    /**
49
     * @param RawZMQSocket $socket
50
     * @param resource $fd
51
     * @param LoopInterface $loop
52
     * @param $writeListener
53
     */
54
    public function __construct(RawZMQSocket $socket, $fd, LoopInterface $loop, $writeListener)
55
    {
56
        $this->socket = $socket;
57
        $this->fd = $fd;
58
        $this->loop = $loop;
59
        $this->writeListener = $writeListener;
60
    }
61
62
    /**
63
     * Send message.
64
     *
65
     * @param string $message
66
     * @return bool
67
     */
68
    public function send($message)
69
    {
70
        if ($this->closed)
71
        {
72
            return false;
73
        }
74
75
        $this->messages[] = $message;
76
77
        if (!$this->listening)
78
        {
79
            $this->listening = true;
80
            $this->loop->addWriteStream($this->fd, $this->writeListener);
81
        }
82
83
        return true;
84
    }
85
86
    /**
87
     * Close buffer.
88
     */
89
    public function end()
90
    {
91
        $this->closed = true;
92
93
        if (!$this->listening)
94
        {
95
            $this->emit('end');
96
        }
97
    }
98
99
    /**
100
     * Handle ZMQ Write Event.
101
     */
102
    public function handleWriteEvent()
103
    {
104
        foreach ($this->messages as $i=>$message)
105
        {
106
            try
107
            {
108
                $message = (array) $message;
109
                $sent = (bool) $this->socket->sendmulti($message, ZMQ::MODE_DONTWAIT);
0 ignored issues
show
Unused Code introduced by
$sent is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
110
                unset($this->messages[$i]);
111
                if (0 === count($this->messages))
112
                {
113
                    $this->loop->removeWriteStream($this->fd);
114
                    $this->listening = false;
115
                    $this->emit('end');
116
                }
117
            }
118
            catch (ZMQSocketException $ex)
0 ignored issues
show
Bug introduced by
The class ZMQSocketException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
119
            {
120
                $this->emit('error', [ $ex ]);
121
            }
122
        }
123
    }
124
}
125