Passed
Push — master ( e249f1...1f3a71 )
by Alain
02:44
created

FilesystemLocation::getURIs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
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 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\View\Location;
13
14
use Exception;
15
16
/**
17
 * Class FilesystemLocation.
18
 *
19
 * @since   0.1.0
20
 *
21
 * @package BrightNucleus\View\Location
22
 * @author  Alain Schlesser <[email protected]>
23
 */
24
class FilesystemLocation implements LocationInterface
25
{
26
27
    /**
28
     * Path that this location points to.
29
     *
30
     * @since 0.1.0
31
     *
32
     * @var string
33
     */
34
    protected $path;
35
36
    /**
37
     * Extensions that this location can accept.
38
     *
39
     * @since 0.1.0
40
     *
41
     * @var array
42
     */
43
    protected $extensions;
44
45
    /**
46
     * Instantiate a FilesystemLocation object.
47
     *
48
     * @since 0.1.0
49
     *
50
     * @param string $path       Path that this location points to.
51
     * @param array  $extensions Array of extensions that this location can accept.
52
     */
53 20
    public function __construct($path, array $extensions = [])
54
    {
55 20
        $this->path       = $path;
56 20
        $this->extensions = array_merge($extensions, ['']);
57 20
    }
58
59
    /**
60
     * Get the first URI that matches the given criteria.
61
     *
62
     * @since 0.1.0
63
     *
64
     * @param array $criteria Criteria to match.
65
     *
66
     * @return string|false URI that matches the criteria or false if none found.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array|false? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
67
     */
68 20
    public function getURI(array $criteria)
69
    {
70 20
        foreach ($criteria as $entry) {
71 20
            if ($uri = $this->transform($entry, true)) {
72 20
                return $uri;
73
            }
74
        }
75
76
        return false;
77
    }
78
79
    /**
80
     * Get all URIs that match the given criteria.
81
     *
82
     * @since 0.1.1
83
     *
84
     * @param array $criteria Criteria to match.
85
     *
86
     * @return array URIs that match the criteria or empty array if none found.
87
     */
88 3
    public function getURIs(array $criteria)
89
    {
90 3
        $uris = [];
91
92 3
        foreach ($criteria as $entry) {
93 3
            if ($uri = $this->transform($entry, false)) {
94 3
                $uris = array_merge($uris, (array)$uri);
95
            }
96
        }
97
98 3
        return $uris;
99
    }
100
101
    /**
102
     * Try to transform the entry into possible URIs.
103
     *
104
     * @since 0.1.0
105
     *
106
     * @param string $entry     Entry to transform.
107
     * @param bool   $firstOnly Return the first result only.
108
     *
109
     * @return array|string|false If $firstOnly is true, returns a string with the URI of the view, or false if none
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
110
     *                            found.
111
     *                            If $firstOnly is false, returns an array with all matching URIs, or an empty array if
112
     *                            none found.
113
     */
114 20
    protected function transform($entry, $firstOnly = true)
115
    {
116 20
        $uris = [];
117
        try {
118 20
            foreach ($this->extensions as $extension) {
119
120
                $variants = [
121 20
                    $entry . $extension,
122 20
                    $this->path . DIRECTORY_SEPARATOR . $entry . $extension,
123
                ];
124
125 20
                foreach ($variants as $uri) {
126 20
                    if (is_readable($uri)) {
127 20
                        if ($firstOnly) {
128 20
                            return $uri;
129
                        }
130 20
                        $uris [] = $uri;
131
                    }
132
                }
133
            }
134
        } catch (Exception $exception) {
135
            // Fail silently.
136
        }
137
138 3
        return $firstOnly ? false : $uris;
139
    }
140
}
141