ProcessMessages   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 1
b 0
f 0
dl 0
loc 20
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 3
1
<?php
2
3
require '../vendor/autoload.php';
4
5
use LeoCarmo\RedisQueue\Listener;
6
7
$redis = new Redis();
8
$redis->connect('localhost');
9
10
//Listener::setDefaultQueueClient($redis);
11
Listener::setQueueClient('my-queue', $redis);
12
13
class ProcessMessages
14
{
15
16
    /**
17
     * @param $events
18
     * @throws Exception
19
     */
20
    public function __invoke($events)
21
    {
22
        foreach ($events as $event) {
23
            $event_decoded = json_decode($event);
24
25
            if ($event_decoded->something === 1) {
26
                // IMPORTANT: This exception on multiple events,
27
                // will send all events received by this worker to dead queue.
28
                // Use transactions or bulk inserts when using multiple events
29
                throw new \Exception('Fail, send to dead queue');
30
            }
31
32
            dump($event);
33
        }
34
    }
35
}
36
37
// If the worker crash, we need to restore the processing messages
38
Listener::restoreMessagesFromProcessingQueue('my-queue', 1);
39
40
$processor = new ProcessMessages();
41
42
while (true) {
43
    Listener::processMessages('my-queue', 1, 10, $processor);
44
}