Completed
Pull Request — master (#75)
by Vladimir
02:42
created

ReadableDocument::getLastModified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 1
    public function setContent($content)
64
    {
65 1
        $this->bodyContent = $content;
66 1
    }
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 original File object given to this document.
103
     *
104
     * @return File
105
     */
106 2
    final public function getFile()
107
    {
108 2
        return $this->file;
109
    }
110
111
    /**
112
     * Get the relative path to the file, with respect to the site root.
113
     *
114
     * @return string
115
     */
116 104
    final public function getRelativeFilePath()
117
    {
118 104
        return $this->file->getRelativeFilePath();
119
    }
120
121
    /**
122
     * Get the extension of the file.
123
     *
124
     * @return string
125
     */
126 71
    final public function getExtension()
127
    {
128 71
        return $this->file->getExtension();
129
    }
130
131
    /**
132
     * Get the name of the file without the extension.
133
     *
134
     * @return string
135
     */
136 96
    final public function getBasename()
137
    {
138 96
        return $this->file->getBasename();
139
    }
140
141
    /**
142
     * Get the absolute path to the file.
143
     *
144
     * @return string
145
     */
146 14
    final public function getAbsoluteFilePath()
147
    {
148 14
        return $this->file->getAbsolutePath();
149
    }
150
151
    /**
152
     * Get the name of the file with its extension.
153
     *
154
     * @return string
155
     */
156 92
    final public function getFilename()
157
    {
158 92
        return $this->file->getFilename();
159
    }
160
161
    /**
162
     * Gets the last modified time.
163
     *
164
     * @return int The last modified time for the file, in a Unix timestamp
165
     */
166
    final public function getLastModified()
167
    {
168
        return $this->file->getLastModified();
169
    }
170
171
    /**
172
     * Read the contents of this file and handle all of the necessary processing/setup for this document.
173
     */
174 124
    final public function readContent()
175
    {
176 124
        $beforeEvent = $this->beforeReadContents();
177 124
        $actualEvent = $this->readContents($beforeEvent);
178 114
        $this->afterReadContents($actualEvent);
179 113
    }
180
181
    /**
182
     * Prepare the Document so it can handle the data that's about to be read in.
183
     *
184
     * @return mixed any information that will be passed to the readContents() method
185
     */
186 6
    protected function beforeReadContents()
187
    {
188 6
        return null;
189
    }
190
191
    /**
192
     * Read the contents of the file and store the information internally **only**.
193
     *
194
     * This method must **not** handle any parsing or processing of the file's content. That should be handled by the
195
     * `afterReadContents()` method.
196
     *
197
     * @param mixed $mixed any information returned from the beforeReadContents() method
198
     *
199
     * @throws \RuntimeException when the file cannot be read
200
     *
201
     * @return mixed
202
     */
203
    abstract protected function readContents($mixed);
204
205
    /**
206
     * After the Document's content has been read, process the it and handle any parsing that's needed.
207
     *
208
     * @param mixed $mixed any information returned from the readContents() method
209
     */
210 6
    protected function afterReadContents($mixed)
211
    {
212 6
    }
213
214
    /**
215
     * Functionality that needs to take place before this document is considered "compiled," meaning everything has been
216
     * processed, configured, and built.
217
     */
218
    protected function beforeCompile()
219
    {
220
    }
221
}
222