Completed
Pull Request — master (#69)
by Vladimir
02:48
created

ReadableDocument::setContent()   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
c 0
b 0
f 0
rs 10
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 1
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 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
     * 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 52
    public function getIndexName()
72
    {
73 52
        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 103
    final public function getRelativeFilePath()
117
    {
118 103
        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 95
    final public function getBasename()
137
    {
138 95
        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 91
    final public function getFilename()
157
    {
158 91
        return $this->file->getFilename();
159
    }
160
161
    /**
162
     * Read the contents of this file and handle all of the necessary processing/setup for this document.
163
     */
164 123
    final public function readContent()
165
    {
166 123
        $beforeEvent = $this->beforeReadContents();
167 123
        $actualEvent = $this->readContents($beforeEvent);
168 113
        $this->afterReadContents($actualEvent);
169 112
    }
170
171
    /**
172
     * Prepare the Document so it can handle the data that's about to be read in.
173
     *
174
     * @return mixed any information that will be passed to the readContents() method
175
     */
176 6
    protected function beforeReadContents()
177
    {
178 6
        return null;
179
    }
180
181
    /**
182
     * Read the contents of the file and store the information internally **only**.
183
     *
184
     * This method must **not** handle any parsing or processing of the file's content. That should be handled by the
185
     * `afterReadContents()` method.
186
     *
187
     * @param mixed $mixed any information returned from the beforeReadContents() method
188
     *
189
     * @throws \RuntimeException when the file cannot be read
190
     *
191
     * @return mixed
192
     */
193
    abstract protected function readContents($mixed);
194
195
    /**
196
     * After the Document's content has been read, process the it and handle any parsing that's needed.
197
     *
198
     * @param mixed $mixed any information returned from the readContents() method
199
     */
200 6
    protected function afterReadContents($mixed)
201
    {
202 6
    }
203
204
    /**
205
     * Functionality that needs to take place before this document is considered "compiled," meaning everything has been
206
     * processed, configured, and built.
207
     */
208
    protected function beforeCompile()
209
    {
210
    }
211
}
212