Completed
Push — dev ( 1265cc...e67fd6 )
by Arnaud
03:08
created

Locator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 91
ccs 40
cts 40
cp 1
rs 10

4 Methods

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