Completed
Pull Request — master (#27)
by Frederic
06:01
created

QueueHandlingDaemon::setClock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Burrow\Daemon;
4
5
use Burrow\Clock;
6
use Burrow\ConsumeOptions;
7
use Burrow\Event\DaemonStarted;
8
use Burrow\Event\DaemonStopped;
9
use Burrow\Event\MessageConsumed;
10
use Burrow\Event\MessageReceived;
11
use Burrow\Event\NullEmitter;
12
use Burrow\RealClock;
13
use Evaneos\Daemon\Daemon;
14
use Evaneos\Daemon\DaemonMonitor;
15
use Burrow\Driver;
16
use Burrow\Message;
17
use Evaneos\Daemon\Monitor\NullMonitor;
18
use Burrow\QueueHandler;
19
use League\Event\Emitter;
20
use League\Event\EmitterInterface;
21
use Psr\Log\LoggerAwareInterface;
22
use Psr\Log\LoggerAwareTrait;
23
use Psr\Log\NullLogger;
24
25
class QueueHandlingDaemon implements Daemon, LoggerAwareInterface
26
{
27
    use LoggerAwareTrait;
28
    
29
    /** @var Driver */
30
    private $driver;
31
32
    /** @var QueueHandler */
33
    private $handler;
34
35
    /** @var string */
36
    private $queueName;
37
38
    /** @var DaemonMonitor */
39 6
    private $monitor;
40
41 6
    /** @var Emitter */
42 6
    private $eventEmitter;
43 6
44
    /** @var Clock */
45 6
    private $clock;
46 6
47 6
    /**
48
     * Handler constructor.
49
     *
50
     * @param Driver                $driver
51
     * @param QueueHandler          $handler
52
     * @param string                $queueName
53
     * @param EmitterInterface|null $eventEmitter
54 6
     */
55
    public function __construct(
56 6
        Driver $driver,
57
        QueueHandler $handler,
58 6
        $queueName,
59
        EmitterInterface $eventEmitter = null
60 6
    ) {
61 6
        $this->driver = $driver;
62 6
        $this->handler = $handler;
63 6
        $this->queueName = $queueName;
64 6
65
        $this->monitor = new NullMonitor();
66 6
        $this->logger = new NullLogger();
67
        $this->eventEmitter = $eventEmitter ?: new NullEmitter();
0 ignored issues
show
Documentation Bug introduced by
$eventEmitter ?: new \Burrow\Event\NullEmitter() is of type object<League\Event\EmitterInterface>, but the property $eventEmitter was declared to be of type object<League\Event\Emitter>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
68 6
        $this->clock = new RealClock();
69 6
    }
70 6
71 6
    /**
72 4
     * Run the daemon
73
     *
74 6
     * @return void
75 6
     */
76
    public function start()
77
    {
78
        $startTime = $this->clock->timestampInMs();
79
80
        $this->eventEmitter->emit(new DaemonStarted());
81
82 6
        $this->logger->info('Starting daemon...');
83
84 6
        $options = $this->handler->options(new ConsumeOptions());
85
86 6
        $this->driver->consume(
87 6
            $this->queueName,
88
            function (Message $message) use ($startTime) {
89
                $this->eventEmitter->emit(new MessageReceived());
90
                $this->monitor->monitor($this, $message);
91
92
                $result = $this->handler->handle($message);
93
94 3
                $this->eventEmitter->emit(new MessageConsumed($this->clock->timestampInMs() - $startTime));
95
                pcntl_signal_dispatch();
96 3
97 3
                return $result;
98
            },
99
            $options->getTimeout(),
100
            $options->isAutoAck()
101
        );
102
103
        $this->stop();
104
        $this->eventEmitter->emit(new DaemonStopped());
105
    }
106
107
    /**
108
     * Stop the daemon
109
     *
110
     * @return void
111
     */
112
    public function stop()
113
    {
114
        $this->logger->info('Closing daemon...');
115
116
        $this->driver->close();
117
    }
118
119
    /**
120
     * Set a monitor.
121
     *
122
     * @param DaemonMonitor $monitor
123
     */
124
    public function setMonitor(DaemonMonitor $monitor)
125
    {
126
        $this->monitor = $monitor;
127
    }
128
129
    /**
130
     * @param Clock $clock
131
     */
132
    public function setClock(Clock $clock)
133
    {
134
        $this->clock = $clock;
135
    }
136
}
137