Passed
Push — v2 ( c4de3c...f057b3 )
by Brice
03:43
created

Worker::consume()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0072

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 13
cp 0.9231
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 1
crap 4.0072
1
<?php
2
3
namespace JobQueue\Domain\Worker;
4
5
use JobQueue\Domain\Task\Profile;
6
use JobQueue\Domain\Task\Queue;
7
use JobQueue\Domain\Task\TaskHandler;
8
use JobQueue\Domain\Task\TaskWasFetched;
9
use JobQueue\Domain\Task\WorkerHasFinished;
10
use JobQueue\Domain\Task\WorkerWasStarted;
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\LoggerAwareTrait;
13
use Symfony\Component\EventDispatcher\EventDispatcher;
14
15
final class Worker implements LoggerAwareInterface
16
{
17
    use LoggerAwareTrait;
18
19
    /**
20
     *
21
     * @var string
22
     */
23
    private $name;
24
25
    /**
26
     *
27
     * @var Queue
28
     */
29
    private $queue;
30
31
    /**
32
     *
33
     * @var Profile
34
     */
35
    private $profile;
36
37
    /**
38
     *
39
     * @var EventDispatcher
40
     */
41
    protected $dispatcher;
42
43
    /**
44
     *
45
     * @param string          $name
46
     * @param Queue           $queue
47
     * @param Profile         $profile
48
     * @param EventDispatcher $dispatcher
49
     */
50 3
    public function __construct(string $name, Queue $queue, Profile $profile, EventDispatcher $dispatcher)
51
    {
52 3
        $this->name = $name;
53 3
        $this->queue = $queue;
54 3
        $this->profile = $profile;
55 3
        $this->dispatcher = $dispatcher;
56 3
    }
57
58
    /**
59
     *
60
     * @return string
61
     */
62
    public function getName(): string
63
    {
64
        return $this->name;
65
    }
66
67
    /**
68
     *
69
     * @return Profile
70
     */
71
    public function getProfile(): Profile
72
    {
73
        return $this->profile;
74
    }
75
76
    /**
77
     *
78
     * @param int|null $quantity Number of tasks to handle
79
     */
80 4
    public function consume(int $quantity = null)
81
    {
82 4
        $this->dispatcher->dispatch(WorkerWasStarted::NAME, new WorkerWasStarted($this));
83
84
        // Set up the Task handler which subscribe to tasks domain events
85 4
        $taskHandler = new TaskHandler($this->queue, $this->dispatcher);
86 4
        if ($this->logger) {
87
            $taskHandler->setLogger($this->logger);
88
        }
89 4
        $this->dispatcher->addSubscriber($taskHandler);
90
91 4
        $i = 0;
92 4
        while ($task = $this->queue->fetch($this->profile)) {
93 4
            $this->dispatcher->dispatch(TaskWasFetched::NAME, new TaskWasFetched($task, $this->queue));
0 ignored issues
show
Unused Code introduced by
The call to JobQueue\Domain\Task\TaskWasFetched::__construct() has too many arguments starting with $this->queue. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
            $this->dispatcher->dispatch(TaskWasFetched::NAME, /** @scrutinizer ignore-call */ new TaskWasFetched($task, $this->queue));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
94
95
            // Exit worker if a quantity has been set
96 4
            $i++;
97 4
            if ($quantity === $i) {
98 4
                break;
99
            }
100
        }
101
102 4
        $this->dispatcher->dispatch(WorkerHasFinished::NAME, new WorkerHasFinished($this));
103 4
    }
104
}
105