AbstractMessage::getRetryTimeout()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 4
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * AppserverIo\Messaging\AbstractMessage
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/messaging
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Messaging;
22
23
use AppserverIo\Psr\Pms\QueueInterface;
24
use AppserverIo\Psr\Pms\MessageInterface;
25
use AppserverIo\Psr\Pms\MonitorInterface;
26
use AppserverIo\Psr\Pms\StateKeyInterface;
27
use AppserverIo\Psr\Pms\PriorityKeyInterface;
28
use AppserverIo\Messaging\Utils\PriorityKeys;
29
use AppserverIo\Messaging\Utils\PriorityLow;
30
use AppserverIo\Messaging\Utils\StateKeys;
31
use AppserverIo\Messaging\Utils\StateActive;
32
use AppserverIo\Messaging\Utils\StateFailed;
33
use AppserverIo\Messaging\Utils\StateProcessed;
34
use AppserverIo\Messaging\Utils\StatePaused;
35
use AppserverIo\Lang\Reflection\ReflectionObject;
36
37
/**
38
 * The abstract superclass for all messages.
39
 *
40
 * @author    Tim Wagner <[email protected]>
41
 * @copyright 2015 TechDivision GmbH <[email protected]>
42
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
43
 * @link      https://github.com/appserver-io/messaging
44
 * @link      http://www.appserver.io
45
 */
46
abstract class AbstractMessage implements MessageInterface, \Serializable
47
{
48
49
    /**
50
     * The unique session-ID.
51
     *
52
     * @var string
53
     */
54
    protected $sessionId = null;
55
56
    /**
57
     * The destination queue to send the message to.
58
     *
59
     * @var \AppserverIo\Psr\Pms\QueueInterface
60
     */
61
    protected $destination = null;
62
63
    /**
64
     * The parent message.
65
     *
66
     * @var \AppserverIo\Psr\Pms\MessageInterface
67
     */
68
    protected $parentMessage = null;
69
70
    /**
71
     * The monitor for monitoring the message.
72
     *
73
     * @var \AppserverIo\Psr\Pms\MonitorInterface
74
     */
75
    protected $messageMonitor = null;
76
77
    /**
78
     * The priority of the message, defaults to 'low'.
79
     *
80
     * @var integer
81
     */
82
    protected $priority = null;
83
84
    /**
85
     * The state of the message, defaults to 'active'.
86
     *
87
     * @var integer
88
     */
89
    protected $state = null;
90
91
    /**
92
     * The flag if the message should be deleted when finished or not.
93
     *
94
     * @var boolean
95
     */
96
    protected $locked = null;
97
98
    /**
99
     * The array with the timeouts for the retries.
100
     *
101
     * @var array
102
     */
103
    protected $retryTimeouts = array();
104
105
    /**
106
     * The array with the callbacks.
107
     *
108
     * @var array
109
     */
110
    protected $callbacks = array();
111
112
    /**
113
     * Initializes the message with the array
114
     * to send to the queue.
115
     */
116 1
    public function __construct()
117
    {
118
        // initialize the default values
119 1
        $this->sessionId = "";
120 1
        $this->priority = PriorityLow::KEY;
121 1
        $this->state = StateActive::KEY;
122 1
        $this->locked = false;
123 1
    }
124
125
    /**
126
     * Sets the unique session id.
127
     *
128
     * @param string $sessionId The uniquid id
129
     *
130
     * @return void
131
     */
132
    public function setSessionId($sessionId)
133
    {
134
        $this->sessionId = $sessionId;
135
    }
136
137
    /**
138
     * Returns the unique session id.
139
     *
140
     * @return string The uniquid id
141
     */
142
    public function getSessionId()
143
    {
144
        return $this->sessionId;
145
    }
146
147
    /**
148
     * Sets the destination queue.
149
     *
150
     * @param \AppserverIo\Psr\Pms\QueueInterface $destination The destination queue
151
     *
152
     * @return void
153
     */
154
    public function setDestination(QueueInterface $destination)
155
    {
156
        $this->destination = $destination;
157
    }
158
159
    /**
160
     * Returns the destination queue.
161
     *
162
     * @return \AppserverIo\Psr\Pms\QueueInterface The destination queue
163
     */
164
    public function getDestination()
165
    {
166
        return $this->destination;
167
    }
168
169
    /**
170
     * Sets the priority of the message.
171
     *
172
     * @param \AppserverIo\Psr\Pms\PriorityKeyInterface $priority The priority to set the message to
173
     *
174
     * @return void
175
     */
176
    public function setPriority(PriorityKeyInterface $priority)
177
    {
178
        $this->priority = $priority->getPriority();
179
    }
180
181
    /**
182
     * Returns the priority of the message.
183
     *
184
     * @return \AppserverIo\Psr\Pms\PriorityKeyInterface The priority of the message
185
     */
186
    public function getPriority()
187
    {
188
        return PriorityKeys::get($this->priority);
189
    }
190
191
    /**
192
     * Sets the state of the message.
193
     *
194
     * @param \AppserverIo\Psr\Pms\StateKeyInterface $state The new state
195
     *
196
     * @return void
197
     */
198
    public function setState(StateKeyInterface $state)
199
    {
200
        $this->state = $state->getState();
201
    }
202
203
    /**
204
     * Returns the state of the message.
205
     *
206
     * @return \AppserverIo\Psr\Pms\StateKeyInterface The message state
207
     */
208
    public function getState()
209
    {
210
        return StateKeys::get($this->state);
211
    }
212
213
    /**
214
     * Sets the parent message.
215
     *
216
     * @param \AppserverIo\Psr\Pms\MessageInterface $parentMessage The parent message
217
     *
218
     * @return void
219
     */
220
    public function setParentMessage(MessageInterface $parentMessage)
221
    {
222
        $this->parentMessage = $parentMessage;
223
    }
224
225
    /**
226
     * Returns the parent message.
227
     *
228
     * @return \AppserverIo\Psr\Pms\MessageInterface The parent message
229
     *
230
     * @see \AppserverIo\Psr\Pms\MessageInterface::getParentMessage()
231
     */
232
    public function getParentMessage()
233
    {
234
        return $this->parentMessage;
235
    }
236
237
    /**
238
     * Sets the monitor for monitoring the message itself.
239
     *
240
     * @param \AppserverIo\Psr\Pms\MonitorInterface $messageMonitor The monitor
241
     *
242
     * @return void
243
     */
244
    public function setMessageMonitor(MonitorInterface $messageMonitor)
245
    {
246
        $this->messageMonitor = $messageMonitor;
247
    }
248
249
    /**
250
     * Returns the message monitor.
251
     *
252
     * @return \AppserverIo\Psr\Pms\MonitorInterface The monitor
253
     */
254
    public function getMessageMonitor()
255
    {
256
        return $this->messageMonitor;
257
    }
258
259
    /**
260
     * Locks the message.
261
     *
262
     * @return void
263
     */
264
    public function lock()
265
    {
266
        $this->locked = true;
267
    }
268
269
    /**
270
     * Unlocks the message.
271
     *
272
     * @return void
273
     */
274
    public function unlock()
275
    {
276
        $this->locked = false;
277
    }
278
279
    /**
280
     * Returns the message lock flag.
281
     *
282
     * @return boolean TRUE if the message is locked, else FALSE
283
     */
284
    public function isLocked()
285
    {
286
        return $this->locked;
287
    }
288
289
    /**
290
     * Serializes the message and returns the serialized representation.
291
      *
292
     * @return string the string representation of the object or null
293
     * @link http://php.net/manual/en/serializable.serialize.php
294
     */
295 1
    public function serialize()
296
    {
297 1
        return serialize(get_object_vars($this));
298
    }
299
300
    /**
301
     * The serialized representation of the message.
302
     *
303
     * @param string $data The string representation of the object
304
     *
305
     * @return void
306
     * @link http://php.net/manual/en/serializable.unserialize.php
307
     */
308 1
    public function unserialize($data)
309
    {
310 1
        foreach (unserialize($data) as $propertyName => $propertyValue) {
311 1
            $this->$propertyName = $propertyValue;
312
        }
313 1
    }
314
315
    /**
316
     * Set's the array with the retry timeouts which is also responsible
317
     * for the the number of retries.
318
     *
319
     * @param array $retryTimeouts The number of retries with their timeouts
320
     *
321
     * @return void
322
     */
323
    public function setRetryTimeouts(array $retryTimeouts)
324
    {
325
        $this->retryTimeouts = $retryTimeouts;
326
    }
327
328
    /**
329
     * Return's the array with the retry timeouts.
330
     *
331
     * @return array The retry timeouts
332
     */
333
    public function getRetryTimeouts()
334
    {
335
        return $this->retryTimeouts;
336
    }
337
338
    /**
339
     * Return's the timeout for the given retry.
340
     *
341
     * @param integer $retry The retry to return the timeout for
342
     *
343
     * @return integer The timeout in seconds for the passed retry
344
     * @throws \InvalidArgumentException Is thrown if the timeout for the passed retry is NOT available
345
     */
346
    public function getRetryTimeout($retry)
347
    {
348
349
        // try to find the timeout for the passed retry
350
        if (isset($this->retryTimeouts[$retry])) {
351
            return $this->retryTimeouts[$retry];
352
        }
353
354
        // throw an exception if the timeout is NOT available
355
        throw new \InvalidArgumentException(sprintf('Can\t find timeout information for retry %d', $retry));
356
    }
357
358
    /**
359
     * Return's the number of retries for this message.
360
     *
361
     * @return integer The number of retries
362
     */
363
    public function getRetryCounter()
364
    {
365
        return sizeof($this->getRetryTimeouts());
366
    }
367
368
    /**
369
     * Add's the callback for the given state.
370
     *
371
     * @param \AppserverIo\Psr\Pms\StateKeyInterface $state    The state to register the callback for
372
     * @param array                                  $callback The array with the bean's lookup and method name that has to be invoked
373
     *
374
     * @return void
375
     * @throws \Exception Is thrown if the passed state doesn't support callbacks
376
     */
377
    public function addCallback(StateKeyInterface $state, array $callback)
378
    {
379
380
        // query whether or not the state supports callbacks
381
        if (in_array($state->getState(), array(StateFailed::KEY, StateProcessed::KEY, StatePaused::KEY))) {
382
            // initialize the array with the state callbacks, if not already done
383
            if (!isset($this->callbacks[$state->getState()])) {
384
                $this->callbacks[$state->getState()] = array();
385
            }
386
387
            // add the callback to the state
388
            array_push($this->callbacks[$state->getState()], $callback);
389
390
            // return immediately
391
            return;
392
        }
393
394
        // throw an exception, if the passed state doesn't support callbacks
395
        throw new \Exception(
396
            sprintf(
397
                'Callbacks for state %s is not supported, state has to be either one of StateFailed, StateProcessed or StatePaused',
398
                (new ReflectionObject($state))->getShortName()
399
            )
400
        );
401
    }
402
403
    /**
404
     * Return's the callback information for the given state.
405
     *
406
     * @param \AppserverIo\Psr\Pms\StateKeyInterface $state The state to register the callback for
407
     *
408
     * @return array The array with the callback information
409
     */
410
    public function getCallbacks(StateKeyInterface $state)
411
    {
412
413
        // initialize the array with the callbacks
414
        $callbacks = array();
415
416
        // try to find the callback for the passed state
417
        if (isset($this->callbacks[$state->getState()])) {
418
            $callbacks = $this->callbacks[$state->getState()];
419
        }
420
421
        // return the array with the callbacks
422
        return $callbacks;
423
    }
424
425
    /**
426
     * Return's whether or not callbacks for the passed state has been registered.
427
     *
428
     * @param \AppserverIo\Psr\Pms\StateKeyInterface $state The state to register the callback for
429
     *
430
     * @return boolean TRUE if callbacks has been registered, else FALSE
431
     */
432
    public function hasCallbacks(StateKeyInterface $state)
433
    {
434
        return isset($this->callbacks[$state->getState()]);
435
    }
436
}
437