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

MergeFilter::run()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 25
cp 0
rs 8.439
cc 6
eloc 18
nc 9
nop 2
crap 42
1
<?php
2
3
namespace JK\Sam\Filter\Merge;
4
5
use JK\Sam\Filter\Filter;
6
use SplFileInfo;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
/**
10
 * Merge different source files into one.
11
 */
12
class MergeFilter extends Filter
13
{
14
    /**
15
     * @param SplFileInfo[] $sources
16
     * @param SplFileInfo[] $destinations
17
     * @return array|\SplFileInfo[]
18
     */
19
    public function run(array $sources, array $destinations)
20
    {
21
        $fileSystem = new Filesystem();
22
        $mergedFiles = [];
23
24
        foreach ($this->getSupportedExtensions() as $extension) {
25
            $shouldAddMergeFile = false;
26
            $mergedFile = $this->getCacheDir().'merged.'.$extension;
27
28
            foreach ($sources as $source) {
29
30
                if ($source->getExtension() !== $extension) {
31
                    continue;
32
                }
33
                // creating the merge file if not exists
34
                if (!$fileSystem->exists($mergedFile)) {
35
                    $this->addNotification('creating '.$mergedFile.' merged file');
36
                    $fileSystem->touch($mergedFile);
37
                }
38
                $this->addNotification('merging file '.$source);
39
40
                // append the current content to the merge file
41
                file_put_contents($mergedFile, file_get_contents($source), FILE_APPEND);
42
43
                $shouldAddMergeFile = true;
44
            }
45
46
            if ($shouldAddMergeFile) {
47
                $mergedFiles[] = $mergedFile;
48
            }
49
        }
50
51
        return $mergedFiles;
52
    }
53
54
    /**
55
     * Return the file extensions supported by this filter
56
     *
57
     * @return string[]
58
     */
59
    public function getSupportedExtensions()
60
    {
61
        return [
62
            'css',
63
            'js',
64
        ];
65
    }
66
}
67