Name   A
last analyzed

↳ Parent: Project

Coupling/Cohesion

Components 1
Dependencies 3

Complexity

Total Complexity 21

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 192
ccs 61
cts 61
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setEngine() 0 6 1
A getEngine() 0 4 1
A setName() 0 20 3
A getName() 0 4 1
A setFolder() 0 6 1
A getFolder() 0 4 1
A setFile() 0 17 3
A getFile() 0 4 1
A doesPathExist() 0 4 1
A getDefaultDirectory() 0 13 2
B getPath() 0 14 5
1
<?php
2
3
namespace League\Plates\Template;
4
5
use League\Plates\Engine;
6
use LogicException;
7
8
/**
9
 * A template name.
10
 */
11
class Name
12
{
13
    /**
14
     * Instance of the template engine.
15
     * @var Engine
16
     */
17
    protected $engine;
18
19
    /**
20
     * The original name.
21
     * @var string
22
     */
23
    protected $name;
24
25
    /**
26
     * The parsed template folder.
27
     * @var Folder
28
     */
29
    protected $folder;
30
31
    /**
32
     * The parsed template filename.
33
     * @var string
34
     */
35
    protected $file;
36
37
    /**
38
     * Create a new Name instance.
39
     * @param Engine $engine
40
     * @param string $name
41
     */
42 200
    public function __construct(Engine $engine, $name)
43
    {
44 200
        $this->setEngine($engine);
45 200
        $this->setName($name);
46 188
    }
47
48
    /**
49
     * Set the engine.
50
     * @param  Engine $engine
51
     * @return Name
52
     */
53 200
    public function setEngine(Engine $engine)
54
    {
55 200
        $this->engine = $engine;
56
57 200
        return $this;
58
    }
59
60
    /**
61
     * Get the engine.
62
     * @return Engine
63
     */
64 4
    public function getEngine()
65
    {
66 4
        return $this->engine;
67
    }
68
69
    /**
70
     * Set the original name and parse it.
71
     * @param  string $name
72
     * @return Name
73
     */
74 200
    public function setName($name)
75
    {
76 200
        $this->name = $name;
77
78 200
        $parts = explode('::', $this->name);
79
80 200
        if (count($parts) === 1) {
81 176
            $this->setFile($parts[0]);
82 110
        } elseif (count($parts) === 2) {
83 20
            $this->setFolder($parts[0]);
84 20
            $this->setFile($parts[1]);
85 8
        } else {
86 4
            throw new LogicException(
87 4
                'The template name "' . $this->name . '" is not valid. ' .
88 2
                'Do not use the folder namespace separator "::" more than once.'
89 2
            );
90
        }
91
92 188
        return $this;
93
    }
94
95
    /**
96
     * Get the original name.
97
     * @return string
98
     */
99 20
    public function getName()
100
    {
101 20
        return $this->name;
102
    }
103
104
    /**
105
     * Set the parsed template folder.
106
     * @param  string $folder
107
     * @return Name
108
     */
109 20
    public function setFolder($folder)
110
    {
111 20
        $this->folder = $this->engine->getFolders()->get($folder);
112
113 20
        return $this;
114
    }
115
116
    /**
117
     * Get the parsed template folder.
118
     * @return string
119
     */
120 16
    public function getFolder()
121
    {
122 16
        return $this->folder;
123
    }
124
125
    /**
126
     * Set the parsed template file.
127
     * @param  string $file
128
     * @return Name
129
     */
130 196
    public function setFile($file)
131
    {
132 196
        if ($file === '') {
133 8
            throw new LogicException(
134 8
                'The template name "' . $this->name . '" is not valid. ' .
135 4
                'The template name cannot be empty.'
136 4
            );
137
        }
138
139 188
        $this->file = $file;
140
141 188
        if (!is_null($this->engine->getFileExtension())) {
142 184
            $this->file .= '.' . $this->engine->getFileExtension();
143 92
        }
144
145 188
        return $this;
146
    }
147
148
    /**
149
     * Get the parsed template file.
150
     * @return string
151
     */
152 16
    public function getFile()
153
    {
154 16
        return $this->file;
155
    }
156
157
    /**
158
     * Resolve template path.
159
     * @return string
160
     */
161 144
    public function getPath()
162
    {
163 144
        if (is_null($this->folder)) {
164 136
            return $this->getDefaultDirectory() . DIRECTORY_SEPARATOR . $this->file;
165
        }
166
167 8
        $path = $this->folder->getPath() . DIRECTORY_SEPARATOR . $this->file;
168
169 8
        if (!is_file($path) and $this->folder->getFallback() and is_file($this->getDefaultDirectory() . DIRECTORY_SEPARATOR . $this->file)) {
170 4
            $path = $this->getDefaultDirectory() . DIRECTORY_SEPARATOR . $this->file;
171 2
        }
172
173 8
        return $path;
174
    }
175
176
    /**
177
     * Check if template path exists.
178
     * @return boolean
179
     */
180 120
    public function doesPathExist()
181
    {
182 120
        return is_file($this->getPath());
183
    }
184
185
    /**
186
     * Get the default templates directory.
187
     * @return string
188
     */
189 140
    protected function getDefaultDirectory()
190
    {
191 140
        $directory = $this->engine->getDirectory();
192
193 140
        if (is_null($directory)) {
194 4
            throw new LogicException(
195 4
                'The template name "' . $this->name . '" is not valid. '.
196 2
                'The default directory has not been defined.'
197 2
            );
198
        }
199
200 136
        return $directory;
201
    }
202
}
203