Completed
Pull Request — master (#2)
by Pol
12:21 queued 10:17
created

RunTask::run()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 4
nop 0
dl 0
loc 34
ccs 0
cts 21
cp 0
crap 30
rs 9.4555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Plugin\Task;
6
7
use PhpTaskman\Core\Plugin\BaseTask;
8
use Robo\Common\BuilderAwareTrait;
9
use Robo\Exception\TaskException;
10
use Robo\Robo;
11
use Robo\Task\Base\loadTasks;
12
13
final class RunTask extends BaseTask
14
{
15
    use BuilderAwareTrait;
16
    use loadTasks;
17
18
    public const ARGUMENTS = [];
19
    public const NAME = 'run';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function run()
25
    {
26
        $arguments = $this->getTaskArguments();
27
28
        $bin = \realpath(__DIR__ . '/../../../' . $this->getConfig()->get('taskman.bin_dir') . '/taskman');
29
30
        if (false === $bin) {
31
            throw new TaskException(__CLASS__, 'Unable to find the taskman binary');
32
        }
33
34
        $taskExec = $this->taskExec($bin)->arg($arguments['command']);
35
36
        $container = Robo::getContainer();
37
38
        /** @var \Robo\Application $app */
39
        $app = $container->get('application');
40
41
        /** @var \Consolidation\AnnotatedCommand\AnnotatedCommand $command */
42
        $command = $app->get($arguments['command']);
43
        $commandOptions = $command->getDefinition()->getOptions();
44
45
        // Propagate any input option passed to the child command.
46
        foreach ($arguments['options'] as $name => $values) {
47
            if (!isset($commandOptions[$name])) {
48
                continue;
49
            }
50
51
            // But only if the called command has this option.
52
            foreach ((array) $values as $value) {
53
                $taskExec->option($name, $value);
54
            }
55
        }
56
57
        return $taskExec->run();
58
    }
59
}
60