Completed
Pull Request — master (#2)
by Pol
12:44 queued 01:45
created

CollectionFactory::simulate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
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 NAME = 'collectionFactory';
29
    public const ARGUMENTS = [
30
        'tasks',
31
        'options',
32
    ];
33
34
    /**
35
     * @return string
36
     */
37
    public function getHelp()
38
    {
39
        return $this->tasks['help'] ?? 'Yaml command defined in tasks.yml';
0 ignored issues
show
Bug introduced by
The property tasks does not exist on PhpTaskman\Core\Plugin\Task\CollectionFactory. Did you mean task?
Loading history...
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getTasks()
46
    {
47
        return $this->tasks['tasks'] ?? $this->tasks;
0 ignored issues
show
Bug introduced by
The property tasks does not exist on PhpTaskman\Core\Plugin\Task\CollectionFactory. Did you mean task?
Loading history...
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function run()
54
    {
55
        $arguments = $this->getTaskArguments();
56
57
        $collection = $this->collectionBuilder();
58
59
        foreach ($arguments['tasks'] as $task) {
60
            if (\is_string($task)) {
61
                $collection->addTask($this->taskExec($task));
62
63
                continue;
64
            }
65
66
            if (!\is_array($task)) {
67
                // Todo: Error.
68
                continue;
69
            }
70
71
            if (!isset($task['task'])) {
72
                // Todo: Error.
73
                continue;
74
            }
75
76
            if (!\is_string($task['task'])) {
77
                // Todo: Error.
78
                continue;
79
            }
80
81
            $collection->addTask($this->taskFactory($task));
82
        }
83
84
        return $collection->run();
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function simulate($context)
91
    {
92
        foreach ($this->getTasks() as $task) {
93
            if (\is_array($task)) {
94
                $task = Yaml::dump($task, 0);
95
            }
96
97
            $this->printTaskInfo($task, $context);
98
        }
99
    }
100
101
    /**
102
     * Secure option value.
103
     *
104
     * @param array  $task
105
     * @param string $name
106
     * @param mixed  $default
107
     */
108
    protected function secureOption(array &$task, $name, $default)
109
    {
110
        $task[$name] = $task[$name] ?? $default;
111
    }
112
113
    /**
114
     * @param array $task
115
     *
116
     * @throws \Robo\Exception\TaskException
117
     *
118
     * @return \Robo\Contract\TaskInterface
119
     */
120
    protected function taskFactory(array $task)
121
    {
122
        $arguments = $this->getTaskArguments();
123
124
        $this->secureOption($task, 'force', false);
125
        $this->secureOption($task, 'umask', 0000);
126
        $this->secureOption($task, 'recursive', false);
127
        $this->secureOption($task, 'time', \time());
128
        $this->secureOption($task, 'atime', \time());
129
        $this->secureOption($task, 'mode', 0777);
130
131
        if (!Robo::getContainer()->has('task.' . $task['task'])) {
132
            throw new TaskException($this, 'Unkown task: ' . $task['task']);
133
        }
134
135
        /** @var \PhpTaskman\Core\Contract\TaskInterface $taskFactory */
136
        $taskFactory = Robo::getContainer()->get('task.' . $task['task']);
137
        $taskFactory->setTask($task);
138
        $taskFactory->setOptions($arguments['options']);
139
140
        return $this
141
            ->collectionBuilder()
142
            ->addTaskList([
143
                $taskFactory,
144
            ]);
145
    }
146
}
147