Locator::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
namespace Tuum\View;
3
4
class Locator implements LocatorInterface
5
{
6
    /**
7
     * @var string[]
8
     */
9
    private $dirs = [];
10
11
    /**
12
     * @param string $root
13
     */
14
    public function __construct($root = null)
0 ignored issues
show
Unused Code introduced by
The parameter $root is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
15
    {
16
        $this->dirs = [];
17
        $roots      = func_get_args();
18
        foreach ($roots as $root) {
19
            $this->addRoot($root);
20
        }
21
    }
22
23
    /**
24
     * @param string $root
25
     */
26
    public function addRoot($root)
27
    {
28
        if (substr($root, -1) !== '/') {
29
            $root .= '/';
30
        }
31
        $this->dirs = array_merge([$root], $this->dirs);
32
    }
33
34
    /**
35
     * @param string $file
36
     * @return bool|string
37
     */
38
    public function locate($file)
39
    {
40
        $file = substr($file,0,1) ==='/' ? substr($file,1) : $file;
41
        foreach ($this->dirs as $system) {
42
            $location = $system . $file;
43
            if (file_exists($location)) {
44
                return $location;
45
            }
46
        }
47
        return false;
48
    }
49
}