CollectionTrait::getJob()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
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
namespace flipbox\queue\jobs\traits;
4
5
use craft\helpers\ArrayHelper;
6
use flipbox\queue\helpers\JobHelper;
7
use flipbox\queue\jobs\JobInterface;
8
use yii\base\Exception;
9
10
trait CollectionTrait
11
{
12
13
    use JobTrait;
14
15
    /**
16
     * @var JobInterface[]
17
     */
18
    protected $jobs;
19
20
    /**
21
     * @param string $class
22
     * @return array
23
     */
24
    protected function jobConfig(string $class): array
25
    {
26
        return [
27
            'class' => $class
28
        ];
29
    }
30
31
    /**
32
     * @param array $jobs
33
     * @return static
34
     */
35
    public function setJobs(array $jobs = [])
36
    {
37
        $this->jobs = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<fli...eue\jobs\JobInterface>> of property $jobs.

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...
38
39
        return $this->addJobs($jobs);
40
    }
41
42
    /**
43
     * @param array $jobs
44
     * @return static
45
     */
46
    protected function addJobs(array $jobs = [])
47
    {
48
        foreach ($jobs as $key => $job) {
49
            if (is_numeric($key) || empty($key)) {
50
                $key = null;
51
            }
52
            $this->addJob($key, $job);
53
        }
54
55
        return $this;
56
    }
57
58
    /**
59
     * @param $key
60
     * @param $job
61
     * @return static
62
     */
63
    public function addJob($key, $job)
64
    {
65
        if (is_string($job)) {
66
            $job = $this->jobConfig($job);
67
        }
68
69
        if (!$job instanceof JobInterface) {
70
            $job = JobHelper::create($job);
71
        }
72
73
        $this->jobs[$key] = $job;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return JobInterface[]
80
     */
81
    public function getJobs()
82
    {
83
        return $this->jobs;
84
    }
85
86
    /**
87
     * @param $identifier
88
     * @return JobInterface
89
     */
90
    public function findJob($identifier)
91
    {
92
        return ArrayHelper::getValue($this->jobs, $identifier);
93
    }
94
95
    /**
96
     * @param $identifier
97
     * @return JobInterface
98
     * @throws Exception
99
     */
100
    public function getJob($identifier)
101
    {
102
        if (!$job = $this->findJob($identifier)) {
103
            throw new Exception("Job not found.");
104
        }
105
106
        return $job;
107
    }
108
109
    /**
110
     * @param $identifier
111
     * @param array $config
112
     * @return JobInterface
113
     */
114
    public function createJob($identifier, array $config = [])
115
    {
116
        $config['class'] = $this->getJob($identifier);
117
118
        return JobHelper::create($config);
119
    }
120
}
121