Completed
Pull Request — master (#1)
by Arnaud
08:08 queued 05:21
created

TaskBuilder::build()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
ccs 0
cts 17
cp 0
rs 8.5806
cc 4
eloc 15
nc 5
nop 1
crap 20
1
<?php
2
3
namespace JK\Sam\Task;
4
5
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
class TaskBuilder
9
{
10
    /**
11
     * @var bool
12
     */
13
    protected $debug;
14
15
    /**
16
     * TaskBuilder constructor.
17
     * 
18
     * @param bool $debug
19
     */
20
    public function __construct($debug = false)
21
    {
22
        $this->debug = $debug;
23
    }
24
25
    /**
26
     * Build and return an array of Task.
27
     *
28
     * @param array $taskConfigurations
29
     * @return Task[]
30
     */
31
    public function build(array $taskConfigurations)
32
    {
33
        $resolver = new OptionsResolver();
34
        $tasks = [];
35
        
36
        foreach ($taskConfigurations as $taskName => $taskConfiguration) {
37
            $resolver->clear();
38
39
            // debug mode
40
            if ($this->debug === true) {
41
                $taskConfiguration['debug'] = $this->debug;
42
            }
43
            // add copy filter in last position if required
44
            if (!$this->hasCopyFilter($taskConfiguration)) {
45
                $taskConfiguration['filters'][] = 'copy';
46
            }
47
48
            $configuration = new TaskConfiguration();
49
            $configuration->configureOptions($resolver);
50
            $configuration->setParameters($resolver->resolve($taskConfiguration));
51
52
            $task = new Task($taskName, $configuration);
53
            $tasks[$taskName] = $task;
54
        }
55
56
        return $tasks;
57
    }
58
59
    /**
60
     * Return true if tje copy filter is in last position.
61
     *
62
     * @param array $configuration
63
     * @return bool
64
     */
65
    protected function hasCopyFilter(array $configuration)
66
    {
67
        return is_array($configuration['filters'])
68
            && $configuration['filters'][count($configuration['filters'] - 1)] == 'copy'
69
        ;
70
    }
71
}
72