Completed
Push — dev ( ca2ae3...8ba62a )
by Arnaud
09:55
created

Locator::locate()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 35
ccs 20
cts 20
cp 1
rs 6.7272
cc 7
eloc 20
nc 14
nop 1
crap 7
1
<?php
2
3
namespace JK\Sam\File;
4
5
use Exception;
6
use SplFileInfo;
7
use Symfony\Component\Finder\Finder;
8
9
class Locator
10
{
11
    /**
12
     * @var Normalizer
13
     */
14
    protected $normalizer;
15
16
    /**
17
     * Locator constructor.
18
     *
19
     * @param Normalizer $normalizer
20
     */
21 2
    public function __construct(Normalizer $normalizer)
22
    {
23 2
        $this->normalizer = $normalizer;
24 2
    }
25
26
    /**
27
     * Locate a source either if it a file or a directory and normalize it. Return an array of SplFileInfo.
28
     *
29
     * @param mixed $source
30
     * @return SplFileInfo[]
31
     * @throws Exception
32
     */
33 2
    public function locate($source)
34
    {
35 2
        $sources = [];
36 2
        $normalizedSources = [];
37
38 2
        // if the wildcard is at the end of the string, it means that we look for a directory
39 1
        $endingWithWilCard = substr($source, strlen($source) - 2) === '/*';
40 1
41
        if ($endingWithWilCard) {
42 1
            $source = substr($source, 0, strlen($source) - 2);
43 1
        }
44 1
45 2
        if (is_dir($source) || $endingWithWilCard) {
46
            $finder = new Finder();
47 2
            $finder->in($source);
48 2
49 2
            foreach ($finder as $file) {
50
                $sources[] = $file;
51
            }
52
        } else if (strstr($source, '*') !== false) {
53 2
            // if the source contains a wildcard, we use it with the finder component
54 2
            $sources = $this->getSourcesFromFinder($source);
55
        } else {
56 2
            $sources[] = $source;
57 2
        }
58
59 2
        // each found sources should be normalized
60
        foreach ($sources as $source) {
61
            $normalizedSources[] = $this
62
                ->normalizer
63
                ->normalize($source);
64
        }
65 1
66
        return $normalizedSources;
67 1
    }
68
69
    /**
70
     * @return Normalizer
71
     */
72
    public function getNormalizer()
73
    {
74 2
        return $this->normalizer;
75
    }
76 2
77 2
    /**
78 2
     * @param $source
79 2
     * @return SplFileInfo[]
80
     */
81
    protected function getSourcesFromFinder($source)
82 2
    {
83 2
        $array = explode(DIRECTORY_SEPARATOR, $source);
84 2
        $filename = array_pop($array);
85 2
        $directory = $source;
86 2
        $pattern = '*';
87
88 2
        // if a dot is present, the last part is the filename pattern
89 2
        if (strstr($filename, '.') !== false) {
90
            $pattern = $filename;
91 2
            $directory = implode('/', $array);
92
        }
93 2
        $finder = new Finder();
94 2
        $finder
95 2
            ->name($pattern)
96
            ->in($directory);
97 2
98
        $sources = [];
99
100
        foreach ($finder as $source) {
101
            $sources[] = $source;
102
        }
103
104
        return $sources;
105
    }
106
}
107