Completed
Push — master ( 9fea99...819409 )
by Carlos C
9s
created

Resolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
namespace EngineWorks\Templates;
3
4
class Resolver
5
{
6
    /** @var string */
7
    private $directory;
8
9
    /** @var string */
10
    private $extension;
11
12
    /**
13
     * Templates constructor.
14
     *
15
     * @param string $directory Locations where templates are
16
     * @param string $extension Templates extension
17
     */
18 24
    public function __construct($directory = '', $extension = 'php')
19
    {
20 24
        $this->setDirectory($directory);
21 24
        $this->setExtension($extension);
22 24
    }
23
24
    /**
25
     * @return string
26
     */
27 3
    public function getDirectory()
28
    {
29 3
        return $this->directory;
30
    }
31
32
    /**
33
     * @param string $directory
34
     */
35 24
    public function setDirectory($directory)
36
    {
37 24
        $this->directory = $directory;
38 24
    }
39
40
    /**
41
     * @return string
42
     */
43 3
    public function getExtension()
44
    {
45 3
        return $this->extension;
46
    }
47
48
    /**
49
     * @param string $extension
50
     */
51 24
    public function setExtension($extension)
52
    {
53 24
        $this->extension = $extension;
54 24
    }
55
56
    /**
57
     * Resolve a filename by its friendly name, the real name will be
58
     * directory + template + extension
59
     *
60
     * @param string $template
61
     * @return string
62
     */
63 12
    public function resolve($template)
64
    {
65 12
        if (0 === strpos($template, '../') || false !== strpos($template, '/../')) {
66 3
            throw new \InvalidArgumentException('The filename try to escape the current path');
67
        }
68 9
        return $this->directory . '/' . $template . '.' . $this->extension;
69
    }
70
}
71