Passed
Push — master ( 1f3a71...6d9d43 )
by Alain
02:40
created

FilesystemLocation   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.55%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 14
c 4
b 0
f 2
lcom 1
cbo 0
dl 0
loc 131
ccs 29
cts 31
cp 0.9355
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getURI() 0 10 3
A getURIs() 0 12 3
B transform() 0 19 6
A getVariants() 0 11 1
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 array|string|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
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
118
        try {
119 20
            foreach ($this->getVariants($entry) as $uri) {
120 20
                if (is_readable($uri)) {
121 20
                    if ($firstOnly) {
122 20
                        return $uri;
123
                    }
124 20
                    $uris [] = $uri;
125
                }
126
            }
127
        } catch (Exception $exception) {
128
            // Fail silently.
129
        }
130
131 3
        return $firstOnly ? false : $uris;
132
    }
133
134
    /**
135
     * Get the individual variants that could be matched for the location.
136
     *
137
     * @since 0.1.1
138
     *
139
     * @param string $entry Entry to get the variants for.
140
     *
141
     * @return array Array of variants to check.
142
     */
143 20
    protected function getVariants($entry)
144
    {
145 20
        $variants = [];
146
147 20
        array_map(function ($extension) use ($entry, &$variants) {
148 20
            $variants[] = $entry . $extension;
149 20
            $variants[] = $this->path . DIRECTORY_SEPARATOR . $entry . $extension;
150 20
        }, $this->extensions);
151
152 20
        return $variants;
153
    }
154
}
155