Completed
Pull Request — v3 (#280)
by Gabriel
14:40 queued 18s
created

Name::setPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 4
cts 7
cp 0.5714
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.3149
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
    const NAMESPACE_DELIMITER = '::';
14
15
    /**
16
     * Hint path delimiter value.
17
     *
18
     * @var string
19
     */
20
    const HINT_PATH_DELIMITER = '::';
21
22
    /**
23
     * Instance of the template engine.
24
     * @var Engine
25
     */
26
    protected $engine;
27
28
    /**
29
     * The original name.
30
     * @var string
31
     */
32
    protected $name;
33
34
    /**
35
     * The parsed namespace
36
     */
37
    protected $namespace;
38
39
    protected $folder;
40
41
    /**
42 100
     * The parsed template path.
43
     * @var string
44 100
     */
45 100
    protected $path;
46 94
47
    /**
48
     * Create a new Name instance.
49
     * @param Engine $engine
50
     * @param string $name
51
     */
52
    public function __construct(Engine $engine, $name)
53 100
    {
54
        $this->setEngine($engine);
55 100
        $this->setName($name);
56
    }
57 100
58
    /**
59
     * Set the engine.
60
     * @param  Engine $engine
61
     * @return Name
62
     */
63
    public function setEngine(Engine $engine)
64 2
    {
65
        $this->engine = $engine;
66 2
67
        return $this;
68
    }
69
70
    /**
71
     * Get the engine.
72
     * @return Engine
73
     */
74 100
    public function getEngine()
75
    {
76 100
        return $this->engine;
77
    }
78 100
79
    /**
80 100
     * Set the original name and parse it.
81 88
     * @param  string $name
82 98
     * @return Name
83 10
     */
84 10
    public function setName($name)
85 8
    {
86 2
        $this->name = $name;
87 2
88
        $parts = explode(static::NAMESPACE_DELIMITER, $this->name);
89 2
90
        if (count($parts) === 1) {
91
            $this->setPath($parts[0]);
92 94
        } elseif (count($parts) === 2) {
93
            $this->setNamespace($parts[0]);
94
            $this->setPath($parts[1]);
95
        } else {
96
            throw new LogicException(
97
                'The template name "' . $this->name . '" is not valid. ' .
98
                'Do not use the folder namespace separator "::" more than once.'
99 10
            );
100
        }
101 10
102
        return $this;
103
    }
104
105
    /**
106
     * Get the original name.
107
     * @return string
108
     */
109 10
    public function getName()
110
    {
111 10
        return $this->name;
112
    }
113 10
114
    /**
115
     * Set the parsed template folder.
116
     * @param  string $namespace
117
     * @return Name
118
     */
119
    public function setNamespace($namespace)
120 8
    {
121
        $this->namespace = $namespace;
122 8
123
        return $this;
124
    }
125
126
    /**
127
     * Get the parsed template folder.
128
     * @return string
129
     */
130 98
    public function getNamespace()
131
    {
132 98
        return $this->namespace;
133 4
    }
134 4
135
    /**
136 4
     * @deprecated
137
     */
138
    public function getFolder()
139 94
    {
140
        if ($this->getNamespace()) {
141 94
            return $this->getEngine()->getFolders()->get($this->getNamespace());
142 92
        }
143 92
        return null;
144
    }
145 94
146
    /**
147
     * Set the parsed template file.
148
     * @param  string $path
149
     * @return Name
150
     */
151
    public function setPath($path)
152 8
    {
153
        if ($path === '') {
154 8
            throw new LogicException(
155
                'The template name "' . $this->name . '" is not valid. ' .
156
                'The template name cannot be empty.'
157
            );
158
        }
159
160
        $this->path = $path;
161 72
162
        return $this;
163 72
    }
164 68
165
    public function getFile()
166
    {
167 4
        $file = $this->path;
168
        if (!is_null($this->getEngine()->getFileExtension())) {
169 4
            $file .= '.'.$this->getEngine()->getFileExtension();
170 2
        }
171 2
        return $file;
172
    }
173 4
174
    /**
175
     * Resolve template path or
176
     * Get the parsed template file.
177
     * @return string
178
     */
179
    public function getPath($resolve = true )
180 60
    {
181
        if ($resolve) {
182 60
            return $this->engine->path($this);
183
        }
184
        return $this->path;
185
    }
186
187
    /**
188
     * Check if template path exists.
189 70
     * @return boolean
190
     */
191 70
    public function doesPathExist()
192
    {
193 70
        return $this->engine->exists($this);
194 2
    }
195
}
196