TaskBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 64
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 27 4
A hasCopyFilter() 0 6 2
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 the 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