MessageManagerMessageIterator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 139
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A current() 0 4 1
A next() 0 5 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A isBufferEmpty() 0 4 1
A setCurrent() 0 8 2
A bufferize() 0 12 3
A findNextMessages() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Iterator;
15
16
use Sonata\NotificationBundle\Model\MessageInterface;
17
use Sonata\NotificationBundle\Model\MessageManagerInterface;
18
19
class MessageManagerMessageIterator implements MessageIteratorInterface
20
{
21
    /**
22
     * @var MessageManagerInterface
23
     */
24
    protected $messageManager;
25
26
    /**
27
     * @var int
28
     */
29
    protected $counter;
30
31
    /**
32
     * @var mixed
33
     */
34
    protected $current;
35
36
    /**
37
     * @var array
38
     */
39
    protected $types;
40
41
    /**
42
     * @var int
43
     */
44
    protected $batchSize;
45
46
    /**
47
     * @var array
48
     */
49
    protected $buffer = [];
50
51
    /**
52
     * @param array $types
53
     * @param int   $pause
54
     * @param int   $batchSize
55
     */
56
    public function __construct(MessageManagerInterface $messageManager, $types = [], $pause = 500000, $batchSize = 10)
57
    {
58
        $this->messageManager = $messageManager;
59
        $this->counter = 0;
60
        $this->pause = $pause;
0 ignored issues
show
Bug introduced by
The property pause does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
        $this->types = $types;
62
        $this->batchSize = $batchSize;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function current()
69
    {
70
        return $this->current;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function next(): void
77
    {
78
        $this->setCurrent();
79
        ++$this->counter;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function key()
86
    {
87
        return $this->counter;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function valid()
94
    {
95
        return true;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function rewind(): void
102
    {
103
        $this->setCurrent();
104
    }
105
106
    /**
107
     * Return true if the internal buffer is empty.
108
     *
109
     * @return bool
110
     */
111
    public function isBufferEmpty()
112
    {
113
        return 0 === \count($this->buffer);
114
    }
115
116
    /**
117
     * Assign current pointer a message.
118
     */
119
    protected function setCurrent(): void
120
    {
121
        if (0 === \count($this->buffer)) {
122
            $this->bufferize($this->types);
123
        }
124
125
        $this->current = array_shift($this->buffer);
126
    }
127
128
    /**
129
     * Fill the inner messages buffer.
130
     *
131
     * @param array $types
132
     */
133
    protected function bufferize($types = []): void
134
    {
135
        while (true) {
136
            $this->buffer = $this->findNextMessages($types);
137
138
            if (\count($this->buffer) > 0) {
139
                break;
140
            }
141
142
            usleep($this->pause);
143
        }
144
    }
145
146
    /**
147
     * Find open messages.
148
     *
149
     * @param array $types
150
     *
151
     * @return mixed
152
     */
153
    protected function findNextMessages($types)
154
    {
155
        return $this->messageManager->findByTypes($types, MessageInterface::STATE_OPEN, $this->batchSize);
156
    }
157
}
158