JuntyRunner::runTask()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 6.9811
cc 7
eloc 12
nc 10
nop 1
1
<?php
2
/**
3
 * Junty
4
 *
5
 * @author Gabriel Jacinto aka. GabrielJMJ <[email protected]>
6
 * @license MIT License
7
 */
8
 
9
namespace Junty\Runner;
10
11
use Junty\TaskRunner\Runner\Runner;
12
use Junty\TaskRunner\Task\{Task, TaskInterface};
13
use Junty\Stream\StreamHandler;
14
use Junty\Exception\JuntyException;
15
16
class JuntyRunner extends Runner
17
{
18
    /**
19
     * Runs one single task
20
     *
21
     * @param string|TaskInterface $task
22
     */
23
    public function runTask($task)
24
    {
25
        if (!is_string($task) && !$task instanceof TaskInterface) {
26
            throw JuntyException::invalidTaskType();
27
        }
28
29
        $tasks = $this->getTasks();
30
31
        if (is_string($task)) {
32
            if (!isset($tasks[$task])) {
33
                throw JuntyException::taskNotRegistred($task);
34
            }
35
        }
36
37
        $task = $task instanceof TaskInterface ? $task : $tasks[$task];
38
        $cb = \Closure::bind($task->getCallback(), new StreamHandler());
39
        $cb();
40
41
        if ($task->hasNext()) {
42
            $this->runTask($tasks[$task->getNext()]);
43
        }
44
    }
45
}