Completed
Push — master ( 3df90d...74371a )
by Vladimir
04:10
created

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