AbstractCollection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 72
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A runInternal() 0 12 3
A runJob() 0 16 4
A toConfig() 0 9 1
A getJobsConfig() 0 8 2
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