WorkerBuilder::usingDriverClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Workana\AsyncJobs;
3
4
use Bernard\Queue\RoundRobinQueue;
5
use Workana\AsyncJobs\Process\ProcessManager;
6
7
/**
8
 * Worker builder
9
 *
10
 * @author Carlos Frutos <[email protected]>
11
 */
12
class WorkerBuilder
13
{
14
    /**
15
     * @var JobManager
16
     */
17
    protected $jm;
18
19
    /**
20
     * Creates a new Worker builder
21
     *
22
     * @param JobManager $jm
23
     */
24
    public function __construct(JobManager $jm)
25
    {
26
        $this->jm = $jm;
27
    }
28
29
    /**
30
     * @var string[]
31
     */
32
    protected $queueNames = [];
33
34
    /**
35
     * Using queue
36
     *
37
     * @param string $queueName
38
     *
39
     * @return self
40
     */
41
    public function usingQueue($queueName)
42
    {
43
        $this->queueNames[] = (string) $queueName;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Using multiple queues
50
     *
51
     * @param string[] $queueNames
52
     *
53
     * @return self
54
     */
55
    public function usingMultipleQueues(array $queueNames)
56
    {
57
        $this->queueNames = array_merge(array_filter($queueNames), $this->queueNames);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge(array_filter...es), $this->queueNames) of type array is incompatible with the declared type array<integer,string> of property $queueNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
59
        return $this;
60
    }
61
62
    /**
63
     * Using Driver class
64
     *
65
     * @param string $driverClass
66
     *
67
     * @return self
68
     */
69
    public function usingDriverClass($driverClass)
70
    {
71
        $this->driverClass = (string) $driverClass;
0 ignored issues
show
Bug introduced by
The property driverClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72
73
        return $this;
74
    }
75
76
    /**
77
     * Build worker
78
     *
79
     * @return Worker
80
     */
81
    public function build()
82
    {
83
        $queue = $this->getQueue();
84
        $retryStrategy = $this->getRetryStrategy();
85
86
        return new Worker(
87
            $queue,
88
            $this->jm->getRouter(),
89
            $this->jm->getEventDispatcher(),
90
            new ProcessManager(),
91
            $retryStrategy
92
        );
93
    }
94
95
    /**
96
     * @return \Bernard\Queue
97
     */
98
    protected function getQueue()
99
    {
100
        if (!empty($this->queueNames)) {
101
            $queueNames = $this->queueNames;
102
        } else {
103
            $queueNames = $this->jm->getDriver()->listQueues();
104
        }
105
106
        $qtyQueues = count($queueNames);
107
108
        if ($qtyQueues === 1) {
109
            $queue = $this->jm->getQueueFactory()->create(reset($queueNames));
110
        } else {
111
            $queueSet = array_map([$this->jm->getQueueFactory(), 'create'], $queueNames);
112
            $queue = new RoundRobinQueue($queueSet);
113
        }
114
115
        return $queue;
116
    }
117
118
    /**
119
     * @return Retry\RetryStrategy
120
     */
121
    protected function getRetryStrategy()
122
    {
123
        $retryStrategy = $this->jm->getContainer()->get($this->jm->getConfiguration()->getRetryStrategyClass());
124
        $retryStrategy->setJobManager($this->jm);
125
126
        return $retryStrategy;
127
    }
128
}