Completed
Push — master ( 38797d...534d41 )
by Nate
03:48
created

Multiple::getSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
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 Yii;
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] = Yii::createObject($queue);
44
        }
45
46
        if (!$this->strategy instanceof StrategyInterface) {
47
            $this->strategy = Yii::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
        return ArrayHelper::getValue($this->queues, $index);
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    protected function deleteJob(JobInterface $job): bool
72
    {
73
        return $this->strategy->delete($job);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    protected function fetchJob()
80
    {
81
        return $this->strategy->fetch();
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    protected function postJob(JobInterface $job): bool
88
    {
89
        reset($this->queues);
90
        return $this->postToQueue($job, key($this->queues));
91
    }
92
93
    /**
94
     * Post new job to a specific queue.
95
     * @param JobInterface $job
96
     * @param integer $index
97
     * @return bool
98
     */
99
    public function postToQueue(JobInterface $job, $index): bool
100
    {
101
        $queue = $this->getQueue($index);
102
        if ($queue === null) {
103
            return false;
104
        }
105
        return $queue->post($job);
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    protected function releaseJob(JobInterface $job): bool
112
    {
113
        $queue = $this->getQueue(
114
            $job->getHeader(StrategyInterface::HEADER_MULTIPLE_QUEUE_INDEX)
115
        );
116
        return $queue->release($job);
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122
    public function getSize(): int
123
    {
124
        return array_sum(array_map(function (QueueInterface $queue) {
125
            return $queue->getSize();
126
        }, $this->queues));
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132
    public function purge(): bool
133
    {
134
        foreach ($this->queues as $queue) {
135
            $queue->purge();
136
        }
137
        return true;
138
    }
139
}
140