Passed
Push — master ( 10b09c...54e166 )
by Schlaefer
04:38
created

Page::parseRawData()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 14
nc 6
nop 0
crap 4
1
<?php
2
/**
3
 * The page model
4
 */
5
namespace Phile\Model;
6
7
use Phile\Core\Container;
8
use Phile\Core\Router;
9
use Phile\Core\ServiceLocator;
10
use Phile\Repository\Page as Repository;
11
12
/**
13
 * the Model class for a page
14
 *
15
 * @author  Frank Nägler
16
 * @link    https://philecms.com
17
 * @license http://opensource.org/licenses/MIT
18
 * @package Phile\Model
19
 */
20
class Page
21
{
22
    /**
23
     * @var \Phile\Model\Meta the meta model
24
     */
25
    protected $meta;
26
27
    /**
28
     * @var string the content
29
     */
30
    protected $content;
31
32
    /**
33
     * @var string the path to the original file
34
     */
35
    protected $filePath;
36
37
    /**
38
     * @var string the raw file
39
     */
40
    protected $rawData;
41
42
    /**
43
     * @var \Phile\ServiceLocator\ParserInterface the parser
44
     */
45
    protected $parser;
46
47
    /**
48
     * @var string the pageId of the page
49
     */
50
    protected $pageId;
51
52
    /**
53
     * @var string The content folder, as passed to the class constructor when initiating the object.
54
     */
55
    protected $contentFolder;
56
57
    /**
58
     * @var string content extension
59
     */
60
    protected $contentExtension;
61
62
    /** @var Repository */
63
    protected $repository;
64
65
    /**
66
     * the constructor
67
     *
68
     * @param $filePath
69
     * @param string   $folder
70
     */
71 19
    public function __construct($filePath, $folder = null)
72
    {
73 19
        $settings = Container::getInstance()->get('Phile_Config')->toArray();
74 19
        $this->contentFolder = $folder ?: $settings['content_dir'];
75 19
        $this->contentExtension = $settings['content_ext'];
76 19
        $this->setFilePath($filePath);
77
78
        /**
79
         * @triggerEvent before_load_content this event is triggered before the content is loaded
80
         *
81
         * @param            string filePath the path to the file
82
         * @param \Phile\Model\Page page     the page model
83
         */
84 19
        Container::getInstance()->get('Phile_EventBus')->trigger(
85 19
            'before_load_content',
86 19
            ['filePath' => &$this->filePath, 'page' => &$this]
87
        );
88 19
        if (file_exists($this->filePath)) {
89 19
            $this->rawData = file_get_contents($this->filePath);
90 19
            $this->parseRawData();
91
        }
92
        /**
93
         * @triggerEvent after_load_content this event is triggered after the content is loaded
94
         *
95
         * @param            string filePath the path to the file
96
         * @param            string rawData  the raw data
97
         * @param \Phile\Model\Page page     the page model
98
         */
99 19
        Container::getInstance()->get('Phile_EventBus')->trigger(
100 19
            'after_load_content',
101
            [
102 19
                'filePath' => &$this->filePath,
103 19
                'rawData' => $this->rawData,
104 19
                'page' => &$this
105
            ]
106
        );
107
108 19
        $this->parser = ServiceLocator::getService('Phile_Parser');
109 19
    }
110
111
    /**
112
     * method to get content of page, this method returned the parsed content
113
     *
114
     * @return mixed
115
     */
116 4
    public function getContent()
117
    {
118
        /**
119
         * @triggerEvent before_parse_content this event is triggered before the content is parsed
120
         *
121
         * @param            string content the raw data
122
         * @param \Phile\Model\Page page    the page model
123
         */
124 4
        Container::getInstance()->get('Phile_EventBus')->trigger(
125 4
            'before_parse_content',
126 4
            ['content' => $this->content, 'page' => &$this]
127
        );
128 4
        $content = $this->parser->parse($this->content);
129
        /**
130
         * @triggerEvent after_parse_content this event is triggered after the content is parsed
131
         *
132
         * @param            string content the parsed content
133
         * @param \Phile\Model\Page page    the page model
134
         */
135 4
        Container::getInstance()->get('Phile_EventBus')->trigger(
136 4
            'after_parse_content',
137 4
            ['content' => &$content, 'page' => &$this]
138
        );
139
140 4
        return $content;
141
    }
142
143
    /**
144
     * set content of page
145
     *
146
     * @param string $content
147
     */
148 3
    public function setContent($content)
149
    {
150 3
        $this->content = $content;
151 3
    }
152
153
    /**
154
     * get raw (un-parsed) page content
155
     *
156
     * @return string
157
     */
158 2
    public function getRawContent()
159
    {
160 2
        return $this->content;
161
    }
162
163
    /**
164
     * get the meta model
165
     *
166
     * @return Meta
167
     */
168 9
    public function getMeta()
169
    {
170 9
        return $this->meta;
171
    }
172
173
    /**
174
     * parse the raw content
175
     */
176 19
    protected function parseRawData()
177
    {
178 19
        $this->meta = new Meta($this->rawData);
179 19
        $rawData = trim($this->rawData);
180
        $fences = [
181 19
            'c' => ['open' => '/*', 'close' => '*/'],
182
            'html' => ['open' => '<!--', 'close' => '-->'],
183
            'yaml' => ['open' => '---', 'close' => '---']
184
        ];
185 19
        $content = '';
186 19
        foreach ($fences as $fence) {
187 19
            if (strncmp($fence['open'], $rawData, strlen($fence['open'])) === 0) {
188 18
                $sub = substr($rawData, strlen($fence['open']));
189 18
                list(, $content) = explode($fence['close'], $sub, 2);
190 19
                break;
191
            }
192
        }
193 19
        $this->content = $content ?: $rawData;
194 19
    }
195
196
    /**
197
     * Sets repository this page was retrieved by/belongs to
198
     *
199
     * @param Repository $repository
200
     * @return $this
201
     */
202 19
    public function setRepository(Repository $repository)
203
    {
204 19
        $this->repository = $repository;
205 19
        return $this;
206
    }
207
208
    /**
209
     * Gets repository this page belongs to
210
     *
211
     * @return Repository
212
     */
213 6
    public function getRepository()
214
    {
215 6
        if (!$this->repository) {
216 1
            $this->repository = new Repository();
217
        }
218 6
        return $this->repository;
219
    }
220
221
    /**
222
     * get the title of page from meta information
223
     *
224
     * @return string|null
225
     */
226 6
    public function getTitle()
227
    {
228 6
        return $this->getMeta()->get('title');
229
    }
230
231
    /**
232
     * get Phile $pageId
233
     *
234
     * @param  string $filePath
235
     * @return string
236
     */
237 19
    protected function buildPageId($filePath)
238
    {
239 19
        $pageId = str_replace($this->contentFolder, '', $filePath);
240 19
        $pageId = str_replace($this->contentExtension, '', $pageId);
241 19
        $pageId = str_replace(DIRECTORY_SEPARATOR, '/', $pageId);
242 19
        $pageId = ltrim($pageId, '/');
243 19
        $pageId = preg_replace('/(?<=^|\/)index$/', '', $pageId);
244 19
        return $pageId;
245
    }
246
247
    /**
248
     * get the url of page
249
     *
250
     * @return string
251
     */
252 3 View Code Duplication
    public function getUrl()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
253
    {
254 3
        $container = Container::getInstance();
255 3
        if ($container->has('Phile_Router')) {
256 2
            $router = $container->get('Phile_Router');
257
        } else {
258
            // BC: some old 1.x plugins may use Pages before the core is initialized
259 1
            $router = new Router;
260
        }
261 3
        return $router->urlForPage($this->pageId, false);
262
    }
263
264
    /**
265
     * set the filepath of the page
266
     *
267
     * @param string $filePath
268
     */
269 19
    public function setFilePath($filePath)
270
    {
271 19
        $this->filePath = $filePath;
272 19
        $this->pageId = $this->buildPageId($this->filePath);
273 19
    }
274
275
    /**
276
     * get the filepath of the page
277
     *
278
     * @return string
279
     */
280 3
    public function getFilePath()
281
    {
282 3
        return $this->filePath;
283
    }
284
285
    /**
286
     * get the folder name
287
     *
288
     * @return string
289
     */
290
    public function getFolder()
291
    {
292
        return basename(dirname($this->getFilePath()));
293
    }
294
295 4
    public function getPageId()
296
    {
297 4
        return $this->pageId;
298
    }
299
300
    /**
301
     * get the previous page if one exist
302
     *
303
     * @return null|\Phile\Model\Page
304
     */
305 1
    public function getPreviousPage()
306
    {
307 1
        return $this->getRepository()->getPageOffset($this, -1);
308
    }
309
310
    /**
311
     * get the next page if one exist
312
     *
313
     * @return null|\Phile\Model\Page
314
     */
315 1
    public function getNextPage()
316
    {
317 1
        return $this->getRepository()->getPageOffset($this, 1);
318
    }
319
}
320