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\Backend; |
15
|
|
|
|
16
|
|
|
use Laminas\Diagnostics\Result\Failure; |
17
|
|
|
use Laminas\Diagnostics\Result\Success; |
18
|
|
|
use Laminas\Diagnostics\Result\Warning; |
19
|
|
|
use Sonata\NotificationBundle\Consumer\ConsumerEvent; |
20
|
|
|
use Sonata\NotificationBundle\Exception\HandlingException; |
21
|
|
|
use Sonata\NotificationBundle\Iterator\MessageManagerMessageIterator; |
22
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
23
|
|
|
use Sonata\NotificationBundle\Model\MessageManagerInterface; |
24
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
25
|
|
|
|
26
|
|
|
class MessageManagerBackend implements BackendInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var MessageManagerInterface |
30
|
|
|
*/ |
31
|
|
|
protected $messageManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $checkLevel; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $pause; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
protected $maxAge; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var MessageManagerBackendDispatcher|null |
50
|
|
|
*/ |
51
|
|
|
protected $dispatcher = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $types; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
protected $batchSize; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param int $pause |
65
|
|
|
* @param int $maxAge |
66
|
|
|
* @param int $batchSize |
67
|
|
|
*/ |
68
|
|
|
public function __construct(MessageManagerInterface $messageManager, array $checkLevel, $pause = 500000, $maxAge = 86400, $batchSize = 10, array $types = []) |
69
|
|
|
{ |
70
|
|
|
$this->messageManager = $messageManager; |
71
|
|
|
$this->checkLevel = $checkLevel; |
72
|
|
|
$this->pause = $pause; |
73
|
|
|
$this->maxAge = $maxAge; |
74
|
|
|
$this->batchSize = $batchSize; |
75
|
|
|
$this->types = $types; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $types |
80
|
|
|
*/ |
81
|
|
|
public function setTypes($types): void |
82
|
|
|
{ |
83
|
|
|
$this->types = $types; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function publish(MessageInterface $message) |
90
|
|
|
{ |
91
|
|
|
$this->messageManager->save($message); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return $message; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function create($type, array $body) |
100
|
|
|
{ |
101
|
|
|
$message = $this->messageManager->create(); |
102
|
|
|
$message->setType($type); |
103
|
|
|
$message->setBody($body); |
104
|
|
|
$message->setState(MessageInterface::STATE_OPEN); |
105
|
|
|
|
106
|
|
|
return $message; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function createAndPublish($type, array $body) |
113
|
|
|
{ |
114
|
|
|
return $this->publish($this->create($type, $body)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function getIterator() |
121
|
|
|
{ |
122
|
|
|
return new MessageManagerMessageIterator($this->messageManager, $this->types, $this->pause, $this->batchSize); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function initialize(): void |
129
|
|
|
{ |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function setDispatcher(MessageManagerBackendDispatcher $dispatcher): void |
133
|
|
|
{ |
134
|
|
|
$this->dispatcher = $dispatcher; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function handle(MessageInterface $message, EventDispatcherInterface $dispatcher) |
141
|
|
|
{ |
142
|
|
|
$event = new ConsumerEvent($message); |
143
|
|
|
|
144
|
|
|
try { |
145
|
|
|
$message->setStartedAt(new \DateTime()); |
146
|
|
|
$message->setState(MessageInterface::STATE_IN_PROGRESS); |
147
|
|
|
$this->messageManager->save($message); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$dispatcher->dispatch($event, $message->getType()); |
150
|
|
|
|
151
|
|
|
$message->setCompletedAt(new \DateTime()); |
152
|
|
|
$message->setState(MessageInterface::STATE_DONE); |
153
|
|
|
$this->messageManager->save($message); |
|
|
|
|
154
|
|
|
|
155
|
|
|
return $event->getReturnInfo(); |
156
|
|
|
} catch (\Exception $e) { |
157
|
|
|
$message->setCompletedAt(new \DateTime()); |
158
|
|
|
$message->setState(MessageInterface::STATE_ERROR); |
159
|
|
|
|
160
|
|
|
$this->messageManager->save($message); |
|
|
|
|
161
|
|
|
|
162
|
|
|
throw new HandlingException('Error while handling a message', 0, $e); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function getStatus() |
170
|
|
|
{ |
171
|
|
|
try { |
172
|
|
|
$states = $this->messageManager->countStates(); |
173
|
|
|
} catch (\Exception $e) { |
174
|
|
|
return new Failure(sprintf('Unable to retrieve message information - %s (Database)', $e->getMessage())); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($states[MessageInterface::STATE_IN_PROGRESS] > $this->checkLevel[MessageInterface::STATE_IN_PROGRESS]) { |
178
|
|
|
return new Failure('Too many messages processed at the same time (Database)'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($states[MessageInterface::STATE_ERROR] > $this->checkLevel[MessageInterface::STATE_ERROR]) { |
182
|
|
|
return new Failure('Too many errors (Database)'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($states[MessageInterface::STATE_OPEN] > $this->checkLevel[MessageInterface::STATE_OPEN]) { |
186
|
|
|
return new Warning('Too many messages waiting to be processed (Database)'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($states[MessageInterface::STATE_DONE] > $this->checkLevel[MessageInterface::STATE_DONE]) { |
190
|
|
|
return new Warning('Too many processed messages, please clean the database (Database)'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return new Success('Ok (Database)'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function cleanup(): void |
200
|
|
|
{ |
201
|
|
|
$this->messageManager->cleanup($this->maxAge); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: