Completed
Pull Request — master (#62)
by Vladimir
02:32
created

ReadableDocument::getIndexName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\Filesystem\File;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
use allejo\stakx\Utilities\NullableArray;
13
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
14
15
abstract class ReadableDocument
16
{
17
    /**
18
     * Do not read the file immediately at construction. The object will need to execute the self::refreshFileContent()
19
     * manually at some point.
20
     *
21
     * @var bool
22
     */
23
    protected $noReadOnConstructor = false;
24
25
    /** @var string The content of the document's body. */
26
    protected $bodyContent = '';
27
28
    protected $metadata;
29
    protected $file;
30
31
    private $compiled = false;
32
33
    /**
34
     * ReadableDocument Constructor.
35
     *
36
     * @throws FileNotFoundException When the file given in the constructor does not exist or may not be accessible.
37
     */
38 123
    public function __construct(File $file)
39
    {
40 123
        $this->metadata = new NullableArray();
41 123
        $this->file = $file;
42
43 123
        if (!$this->noReadOnConstructor)
44
        {
45 117
            $this->readContent();
46
        }
47 112
    }
48
49
    /**
50
     * Get the contents of this document.
51
     *
52
     * @return string
53
     */
54 3
    public function getContent()
55
    {
56 3
        return $this->bodyContent;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 52
    public function getIndexName()
63
    {
64 52
        return $this->getRelativeFilePath();
65
    }
66
67
    /**
68
     * When a document is compiled, all of its internals are finished being configured.
69
     */
70 10
    final public function compile()
71
    {
72 10
        if ($this->compiled)
73
        {
74
            return;
75
        }
76
77 10
        $this->beforeCompile();
78
79 10
        $this->compiled = true;
80 10
    }
81
82
    /**
83
     * Determine whether or not this document has been compiled.
84
     *
85
     * @return bool
86
     */
87
    final protected function isCompiled()
88
    {
89
        return $this->compiled;
90
    }
91
92
    /**
93
     * Get the original File object given to this document.
94
     *
95
     * @return File
96
     */
97 2
    final public function getFile()
98
    {
99 2
        return $this->file;
100
    }
101
102
    /**
103
     * Get the relative path to the file, with respect to the site root.
104
     *
105
     * @return string
106
     */
107 103
    final public function getRelativeFilePath()
108
    {
109 103
        return $this->file->getRelativeFilePath();
110
    }
111
112
    /**
113
     * Get the extension of the file.
114
     *
115
     * @return string
116
     */
117 71
    final public function getExtension()
118
    {
119 71
        return $this->file->getExtension();
120
    }
121
122
    /**
123
     * Get the name of the file without the extension.
124
     *
125
     * @return string
126
     */
127 4
    final public function getBasename()
128
    {
129 4
        return $this->file->getBasename();
130
    }
131
132
    /**
133
     * Get the absolute path to the file.
134
     *
135
     * @return string
136
     */
137 14
    final public function getAbsoluteFilePath()
138
    {
139 14
        return $this->file->getAbsolutePath();
140
    }
141
142
    /**
143
     * Get the name of the file with its extension.
144
     *
145
     * @return string
146
     */
147
    final public function getFilename()
148
    {
149
        return $this->file->getFilename();
150
    }
151
152
    /**
153
     * Read the contents of this file and handle all of the necessary processing/setup for this document.
154
     */
155 123
    final public function readContent()
156
    {
157 123
        $beforeEvent = $this->beforeReadContents();
158 123
        $actualEvent = $this->readContents($beforeEvent);
159 113
        $this->afterReadContents($actualEvent);
160 112
    }
161
162
    /**
163
     * Prepare the Document so it can handle the data that's about to be read in.
164
     *
165
     * @return mixed Any information that will be passed to the readContents() method.
166
     */
167 6
    protected function beforeReadContents()
168
    {
169 6
        return null;
170
    }
171
172
    /**
173
     * Read the contents of the file and store the information internally **only**.
174
     *
175
     * This method must **not** handle any parsing or processing of the file's content. That should be handled by the
176
     * `afterReadContents()` method.
177
     *
178
     * @param mixed $mixed Any information returned from the beforeReadContents() method.
179
     *
180
     * @throws \RuntimeException When the file cannot be read.
181
     *
182
     * @return mixed
183
     */
184
    abstract protected function readContents($mixed);
185
186
    /**
187
     * After the Document's content has been read, process the it and handle any parsing that's needed.
188
     *
189
     * @param mixed $mixed Any information returned from the readContents() method.
190
     */
191
    protected function afterReadContents($mixed) {}
192
193
    /**
194
     * Functionality that needs to take place before this document is considered "compiled," meaning everything has been
195
     * processed, configured, and built.
196
     */
197
    protected function beforeCompile() {}
198
}
199