Completed
Pull Request — master (#5)
by Josh
05:31
created

TaskList::getTasks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace pmill\Scheduler;
4
5
use pmill\Scheduler\Interfaces\Task as TaskInterface;
6
7
class TaskList
8
{
9
    /**
10
     * @var TaskInterface[]
11
     */
12
    protected $tasks = [];
13
14
    /**
15
     * @var string[]
16
     */
17
    protected $output = [];
18
19
    /**
20
     * Adds a new task to the list
21
     *
22
     * @param TaskInterface $task
23
     * @return TaskList $this
24
     */
25
    public function addTask(TaskInterface $task)
26
    {
27
        $this->tasks[] = $task;
28
        return $this;
29
    }
30
31
    /**
32
     * @param TaskInterface[] $tasks
33
     */
34
    public function setTasks($tasks)
35
    {
36
        $this->tasks = $tasks;
37
    }
38
39
    /**
40
     * @return TaskInterface[]
41
     */
42
    public function getTasks()
43
    {
44
        return $this->tasks;
45
    }
46
47
    /**
48
     * @return TaskInterface[]
49
     */
50
    public function getTasksDue()
51
    {
52
        return array_filter($this->tasks, function (TaskInterface $task) {
53
            return $task->isDue();
54
        });
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function getOutput()
61
    {
62
        return $this->output;
63
    }
64
65
    /**
66
     * Runs any due task, returning an array containing the output from each task
67
     *
68
     * @return array
69
     */
70
    public function run()
71
    {
72
        $this->output = [];
73
74
        foreach ($this->getTasksDue() as $task) {
75
            $result = $task->run();
76
            $this->output[] = [
77
                'task' => get_class($task),
78
                'result' => $result,
79
                'output' => $task->getOutput(),
80
            ];
81
        }
82
83
        return $this->output;
84
    }
85
}
86