Queue::onResize()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of the phpflo/phpflo package.
4
 *
5
 * (c) Henri Bergius <[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 PhpFlo\Component;
12
13
use PhpFlo\Component;
14
15
/**
16
 * Component for creating a simple queue of messages.
17
 *
18
 * All incomming messages are kept in memory. As soon as the queue reaches the
19
 * pre-configured size of 100 messages, the current list of messages is send.
20
 *
21
 * You can reconfigure the queue size of the component by sending a message to
22
 * the `size` input port of this component. If the incomming data is not a
23
 * positive integer, an error message will be send to the `err` out port of this
24
 * component.
25
 *
26
 * @author Marijn Huizendveld <[email protected]>
27
 */
28
class Queue extends Component
29
{
30
    /**
31
     * @var integer
32
     */
33
    private $size;
34
35
    /**
36
     * @var array<mixed>
37
     */
38
    private $messages;
39
40
    public function __construct()
41
    {
42
        $this->size = 100;
43
        $this->messages = [];
44
45
        $this->inPorts()
46
            ->add('in', ['datatype' => 'all'])
47
            ->add('size', ['datatype' => 'all']);
48
49
        $this->outPorts()
50
            ->add('error', ['datatype' => 'all'])
51
            ->add('messages', ['datatype' => 'all']);
52
53
        $this->inPorts()->in->on('data', [$this, 'onAppendQueue']);
54
        $this->inPorts()->in->on('detach', [$this, 'onStreamEnded']);
55
56
        $this->inPorts()->size->on('data', [$this, 'onResize']);
57
    }
58
59
    /**
60
     * @param mixed $data
61
     */
62
    public function onAppendQueue($data)
63
    {
64
        $this->messages[] = $data;
65
66
        $this->sendQueue();
67
    }
68
69
    public function onStreamEnded()
70
    {
71
        $this->flushQueue();
72
    }
73
74
    /**
75
     * @param mixed $data
76
     */
77
    public function onResize($data)
78
    {
79
        if (!is_int($data) || 0 > $data) {
80
            $dumped = var_dump($data);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($data); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
Bug introduced by
Are you sure the assignment to $dumped is correct as var_dump($data) (which targets var_dump()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
81
82
            $this->outPorts()->error->send(
83
                "Invalid queue size: '{$dumped}'. Queue resize operation expects a positive integer value."
84
            );
85
        }
86
87
        $this->size = $data;
88
89
        $this->sendQueue();
90
    }
91
92
    private function sendQueue()
93
    {
94
        if ($this->size <= count($this->messages)) {
95
            $this->flushQueue();
96
        }
97
    }
98
99
    private function flushQueue()
100
    {
101
        $this->outPorts()
102
            ->messages
103
            ->send($this->messages)
104
            ->disconnect();
105
106
        $this->messages = [];
107
    }
108
}
109