Completed
Pull Request — master (#86)
by Vladimir
31:16
created

ReadableDocument   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 2

Test Coverage

Coverage 72%

Importance

Changes 0
Metric Value
wmc 21
lcom 4
cbo 2
dl 0
loc 233
ccs 36
cts 50
cp 0.72
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getContent() 0 4 1
A setContent() 0 4 1
A getIndexName() 0 4 1
A isCompiled() 0 4 1
A compile() 0 11 2
A getMetadata() 0 4 1
A setMetadata() 0 4 1
A getFile() 0 4 1
A getRelativeFilePath() 0 4 1
A getExtension() 0 4 1
A getBasename() 0 4 1
A getAbsoluteFilePath() 0 4 1
A getFilename() 0 4 1
A getLastModified() 0 4 1
A readContent() 0 6 1
A beforeReadContents() 0 4 1
readContents() 0 1 ?
A afterReadContents() 0 3 1
A beforeCompile() 0 3 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\Filesystem\File;
11
use allejo\stakx\Utilities\NullableArray;
12
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
13
14
abstract class ReadableDocument
15
{
16
    /**
17
     * Do not read the file immediately at construction. The object will need to execute the self::refreshFileContent()
18
     * manually at some point.
19
     *
20
     * @var bool
21
     */
22
    protected $noReadOnConstructor = false;
23
24
    /** @var string The content of the document's body. */
25
    protected $bodyContent = '';
26
27
    protected $metadata;
28
    protected $file;
29
30
    private $compiled = false;
31
32
    /**
33
     * ReadableDocument Constructor.
34
     *
35
     * @throws FileNotFoundException when the file given in the constructor does not exist or may not be accessible
36
     */
37 124
    public function __construct(File $file)
38
    {
39 124
        $this->metadata = new NullableArray();
40 124
        $this->file = $file;
41
42 124
        if (!$this->noReadOnConstructor)
43
        {
44 118
            $this->readContent();
45
        }
46 113
    }
47
48
    /**
49
     * Get the contents of this document.
50
     *
51
     * @return string
52
     */
53 3
    public function getContent()
54
    {
55 3
        return $this->bodyContent;
56
    }
57
58
    /**
59
     * Manually set the contents of this document.
60
     *
61
     * @param string $content
62
     */
63
    public function setContent($content)
64
    {
65
        $this->bodyContent = $content;
66
    }
67
68
    /**
69
     * @return string
70
     */
71 53
    public function getIndexName()
72
    {
73 53
        return $this->getRelativeFilePath();
74
    }
75
76
    /**
77
     * When a document is compiled, all of its internals are finished being configured.
78
     */
79 10
    final public function compile()
80
    {
81 10
        if ($this->compiled)
82
        {
83
            return;
84
        }
85
86 10
        $this->beforeCompile();
87
88 10
        $this->compiled = true;
89 10
    }
90
91
    /**
92
     * Determine whether or not this document has been compiled.
93
     *
94
     * @return bool
95
     */
96
    final protected function isCompiled()
97
    {
98
        return $this->compiled;
99
    }
100
101
    /**
102
     * Get the value stored under this key in the file's internal metadata only available to stakx.
103
     *
104
     * @param string $key
105
     *
106
     * @return mixed|null
107
     */
108
    final public function getMetadata($key)
109
    {
110
        return $this->metadata[$key];
111
    }
112
113
    /**
114
     * Set a value in the file's internal metadata only available to stakx.
115
     *
116
     * @param string $key
117
     * @param mixed  $value
118
     *
119
     * @return mixed
120
     */
121
    final public function setMetadata($key, $value)
122
    {
123
        return $this->metadata[$key] = $value;
124
    }
125
126
    /**
127
     * Get the original File object given to this document.
128
     *
129
     * @return File
130
     */
131 2
    final public function getFile()
132
    {
133 2
        return $this->file;
134
    }
135
136
    /**
137
     * Get the relative path to the file, with respect to the site root.
138
     *
139
     * @return string
140
     */
141 104
    final public function getRelativeFilePath()
142
    {
143 104
        return $this->file->getRelativeFilePath();
144
    }
145
146
    /**
147
     * Get the extension of the file.
148
     *
149
     * @return string
150
     */
151 71
    final public function getExtension()
152
    {
153 71
        return $this->file->getExtension();
154
    }
155
156
    /**
157
     * Get the name of the file without the extension.
158
     *
159
     * @return string
160
     */
161 96
    final public function getBasename()
162
    {
163 96
        return $this->file->getBasename();
164
    }
165
166
    /**
167
     * Get the absolute path to the file.
168
     *
169
     * @return string
170
     */
171 14
    final public function getAbsoluteFilePath()
172
    {
173 14
        return $this->file->getAbsolutePath();
174
    }
175
176
    /**
177
     * Get the name of the file with its extension.
178
     *
179
     * @return string
180
     */
181 92
    final public function getFilename()
182
    {
183 92
        return $this->file->getFilename();
184
    }
185
186
    /**
187
     * Gets the last modified time.
188
     *
189
     * @return int The last modified time for the file, in a Unix timestamp
190
     */
191
    final public function getLastModified()
192
    {
193
        return $this->file->getLastModified();
194
    }
195
196
    /**
197
     * Read the contents of this file and handle all of the necessary processing/setup for this document.
198
     */
199 124
    final public function readContent()
200
    {
201 124
        $beforeEvent = $this->beforeReadContents();
202 124
        $actualEvent = $this->readContents($beforeEvent);
203 114
        $this->afterReadContents($actualEvent);
204 113
    }
205
206
    /**
207
     * Prepare the Document so it can handle the data that's about to be read in.
208
     *
209
     * @return mixed any information that will be passed to the readContents() method
210
     */
211 6
    protected function beforeReadContents()
212
    {
213 6
        return null;
214
    }
215
216
    /**
217
     * Read the contents of the file and store the information internally **only**.
218
     *
219
     * This method must **not** handle any parsing or processing of the file's content. That should be handled by the
220
     * `afterReadContents()` method.
221
     *
222
     * @param mixed $mixed any information returned from the beforeReadContents() method
223
     *
224
     * @throws \RuntimeException when the file cannot be read
225
     *
226
     * @return mixed
227
     */
228
    abstract protected function readContents($mixed);
229
230
    /**
231
     * After the Document's content has been read, process the it and handle any parsing that's needed.
232
     *
233
     * @param mixed $mixed any information returned from the readContents() method
234
     */
235 6
    protected function afterReadContents($mixed)
236
    {
237 6
    }
238
239
    /**
240
     * Functionality that needs to take place before this document is considered "compiled," meaning everything has been
241
     * processed, configured, and built.
242
     */
243
    protected function beforeCompile()
244
    {
245
    }
246
}
247