AbstractCollection::runInternal()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 12
1
<?php
2
3
namespace flipbox\queue\jobs;
4
5
use flipbox\queue\helpers\JobHelper;
6
use flipbox\queue\jobs\traits\CollectionTrait;
7
8
abstract class AbstractCollection extends AbstractJob
9
{
10
11
    use CollectionTrait;
12
13
    /**
14
     * @var bool
15
     */
16
    public $toQueue = true;
17
18
    /**
19
     * @return bool
20
     */
21
    protected function runInternal()
22
    {
23
        $success = true;
24
25
        foreach ($this->getJobs() as $job) {
26
            if (!$this->runJob($job)) {
27
                $success = false;
28
            }
29
        }
30
31
        return $success;
32
    }
33
34
    /**
35
     * @param string $job
36
     * @return mixed
37
     */
38
    protected function runJob($job)
39
    {
40
        if (is_string($job)) {
41
            $job = $this->jobConfig($job);
42
        }
43
44
        if (!$job instanceof JobInterface) {
45
            $job = JobHelper::create($job);
46
        }
47
48
        if ($this->toQueue) {
49
            return $job->toQueue();
50
        }
51
52
        return $job->run();
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function toConfig(): array
59
    {
60
        return array_merge(
61
            parent::toConfig(),
62
            [
63
                'jobs' => $this->getJobsConfig()
64
            ]
65
        );
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    protected function getJobsConfig()
72
    {
73
        $jobsConfig = [];
74
        foreach ($this->getJobs() as $job) {
75
            $jobsConfig[] = $job->toConfig();
76
        }
77
        return $jobsConfig;
78
    }
79
}
80