AbstractFileBasedTemplate::getContent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-10-07
5
 */
6
namespace Net\Bazzline\Component\Template;
7
8
use InvalidArgumentException;
9
use RuntimeException;
10
11
abstract class AbstractFileBasedTemplate extends AbstractTemplate
12
{
13
    /** @var null|string */
14
    private $filePath;
15
16
    /**
17
     * @param array $variables
18
     * @param null|string $filePath
19
     * @throws InvalidArgumentException
20
     * @throws RuntimeException
21
     */
22
    public function __construct($variables = array(), $filePath = null)
23
    {
24
        parent::__construct($variables);
25
26
        $this->setFilePathIfProvided($filePath);
27
    }
28
29
    /**
30
     * @param array $variables
31
     * @param null $filePath
32
     * @return string
33
     * @throws InvalidArgumentException
34
     * @throws RuntimeException
35
     */
36
    public function __invoke($variables = array(), $filePath = null)
37
    {
38
        $this->setFilePathIfProvided($filePath);
39
40
        return parent::__invoke($variables);
41
    }
42
43
    /**
44
     * @param string $filePath
45
     * @throws InvalidArgumentException
46
     * @throws RuntimeException
47
     */
48
    public function setFilePath($filePath)
49
    {
50
        $this->throwRuntimeExceptionIfFilePathIsInvalid($filePath);
51
52
        if (!is_readable($filePath)) {
53
            throw new InvalidArgumentException(
54
                'file path: "' . $filePath . '" is not readable'
55
            );
56
        }
57
58
        $this->filePath = $filePath;
59
    }
60
61
    /**
62
     * @return string
63
     * @throws RuntimeException
64
     */
65
    protected function getContent()
66
    {
67
        $filePath = $this->getFilePath();
68
69
        $this->throwRuntimeExceptionIfFilePathIsInvalid($filePath);
70
71
        $content = file_get_contents($filePath);
72
73
        if ($content === false) {
74
            throw new RuntimeException(
75
                'could not read content from "' . $filePath . '"'
76
            );
77
        }
78
79
        return $content;
80
    }
81
82
    /**
83
     * @return null|string
84
     */
85
    protected function getFilePath()
86
    {
87
        return $this->filePath;
88
    }
89
90
    /**
91
     * @param string $filePath
92
     * @throws RuntimeException
93
     */
94
    protected function throwRuntimeExceptionIfFilePathIsInvalid($filePath)
95
    {
96
        if (is_null($filePath)) {
97
            throw new RuntimeException(
98
                'file path must be a valid string'
99
            );
100
        }
101
102
        if (!file_exists($filePath)) {
103
            throw new InvalidArgumentException(
104
                'file path: "' . $filePath . '" does not exist'
105
            );
106
        }
107
108
        if (!is_readable($filePath)) {
109
            throw new RuntimeException(
110
                'file path: "' . $filePath . '" is not readable'
111
            );
112
        }
113
    }
114
115
    /**
116
     * @param null|string $filePath
117
     * @throws RuntimeException
118
     */
119
    protected function setFilePathIfProvided($filePath = null)
120
    {
121
        if (!is_null($filePath)) {
122
            $this->setFilePath($filePath);
123
        }
124
    }
125
}