Test Failed
Push — master ( 147d4d...670e11 )
by Alain
03:28
created

FilesystemLocation::getNamePattern()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 10
cts 10
cp 1
rs 8.8497
c 0
b 0
f 0
cc 6
nc 1
nop 2
crap 6
1
<?php declare(strict_types=1);
2
/**
3
 * Bright Nucleus View Component.
4
 *
5
 * @package   BrightNucleus\View
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016-2017 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\View\Location;
13
14
use BrightNucleus\View\Support\Extensions;
15
use BrightNucleus\View\Support\URIHelper;
16
use Exception;
17
use Symfony\Component\Finder\Finder;
18
use Symfony\Component\Finder\SplFileInfo;
19
20
/**
21
 * Class FilesystemLocation.
22
 *
23
 * @since   0.1.0
24
 *
25
 * @package BrightNucleus\View\Location
26
 * @author  Alain Schlesser <[email protected]>
27
 */
28
class FilesystemLocation implements Location
29
{
30
31
    /**
32
     * Path that this location points to.
33
     *
34
     * @since 0.1.0
35
     *
36
     * @var string
37
     */
38
    protected $path;
39
40
    /**
41
     * Extensions that this location can accept.
42
     *
43
     * @since 0.1.0
44
     *
45
     * @var Extensions
46
     */
47
    protected $extensions;
48
49
    /**
50
     * Instantiate a FilesystemLocation object.
51
     *
52
     * @since 0.1.0
53
     *
54
     * @param string                       $path       Path that this location points to.
55
     * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept.
56
     */
57 45
    public function __construct(string $path, $extensions = null)
58
    {
59 45
        $this->path       = $path;
60 45
        $this->extensions = $this->validateExtensions($extensions);
61 45
    }
62
63
    /**
64
     * Get the first URI that matches the given criteria.
65
     *
66
     * @since 0.1.0
67
     *
68
     * @param array $criteria Criteria to match.
69
     *
70
     * @return string|false URI that matches the criteria or false if none found.
71
     */
72 44
    public function getURI(array $criteria)
73
    {
74 44
        $uris = $this->getURIs($criteria);
75
76 44
        return $uris->count() > 0
77 43
            ? $uris->first()
78 44
            : false;
79
    }
80
81
    /**
82
     * Get all URIs that match the given criteria.
83
     *
84
     * @since 0.1.1
85
     *
86
     * @param array $criteria Criteria to match.
87
     *
88
     * @return URIs URIs that match the criteria or an empty collection if none found.
89
     */
90 44
    public function getURIs(array $criteria): URIs
91
    {
92 44
        $uris = new URIs();
93
94 44
        foreach ($this->extensions as $extension) {
95 44
            $finder = new Finder();
96
97
            try {
98 44
                $finder->files()
99 44
                    ->name($this->getNamePattern($criteria, $extension))
100 44
                    ->in($this->getPathPattern());
101 43
                foreach ($finder as $file) {
102
                    /** @var SplFileInfo $file */
103 43
                    $uris->add($file->getPathname());
104
                }
105 44
            } catch (Exception $exception) {
106
                // Fail silently;
107
            }
108
        }
109
110 44
        return $uris;
111
    }
112
113
    /**
114
     * Get the name pattern to pass to the file finder.
115
     *
116
     * @since 0.1.3
117
     *
118
     * @param array  $criteria  Criteria to match.
119
     * @param string $extension Extension to match.
120
     *
121
     * @return string Name pattern to pass to the file finder.
122
     */
123 44
    protected function getNamePattern(array $criteria, string $extension): string
124
    {
125 44
        $names = [];
126
127
        $names[] = array_map(function ($criterion) use ($extension) {
128 44
            $uriExtension = URIHelper::containsExtension($criterion);
129
            if (! empty($extension)) {
130 44
                $extension = ltrim($extension, '.');
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $extension, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
131 20
132 44
                if ($uriExtension === $extension) {
133 44
                    $criterion = substr($criterion,0,-strlen(".{$extension}"));
134
                }
135 44
            } else {
136
                $extension = URIHelper::containsExtension($criterion);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $extension, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
137
                if (!empty($extension)) {
138
                    $criterion = substr($criterion,0,-strlen(".{$extension}"));
139
                }
140
            }
141
142
            $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1));
143
144
            return (empty($extension) || URIHelper::hasExtension($criterion, $extension))
145 44
                ? "{$criterion}(?:\..*?)$"
146
                : "{$criterion}\.{$extension}$";
147 44
        }, $criteria)[0];
148
149
        return chr(1) . implode('|', array_unique($names)) . chr(1);
150
    }
151
152
    /**
153
     * Get the path pattern to pass to the file finder.
154
     *
155
     * @since 0.1.3
156
     *
157
     * @return string Path pattern to pass to the file finder.
158
     */
159
    protected function getPathPattern(): string
160
    {
161 44
        return $this->path;
162 44
    }
163 44
164
    /**
165 44
     * Validate the extensions and return a collection.
166
     *
167
     * @since 0.1.1
168
     *
169
     * @param Extensions|array|string|null $extensions Extensions to validate.
170
     *
171
     * @return Extensions Validated extensions collection.
172
     */
173
    protected function validateExtensions($extensions): Extensions
174
    {
175
        if (empty($extensions)) {
176
            $extensions = new Extensions(['']);
177 45
        }
178
179 45
        if (! $extensions instanceof Extensions) {
180 8
            $extensions = new Extensions((array)$extensions);
181
        }
182
183 45
        return $extensions;
184 38
    }
185
}
186