Completed
Push — master ( 0b4c0a...f06a3d )
by Adam
14:19
created

Worker::invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Equip\Queue;
4
5
use Equip\Queue\Driver\DriverInterface;
6
use Exception;
7
use League\Tactician\CommandBus;
8
9
class Worker
10
{
11
    /**
12
     * @var DriverInterface
13
     */
14
    private $driver;
15
16
    /**
17
     * @var Event
18
     */
19
    private $event;
20
21
    /**
22
     * @var CommandBus
23
     */
24
    private $command_bus;
25
26 3
    public function __construct(
27
        DriverInterface $driver,
28
        Event $event,
29
        CommandBus $command_bus
30
    ) {
31 3
        $this->driver = $driver;
32 3
        $this->event = $event;
33 3
        $this->command_bus = $command_bus;
34 3
    }
35
36
    /**
37
     * Consumes messages off of the queue
38
     *
39
     * @codeCoverageIgnore
40
     *
41
     * @param string $queue
42
     */
43
    public function consume($queue)
44
    {
45
        while ($this->tick($queue)) { /* NOOP */ }
46
    }
47
48
    /**
49
     * Handles fetching messages from the queue
50
     *
51
     * @param string $queue
52
     *
53
     * @return bool
54
     */
55 3
    protected function tick($queue)
56
    {
57 3
        $message = $this->driver->dequeue($queue);
58 3
        if (empty($message)) {
59 1
            return true;
60
        }
61
62 2
        $command = unserialize($message);
63
        try {
64 2
            $this->event->acknowledge($command);
65 2
            $this->command_bus->handle($command);
66 1
            $this->event->finish($command);
67 2
        } catch (Exception $exception) {
68 1
            $this->event->reject($command, $exception);
69
        }
70
71 2
        return true;
72
    }
73
}
74