Completed
Pull Request — master (#2)
by Pol
02:43
created

CollectionFactory::simulate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 12
rs 10
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 PhpTaskman\Core\Robo\Task\Filesystem\LoadFilesystemTasks;
9
use Robo\Contract\BuilderAwareInterface;
10
use Robo\Contract\SimulatedInterface;
11
use Robo\Exception\TaskException;
12
use Robo\LoadAllTasks;
13
use Robo\Robo;
14
use Symfony\Component\Yaml\Yaml;
15
16
/**
17
 * Class CollectionFactory.
18
 *
19
 * Return a task collection given its array representation.
20
 */
21
final class CollectionFactory extends BaseTask implements
22
    BuilderAwareInterface,
23
    SimulatedInterface
24
{
25
    use LoadAllTasks;
26
    use LoadFilesystemTasks;
27
28
    public const ARGUMENTS = [
29
        'tasks',
30
        'options',
31
    ];
32
    public const NAME = 'collectionFactory';
33
34
    /**
35
     * @var array
36
     */
37
    private $tasks;
38
39
    /**
40
     * @return string
41
     */
42
    public function getHelp()
43
    {
44
        return $this->tasks['help'] ?? 'Yaml command defined in tasks.yml';
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getTasks()
51
    {
52
        return $this->tasks['tasks'] ?? $this->tasks;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function run()
59
    {
60
        $arguments = $this->getTaskArguments();
61
62
        $collection = $this->collectionBuilder();
63
64
        foreach ($arguments['tasks'] as $task) {
65
            if (\is_string($task)) {
66
                $collection->addTask($this->taskExec($task));
67
68
                continue;
69
            }
70
71
            if (!\is_array($task)) {
72
                // Todo: Error.
73
                continue;
74
            }
75
76
            if (!isset($task['task'])) {
77
                // Todo: Error.
78
                continue;
79
            }
80
81
            if (!\is_string($task['task'])) {
82
                // Todo: Error.
83
                continue;
84
            }
85
86
            $collection->addTask($this->taskFactory($task));
87
        }
88
89
        return $collection->run();
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function simulate($context)
96
    {
97
        foreach ($this->getTasks() as $task) {
98
            if (\is_array($task)) {
99
                $task = Yaml::dump($task, 0);
100
            }
101
102
            $this->printTaskInfo($task, $context);
103
        }
104
    }
105
106
    /**
107
     * Secure option value.
108
     *
109
     * @param array  $task
110
     * @param string $name
111
     * @param mixed  $default
112
     */
113
    protected function secureOption(array &$task, $name, $default)
114
    {
115
        $task[$name] = $task[$name] ?? $default;
116
    }
117
118
    /**
119
     * @param array $task
120
     *
121
     * @throws \Robo\Exception\TaskException
122
     *
123
     * @return \Robo\Contract\TaskInterface
124
     */
125
    protected function taskFactory(array $task)
126
    {
127
        $this->secureOption($task, 'force', false);
128
        $this->secureOption($task, 'umask', 0000);
129
        $this->secureOption($task, 'recursive', false);
130
        $this->secureOption($task, 'time', \time());
131
        $this->secureOption($task, 'atime', \time());
132
        $this->secureOption($task, 'mode', 0777);
133
134
        $arguments = \array_merge($this->getTaskArguments(), $task);
135
136
        if (!Robo::getContainer()->has('task.' . $task['task'])) {
137
            throw new TaskException($this, 'Unkown task: ' . $task['task']);
138
        }
139
140
        /** @var \PhpTaskman\Core\Contract\TaskInterface $taskFactory */
141
        $taskFactory = Robo::getContainer()->get('task.' . $task['task']);
142
        $taskFactory->setTaskArguments($arguments);
143
144
        return $this
145
            ->collectionBuilder()
146
            ->addTaskList([
147
                $taskFactory,
148
            ]);
149
    }
150
}
151