Multiple::getQueue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * @author    Flipbox Factory
5
 * @copyright Copyright (c) 2017, Flipbox Digital
6
 * @link      https://github.com/flipbox/queue/releases/latest
7
 * @license   https://github.com/flipbox/queue/blob/master/LICENSE
8
 */
9
10
namespace flipbox\queue\queues;
11
12
use flipbox\queue\jobs\JobInterface;
13
use flipbox\queue\strategies\Random;
14
use flipbox\queue\strategies\StrategyInterface;
15
use Craft;
16
use yii\helpers\ArrayHelper;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class Multiple extends AbstractQueue implements MultipleQueueInterface
23
{
24
    /**
25
     * @var QueueInterface[]
26
     */
27
    public $queues = [];
28
29
    /**
30
     * @var StrategyInterface
31
     */
32
    public $strategy = ['class' => Random::class];
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function init()
38
    {
39
        parent::init();
40
41
        // Add queues
42
        foreach ($this->queues as $id => $queue) {
43
            $this->queues[$id] = Craft::createObject($queue);
44
        }
45
46
        if (!$this->strategy instanceof StrategyInterface) {
47
            $this->strategy = Craft::createObject($this->strategy);
48
        }
49
        $this->strategy->setQueue($this);
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function getQueues()
56
    {
57
        return $this->queues;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function getQueue($index)
64
    {
65
        if ($index === null) {
66
            return reset($this->queues);
67
        }
68
        return ArrayHelper::getValue($this->queues, $index);
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    protected function deleteJob(JobInterface $job): bool
75
    {
76
        return $this->strategy->delete($job);
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    protected function fetchJob()
83
    {
84
        return $this->strategy->fetch();
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    protected function postJob(JobInterface $job, array $options = []): bool
91
    {
92
        reset($this->queues);
93
        return $this->postToQueue($job, key($this->queues));
94
    }
95
96
    /**
97
     * Post new job to a specific queue.
98
     * @param JobInterface $job
99
     * @param integer $index
100
     * @return bool
101
     */
102
    public function postToQueue(JobInterface $job, $index): bool
103
    {
104
        $queue = $this->getQueue($index);
105
        if ($queue === null) {
106
            return false;
107
        }
108
        return $queue->post($job);
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    protected function releaseJob(JobInterface $job): bool
115
    {
116
        $queue = $this->getQueue(
117
            $job->getHeader(StrategyInterface::HEADER_MULTIPLE_QUEUE_INDEX)
118
        );
119
        return $queue->release($job);
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    public function getSize(): int
126
    {
127
        return array_sum(array_map(function (QueueInterface $queue) {
128
            return $queue->getSize();
129
        }, $this->queues));
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function purge(): bool
136
    {
137
        foreach ($this->queues as $queue) {
138
            $queue->purge();
139
        }
140
        return true;
141
    }
142
}
143