Completed
Push — master ( 9f2dac...cab9cb )
by Arnaud
07:03 queued 01:55
created

TaskRunner::fetchSources()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace JK\Sam\Task;
4
5
use Exception;
6
use JK\Sam\File\Locator;
7
use JK\Sam\Filter\FilterInterface;
8
use SplFileInfo;
9
10
class TaskRunner
11
{
12
    /**
13
     * @var FilterInterface[]
14
     */
15
    protected $filters;
16
17
    /**
18
     * @var Locator
19
     */
20
    protected $locator;
21
22
    /**
23
     * TaskRunner constructor.
24
     *
25
     * @param array $filters
26
     * @param Locator $locator
27
     */
28
    public function __construct(array $filters, Locator $locator)
29
    {
30
        $this->filters = $filters;
31
        $this->locator = $locator;
32
    }
33
34
    /**
35
     * Run a task, load its sources before and call the clean method on the filter.
36
     *
37
     * @param Task $task
38
     * @throws Exception
39
     */
40
    public function run(Task $task)
41
    {
42
        // get configured filters for this task
43
        $filters = $task
44
            ->getConfiguration()
45
            ->getParameter('filters');
46
47
        // automatic adding of the copy filter
48
        $filters[] = 'copy';
49
50
        // get sources files
51
        $sources = $this->fetchSources($task);
52
        $destinations = $this->fetchDestinations($task);
53
54
        foreach ($filters as $filterName) {
55
            // get current configured filter
56
            $filter = $this->getFilter($filterName);
57
58
            // filter the files supported by this filter
59
            $filteredSources = $this->filterSources($sources, $filter);
60
61
            // apply the filter
62
            $updatedSources = $filter->run($filteredSources, $destinations);
63
64
            if ($updatedSources === null) {
65
                $updatedSources = [];
66
            }
67
68
            // clean the generated files by the filter
69
            $filter->clean();
70
            
71
            // update new sources
72
            $sources = $this->updateSources($sources, $filteredSources, $updatedSources);
73
        }
74
    }
75
76
    /**
77
     * Return a filter by its name. Throw an exception if it is not exists.
78
     *
79
     * @param string $name
80
     * @return FilterInterface
81
     * @throws Exception
82
     */
83
    protected function getFilter($name)
84
    {
85
        // filters must exists in configured filters
86
        if (!array_key_exists($name, $this->filters)) {
87
            throw new Exception('Invalid filter '.$name.'. Check your mapping configuration');
88
        }
89
90
        return $this->filters[$name];
91
    }
92
93
    /**
94
     * Fetch the source files from the task and return and array of SplInfo.
95
     *
96
     * @param Task $task
97
     * @return array
98
     */
99
    protected function fetchSources(Task $task)
100
    {
101
        $sources = [];
102
103
        foreach ($task->getSources() as $source) {
104
            // locate new resource and merge them to the existing sources
105
            $sources = array_merge($sources, $this->locator->locate($source));
106
        }
107
108
        return $sources;
109
    }
110
111
    /**
112
     * Fetch the destination files from the task and return and array of SplInfo.
113
     *
114
     * @param Task $task
115
     * @return array
116
     */
117
    protected function fetchDestinations(Task $task)
118
    {
119
        $sources = [];
120
121
        foreach ($task->getDestinations() as $source) {
122
            // locate new resource and merge them to the existing sources
123
            $sources[] = $this
124
                ->locator
125
                ->getNormalizer()
126
                ->normalize($source);
127
        }
128
129
        return $sources;
130
    }
131
132
    /**
133
     * Filter only the sources supported by the current filter.
134
     *
135
     * @param SplFileInfo[] $sources
136
     * @param FilterInterface $filter
137
     * @return array
138
     */
139
    protected function filterSources(array $sources, FilterInterface $filter)
140
    {
141
        $filteredSources = [];
142
143
        // if the filter support all the files types, no need to filter
144
        if (in_array('*', $filter->getSupportedExtensions())) {
145
            return $sources;
146
        }
147
148
        foreach ($sources as $source) {
149
150
            if (in_array($source->getExtension(), $filter->getSupportedExtensions())) {
151
                $filteredSources[] = $source;
152
            }
153
        }
154
155
        return $filteredSources;
156
    }
157
158
    /**
159
     * Remove the filtered files from the sources, and merge with the new ones.
160
     *
161
     * @param SplFileInfo[] $originalSources
162
     * @param SplFileInfo[] $filteredSources
163
     * @param SplFileInfo[] $updatedSources
164
     * @return SplFileInfo[]
165
     * @throws Exception
166
     */
167
    protected function updateSources(array $originalSources, array $filteredSources, array $updatedSources)
168
    {
169
        $sources = [];
170
171
        // keep only the not filtered files
172
        foreach ($originalSources as $source) {
173
174
            if (!in_array($source, $filteredSources)) {
175
                $sources[] = $source;
176
            }
177
        }
178
        // check updated files
179
        foreach ($updatedSources as $index => $source) {
180
            if (is_string($source)) {
181
                $updatedSources[$index] = new SplFileInfo($source);
182
            } else if (!($source instanceof SplFileInfo)) {
183
                throw new Exception('Invalid source file type '.gettype($source));
184
            }
185
        }
186
        // merge with the new sources
187
        $sources = array_merge($sources, $updatedSources);
188
189
        return $sources;
190
    }
191
}
192