AbstractTaskRunner   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 56.36%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 183
wmc 19
lcom 2
cbo 1
ccs 31
cts 55
cp 0.5636
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCurrentTime() 0 4 1
A setCurrentTime() 0 9 2
A getCurrentEnvironment() 0 4 1
A setCurrentEnvironment() 0 5 1
B getDueTasks() 0 17 5
A runAllTasks() 0 7 1
A runDueTasks() 0 7 1
A runTask() 0 9 1
A runTasks() 0 11 2
A flagForTesting() 0 5 1
A getTaskBags() 0 4 1
A setTaskBags() 0 5 1
executeTasks() 0 1 ?
1
<?php
2
namespace Mistletoe\Runners;
3
use Mistletoe\Contracts\TaskRunnerInterface;
4
use Mistletoe\TaskBag;
5
6
/**
7
 * Class AbstractTaskRunner
8
 * @package Runners
9
 */
10
abstract class AbstractTaskRunner implements TaskRunnerInterface
11
{
12
    /**
13
     * @var array of TaskBags
14
     */
15
    private $taskBags;
16
17
    /**
18
     * @var string
19
     */
20
    private $currentTime = 'now';
21
22
    /**
23
     * @var string
24
     */
25
    protected $currentEnvironment;
26
27
    /**
28
     * @var bool
29
     */
30
    protected $testing = false;
31
32
    /**
33
     * TaskRunner constructor.
34
     * @param array $taskBags
35
     */
36 2
    public function __construct(array $taskBags = [])
37
    {
38 2
        $this->taskBags = $taskBags;
39 2
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getCurrentTime()
45
    {
46
        return $this->currentTime;
47
    }
48
49
    /**
50
     * @param string|\DateTime $currentTime
51
     * @return $this
52
     * @throws \Exception
53
     */
54 2
    public function setCurrentTime($currentTime)
55
    {
56 2
        if (!is_string($currentTime)) {
57
            throw new \Exception("Current time must be a string");
58
        }
59
60 2
        $this->currentTime = $currentTime;
61 2
        return $this;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getCurrentEnvironment()
68
    {
69
        return $this->currentEnvironment;
70
    }
71
72
    /**
73
     * @param string $currentEnvironment
74
     * @return $this
75
     */
76 2
    public function setCurrentEnvironment($currentEnvironment)
77
    {
78 2
        $this->currentEnvironment = $currentEnvironment;
79 2
        return $this;
80
    }
81
82
    /**
83
     * Returns an array of currently due tasks
84
     * @return array
85
     * @throws \Exception
86
     */
87 2
    public function getDueTasks()
88
    {
89 2
        $dueTasks = [];
90 2
        foreach ($this->taskBags as $taskName => $task) {
91 2
            if (!$task instanceof TaskBag) {
92
                throw new \Exception("Tasks must be instances of TaskBag");
93
            }
94
95 2
            if ($task->isDue($this->currentTime)) {
96 2
                if (in_array($this->currentEnvironment, $task->getEnvironments())) {
97 2
                    $dueTasks[$taskName] = $task;
98 2
                }
99 2
            }
100 2
        }
101
102 2
        return $dueTasks;
103
    }
104
105
    /* Run Tasks */
106
    /**
107
     * Force run every registered task
108
     * @return bool
109
     */
110
    public function runAllTasks()
111
    {
112
        $tasks = $this->taskBags;
113
        $success = $this->executeTasks($tasks);
114
115
        return $success;
116
    }
117
118
    /**
119
     * Run the tasks that are due right now
120
     * @return bool
121
     * @throws \Exception
122
     */
123 1
    public function runDueTasks()
124
    {
125 1
        $tasks = $this->getDueTasks();
126 1
        $success = $this->executeTasks($tasks);
127
128 1
        return $success;
129
    }
130
131
    /**
132
     * Run a specific task
133
     * @param $task
134
     * @return bool
135
     */
136
    public function runTask($task)
137
    {
138
        $tasks = [
139
            $task => $this->taskBags[$task]
140
        ];
141
        $success = $this->executeTasks($tasks);
142
143
        return $success;
144
    }
145
146
    /**
147
     * Run multiple specific tasks
148
     * @param array $tasks
149
     * @return bool
150
     */
151
    public function runTasks(array $tasks)
152
    {
153
        $list = [];
154
        foreach ($tasks as $task) {
155
            $list[$task] = $this->taskBags[$task];
156
        }
157
158
        $success = $this->executeTasks($list);
159
160
        return $success;
161
    }
162
163 1
    public function flagForTesting($switch = false)
164
    {
165 1
        $this->testing = $switch;
166 1
        return $this;
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    public function getTaskBags()
173
    {
174
        return $this->taskBags;
175
    }
176
177
    /**
178
     * @param array $taskBags
179
     * @return $this
180
     */
181 1
    public function setTaskBags($taskBags)
182
    {
183 1
        $this->taskBags = $taskBags;
184 1
        return $this;
185
    }
186
187
    /**
188
     * @param array $tasks
189
     * @return mixed
190
     */
191
    abstract protected function executeTasks(array $tasks);
192
}
193