FilesystemLoader   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 55.17%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 82
ccs 16
cts 29
cp 0.5517
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A cacheKey() 0 9 2
A load() 0 9 2
A getBasePath() 0 3 1
A setBasePath() 0 4 1
B componentPath() 0 19 5
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Ebi;
9
10
11
class FilesystemLoader implements TemplateLoaderInterface {
12
    /**
13
     * @var string
14
     */
15
    private $basePath;
16
17 26
    public function __construct($basePath) {
18 26
        $this->basePath = $basePath;
19 26
    }
20
21
    /**
22
     * Return the cache key of a component.
23
     *
24
     * @param string $component The name of the component.
25
     * @return string Returns the unique key of the component.
26
     */
27 26
    public function cacheKey($component) {
28 26
        $subpath = $this->componentPath($component, false);
29
30 26
        if (empty($subpath)) {
31
            return null;
32
        } else {
33 26
            return str_replace(DIRECTORY_SEPARATOR, '.', $subpath);
34
        }
35
    }
36
37
    /**
38
     * Return the template source of a component.
39
     *
40
     * @param string $component The name of the component.
41
     * @return string Returns the template source of the component.
0 ignored issues
show
Documentation introduced by
Should the return type not be null|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.

Loading history...
42
     */
43 25
    public function load($component) {
44 25
        $path = $this->componentPath($component, true);
45
46 25
        if (empty($path)) {
47
            return null;
48
        } else {
49 25
            return file_get_contents($path);
50
        }
51
    }
52
53 26
    private function componentPath($component, $full = true) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
54 26
        $subpath = str_replace('.', DIRECTORY_SEPARATOR, $component);
55
56
        do {
57 26
            $path = "{$this->basePath}/$subpath.html";
58 26
            if (file_exists($path)) {
59 26
                return $full ? $path : $subpath;
60
            }
61
62
            if ($pos = strrpos($subpath, DIRECTORY_SEPARATOR)) {
63
                $subpath = substr($subpath, 0, $pos - 1);
64
            } else {
65
                break;
66
            }
67
68
        } while (!empty($subpath));
69
70
        return '';
71
    }
72
73
    /**
74
     * Get the basePath.
75
     *
76
     * @return string Returns the basePath.
77
     */
78
    public function getBasePath() {
79
        return $this->basePath;
80
    }
81
82
    /**
83
     * Set the basePath.
84
     *
85
     * @param string $basePath
86
     * @return $this
87
     */
88
    public function setBasePath($basePath) {
89
        $this->basePath = $basePath;
90
        return $this;
91
    }
92
}
93