|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\NotificationBundle\Iterator; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
|
15
|
|
|
use Sonata\NotificationBundle\Model\MessageManagerInterface; |
|
16
|
|
|
|
|
17
|
|
|
class MessageManagerMessageIterator implements MessageIteratorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var MessageManagerInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $messageManager; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $counter; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $current; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $types; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $batchSize; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $buffer = array(); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param MessageManagerInterface $messageManager |
|
51
|
|
|
* @param array $types |
|
52
|
|
|
* @param int $pause |
|
53
|
|
|
* @param int $batchSize |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(MessageManagerInterface $messageManager, $types = array(), $pause = 500000, $batchSize = 10) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->messageManager = $messageManager; |
|
58
|
|
|
$this->counter = 0; |
|
59
|
|
|
$this->pause = $pause; |
|
|
|
|
|
|
60
|
|
|
$this->types = $types; |
|
61
|
|
|
$this->batchSize = $batchSize; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function current() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->current; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function next() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->setCurrent(); |
|
78
|
|
|
++$this->counter; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function key() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->counter; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function valid() |
|
93
|
|
|
{ |
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
public function rewind() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->setCurrent(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return true if the internal buffer is empty. |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function isBufferEmpty() |
|
111
|
|
|
{ |
|
112
|
|
|
return 0 === count($this->buffer); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Assign current pointer a message. |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function setCurrent() |
|
119
|
|
|
{ |
|
120
|
|
|
if (count($this->buffer) === 0) { |
|
121
|
|
|
$this->bufferize($this->types); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$this->current = array_pop($this->buffer); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Fill the inner messages buffer. |
|
129
|
|
|
* |
|
130
|
|
|
* @param array $types |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function bufferize($types = array()) |
|
133
|
|
|
{ |
|
134
|
|
|
while (true) { |
|
135
|
|
|
$this->buffer = $this->findNextMessages($types); |
|
136
|
|
|
|
|
137
|
|
|
if (count($this->buffer) > 0) { |
|
138
|
|
|
break; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
usleep($this->pause); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Find open messages. |
|
147
|
|
|
* |
|
148
|
|
|
* @param array $types |
|
149
|
|
|
* |
|
150
|
|
|
* @return mixed |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function findNextMessages($types) |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->messageManager->findByTypes($types, MessageInterface::STATE_OPEN, $this->batchSize); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: