Completed
Push — master ( 2319eb...3c4d84 )
by Vladimir
02:22
created

ReadableDocument   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
wmc 18
lcom 3
cbo 4
dl 0
loc 188
rs 10
c 0
b 0
f 0
ccs 28
cts 40
cp 0.7

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
A getIndexName() 0 4 1
A isCompiled() 0 4 1
A getContent() 0 4 1
A compile() 0 11 2
A getFile() 0 4 1
A getRelativeFilePath() 0 4 1
A getExtension() 0 4 1
A getBasename() 0 4 1
A getAbsoluteFilePath() 0 4 1
A getFilename() 0 4 1
A readContent() 0 6 1
A beforeReadContents() 0 4 1
readContents() 0 1 ?
A afterReadContents() 0 1 1
A beforeCompile() 0 1 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 114
    public function __construct(File $file)
39
    {
40 114
        $filePath = (string)$file;
41
42 114
        if (!fs::exists($filePath))
43
        {
44 2
            throw new FileNotFoundException(null, 0, null, $filePath);
45
        }
46
47 112
        $this->metadata = new NullableArray();
48 112
        $this->file = $file;
49
50 112
        if (!$this->noReadOnConstructor)
51
        {
52 106
            $this->readContent();
53
        }
54 101
    }
55
56
    /**
57
     * Get the contents of this document.
58
     *
59
     * @return string
60
     */
61
    public function getContent()
62
    {
63
        return $this->bodyContent;
64
    }
65
66
    /**
67
     * @return string
68
     */
69 40
    public function getIndexName()
70
    {
71 40
        return $this->getRelativeFilePath();
72
    }
73
74
    /**
75
     * When a document is compiled, all of its internals are finished being configured.
76
     */
77
    final public function compile()
78
    {
79
        if ($this->compiled)
80
        {
81
            return;
82
        }
83
84
        $this->beforeCompile();
85
86
        $this->compiled = true;
87
    }
88
89
    /**
90
     * Determine whether or not this document has been compiled.
91
     *
92
     * @return bool
93
     */
94
    final protected function isCompiled()
95
    {
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 92
    final public function getRelativeFilePath()
115
    {
116 92
        return $this->file->getRelativeFilePath();
117
    }
118
119
    /**
120
     * Get the extension of the file.
121
     *
122
     * @return string
123
     */
124 14
    final public function getExtension()
125
    {
126 14
        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 2
    final public function getAbsoluteFilePath()
145
    {
146 2
        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 112
    final public function readContent()
163
    {
164 112
        $beforeEvent = $this->beforeReadContents();
165 112
        $actualEvent = $this->readContents($beforeEvent);
166 102
        $this->afterReadContents($actualEvent);
167 101
    }
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