FinderAwareTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
cbo 1
dl 0
loc 61
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D getFiles() 0 33 10
A appendFileSet() 0 9 2
1
<?php
2
3
/**
4
 * This file is part of Bldr.io
5
 *
6
 * (c) Aaron Scherer <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE
10
 */
11
12
namespace Bldr\Block\Core\Task\Traits;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Finder\SplFileInfo;
16
17
/**
18
 * @author Aaron Scherer <[email protected]>
19
 */
20
trait FinderAwareTrait
21
{
22
    /**
23
     * Finds all the files for the given config
24
     *
25
     * @param array $source
26
     *
27
     * @return SplFileInfo[]
28
     *
29
     * @throws \Exception
30
     */
31
    protected function getFiles(array $source)
32
    {
33
        $fileSet = [];
34
        foreach ($source as $set) {
35
            if (!array_key_exists('files', $set)) {
36
                throw new \Exception("`src` must have a `files` option");
37
            }
38
39
            if (!array_key_exists('path', $set)) {
40
                $set['path'] = getcwd();
41
            }
42
43
            if (!array_key_exists('recursive', $set)) {
44
                $set['recursive'] = false;
45
            }
46
47
            $paths = is_array($set['path']) ? $set['path'] : [$set['path']];
48
            $files = is_array($set['files']) ? $set['files'] : [$set['files']];
49
            foreach ($paths as $path) {
50
                foreach ($files as $file) {
51
                    $finder = new Finder();
52
                    $finder->files()->in($path)->name($file);
53
                    if (!$set['recursive']) {
54
                        $finder->depth('== 0');
55
                    }
56
57
                    $fileSet = $this->appendFileSet($finder, $fileSet);
58
                }
59
            }
60
        }
61
62
        return $fileSet;
63
    }
64
65
    /**
66
     * @param Finder $finder
67
     * @param array  $fileSet
68
     *
69
     * @return SplFileInfo[]
70
     */
71
    protected function appendFileSet(Finder $finder, array $fileSet)
72
    {
73
        /** @var SplFileInfo[] $files */
74
        foreach ($finder as $file) {
75
            $fileSet[] = $file;
76
        }
77
78
        return $fileSet;
79
    }
80
}
81