Resolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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