Completed
Pull Request — master (#1)
by Arnaud
02:14
created

Locator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 98
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C locate() 0 35 7
A getNormalizer() 0 4 1
B getSourcesFromFinder() 0 25 3
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 5
    public function __construct(Normalizer $normalizer)
22
    {
23 5
        $this->normalizer = $normalizer;
24 5
    }
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 5
    public function locate($source)
34
    {
35 5
        $sources = [];
36 5
        $normalizedSources = [];
37
38
        // if the wildcard is at the end of the string, it means that we look for a directory
39 5
        $endingWithWilCard = substr($source, strlen($source) - 2) === '/*';
40
41 5
        if ($endingWithWilCard) {
42 1
            $source = substr($source, 0, strlen($source) - 2);
43 1
        }
44
45 5
        if (is_dir($source) || $endingWithWilCard) {
46 2
            $finder = new Finder();
47 2
            $finder->in($source);
48
49 2
            foreach ($finder as $file) {
50 2
                $sources[] = $file;
51 2
            }
52 5
        } else if (strstr($source, '*') !== false) {
53
            // if the source contains a wildcard, we use it with the finder component
54 2
            $sources = $this->getSourcesFromFinder($source);
55 2
        } else {
56 2
            $sources[] = $source;
57
        }
58
59
        // each found sources should be normalized
60 5
        foreach ($sources as $source) {
61 5
            $normalizedSources[] = $this
62
                ->normalizer
63 5
                ->normalize($source);
64 5
        }
65
66 5
        return $normalizedSources;
67
    }
68
69
    /**
70
     * @return Normalizer
71
     */
72 2
    public function getNormalizer()
73
    {
74 2
        return $this->normalizer;
75
    }
76
77
    /**
78
     * @param $source
79
     * @return SplFileInfo[]
80
     */
81 2
    protected function getSourcesFromFinder($source)
82
    {
83 2
        $array = explode(DIRECTORY_SEPARATOR, $source);
84 2
        $filename = array_pop($array);
85 2
        $directory = $source;
86 2
        $pattern = '*';
87
88
        // if a dot is present, the last part is the filename pattern
89 2
        if (strstr($filename, '.') !== false) {
90 2
            $pattern = $filename;
91 2
            $directory = implode('/', $array);
92 2
        }
93 2
        $finder = new Finder();
94
        $finder
95 2
            ->name($pattern)
96 2
            ->in($directory);
97
98 2
        $sources = [];
99
100 2
        foreach ($finder as $source) {
101 2
            $sources[] = $source;
102 2
        }
103
104 2
        return $sources;
105
    }
106
}
107