Template::__call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
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
use RuntimeException;
9
10
class Template
11
{
12
    /** @var Callables */
13
    private $callables;
14
15
    /** @var Resolver */
16
    private $resolver;
17
18
    /**
19
     * Template constructor.
20
     *
21
     * @param Callables|null $callables If NULL then an empty Callables will be created
22
     * @param Resolver|null $resolver If NULL then a basic Resolver will be created
23
     */
24 16
    public function __construct(Callables $callables = null, Resolver $resolver = null)
25
    {
26 16
        $this->callables = $callables ?? new Callables();
27 16
        $this->resolver = $resolver ?? new Resolver();
28
    }
29
30
    /**
31
     * Retrieve the Callables object
32
     */
33 4
    public function callables(): Callables
34
    {
35 4
        return $this->callables;
36
    }
37
38
    /**
39
     * Retrieve the Resolver object
40
     */
41 3
    public function resolver(): Resolver
42
    {
43 3
        return $this->resolver;
44
    }
45
46
    /**
47
     * Magic method to export calls to $this into calls to Callables
48
     *
49
     * @param string $name
50
     * @param array<mixed> $arguments
51
     * @return string
52
     */
53 1
    public function __call(string $name, array $arguments): string
54
    {
55 1
        return $this->callables->call($name, $arguments);
56
    }
57
58
    /**
59
     * Retrieve if a resolved file is a valid template
60
     * If return FALSE then the $errorMessage variable is populated
61
     */
62 11
    public function isValidTemplateFilename(string $templateFilename, string &$errorMessage = ''): bool
63
    {
64 11
        if (! file_exists($templateFilename)) {
65 2
            $errorMessage = "Template $templateFilename does not exists";
66 2
            return false;
67
        }
68 9
        if (! is_readable($templateFilename)) {
69 1
            $errorMessage = "Template $templateFilename is not readable";
70 1
            return false;
71
        }
72 8
        if (is_dir($templateFilename)) {
73 1
            $errorMessage = "Template $templateFilename is a directory";
74 1
            return false;
75
        }
76 7
        return true;
77
    }
78
79
    /**
80
     * Fetch and return the content of a templates passing the specified variables.
81
     * Inside the template the variable $this refer to exactly this template object
82
     *
83
     * @param array<string, mixed> $templateVariables
84
     */
85 8
    public function fetch(string $templateName, array $templateVariables = []): string
86
    {
87 8
        $templateName = $this->resolver->resolve($templateName);
88 8
        $errorMessage = '';
89 8
        if (! $this->isValidTemplateFilename($templateName, $errorMessage)) {
90 1
            throw new InvalidArgumentException($errorMessage);
91
        }
92 7
        if (! ob_start()) {
93
            throw new RuntimeException('Cannot create a new buffer');
94
        }
95
        // as we are using EXTR_OVERWRITE lets remove $templateName if set
96 7
        unset($templateVariables['templateName']);
97 7
        extract($templateVariables, EXTR_OVERWRITE);
98 7
        require $templateName;
99 7
        return (string) ob_get_clean();
100
    }
101
}
102