Completed
Push — master ( 808f8c...952fde )
by Vladimir
11s
created

ReadableDocument::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2.0185
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 123
    public function __construct(File $file)
38
    {
39 123
        $this->metadata = new NullableArray();
40 123
        $this->file = $file;
41
42 123
        if (!$this->noReadOnConstructor)
43
        {
44 117
            $this->readContent();
45
        }
46 112
    }
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
     * @return string
60
     */
61 52
    public function getIndexName()
62
    {
63 52
        return $this->getRelativeFilePath();
64
    }
65
66
    /**
67
     * When a document is compiled, all of its internals are finished being configured.
68
     */
69 10
    final public function compile()
70
    {
71 10
        if ($this->compiled)
72
        {
73
            return;
74
        }
75
76 10
        $this->beforeCompile();
77
78 10
        $this->compiled = true;
79 10
    }
80
81
    /**
82
     * Determine whether or not this document has been compiled.
83
     *
84
     * @return bool
85
     */
86
    final protected function isCompiled()
87
    {
88
        return $this->compiled;
89
    }
90
91
    /**
92
     * Get the original File object given to this document.
93
     *
94
     * @return File
95
     */
96 2
    final public function getFile()
97
    {
98 2
        return $this->file;
99
    }
100
101
    /**
102
     * Get the relative path to the file, with respect to the site root.
103
     *
104
     * @return string
105
     */
106 103
    final public function getRelativeFilePath()
107
    {
108 103
        return $this->file->getRelativeFilePath();
109
    }
110
111
    /**
112
     * Get the extension of the file.
113
     *
114
     * @return string
115
     */
116 71
    final public function getExtension()
117
    {
118 71
        return $this->file->getExtension();
119
    }
120
121
    /**
122
     * Get the name of the file without the extension.
123
     *
124
     * @return string
125
     */
126 4
    final public function getBasename()
127
    {
128 4
        return $this->file->getBasename();
129
    }
130
131
    /**
132
     * Get the absolute path to the file.
133
     *
134
     * @return string
135
     */
136 14
    final public function getAbsoluteFilePath()
137
    {
138 14
        return $this->file->getAbsolutePath();
139
    }
140
141
    /**
142
     * Get the name of the file with its extension.
143
     *
144
     * @return string
145
     */
146
    final public function getFilename()
147
    {
148
        return $this->file->getFilename();
149
    }
150
151
    /**
152
     * Read the contents of this file and handle all of the necessary processing/setup for this document.
153
     */
154 123
    final public function readContent()
155
    {
156 123
        $beforeEvent = $this->beforeReadContents();
157 123
        $actualEvent = $this->readContents($beforeEvent);
158 113
        $this->afterReadContents($actualEvent);
159 112
    }
160
161
    /**
162
     * Prepare the Document so it can handle the data that's about to be read in.
163
     *
164
     * @return mixed any information that will be passed to the readContents() method
165
     */
166 6
    protected function beforeReadContents()
167
    {
168 6
        return null;
169
    }
170
171
    /**
172
     * Read the contents of the file and store the information internally **only**.
173
     *
174
     * This method must **not** handle any parsing or processing of the file's content. That should be handled by the
175
     * `afterReadContents()` method.
176
     *
177
     * @param mixed $mixed any information returned from the beforeReadContents() method
178
     *
179
     * @throws \RuntimeException when the file cannot be read
180
     *
181
     * @return mixed
182
     */
183
    abstract protected function readContents($mixed);
184
185
    /**
186
     * After the Document's content has been read, process the it and handle any parsing that's needed.
187
     *
188
     * @param mixed $mixed any information returned from the readContents() method
189
     */
190 6
    protected function afterReadContents($mixed)
191
    {
192 6
    }
193
194
    /**
195
     * Functionality that needs to take place before this document is considered "compiled," meaning everything has been
196
     * processed, configured, and built.
197
     */
198
    protected function beforeCompile()
199
    {
200
    }
201
}
202