Completed
Push — master ( 74371a...6da8c0 )
by Vladimir
02:21
created

ContentItem::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 2
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\Engines\Markdown\MarkdownEngine;
11
use allejo\stakx\Engines\PlainTextEngine;
12
use allejo\stakx\Engines\RST\RstEngine;
13
use allejo\stakx\Service;
14
use allejo\stakx\Templating\TemplateErrorInterface;
15
16
class ContentItem extends PermalinkFrontMatterDocument implements CollectableItem, TemplateReadyDocument
17
{
18
    use CollectableItemTrait;
19
    use TemplateEngineDependent;
20
21
    ///
22
    // Permalink management
23
    ///
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function handleSpecialRedirects()
29
    {
30 1
        $fm = $this->getFrontMatter();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $fm. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
31
32 1
        if (isset($fm['redirect_from']))
33
        {
34
            $redirects = $fm['redirect_from'];
35
36
            if (!is_array($redirects))
37
            {
38
                $redirects = [$redirects];
39
            }
40
41
            $this->redirects = array_merge($this->redirects, $redirects);
42
        }
43 1
    }
44
45
    ///
46
    // Document body transformation
47
    ///
48
49
    /**
50
     * @throws TemplateErrorInterface
51
     *
52
     * @return string
53
     */
54 7
    public function getContent()
55
    {
56 7
        if (!$this->bodyContentEvaluated)
57
        {
58 7
            $this->bodyContent = $this->parseTemplateLanguage($this->bodyContent);
59 7
            $this->parseMarkupLanguage();
60
61 7
            $this->bodyContentEvaluated = true;
62
        }
63
64 7
        return (string)$this->bodyContent;
65
    }
66
67
    /**
68
     * Transform the document's body from a markup language to HTML.
69
     *
70
     * @todo Port this to follow the same pattern as the template engine
71
     */
72 7
    private function parseMarkupLanguage()
73
    {
74 7
        switch ($this->getExtension())
75
        {
76 7
            case 'md':
77 6
            case 'markdown':
78 1
                $pd = new MarkdownEngine();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $pd. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
79 1
                break;
80
81 6
            case 'rst':
82 1
                $pd = new RstEngine();
83 1
                $pd->setIncludePolicy(true, Service::getWorkingDirectory());
84 1
                break;
85
86
            default:
87 5
                $pd = new PlainTextEngine();
88 5
                break;
89
        }
90
91 7
        $this->bodyContent = $pd->parse($this->bodyContent);
92 7
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 25 View Code Duplication
    public function createJail()
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...
98
    {
99 25
        $whiteListedFunctions = array_merge(self::$whiteListedFunctions, [
100 25
        ]);
101
102
        $jailedFunctions = [
103 25
            'getPageView'   => 'getJailedPageView',
104
            'getCollection' => 'getNamespace',
105
        ];
106
107 25
        return (new JailedDocument($this, $whiteListedFunctions, $jailedFunctions));
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 View Code Duplication
    public function jsonSerialize()
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...
114
    {
115
        return array_merge($this->getFrontMatter(), [
116
            'content'   => $this->getContent(),
117
            'permalink' => $this->getPermalink(),
118
            'redirects' => $this->getRedirects(),
119
        ]);
120
    }
121
}
122