Completed
Push — master ( 3a96da...9452fe )
by Vladimir
02:20
created

ContentItem::parseTwig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 2.1481
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Object;
4
5
use allejo\stakx\Engines\MarkdownEngine;
6
use allejo\stakx\Engines\PlainTextEngine;
7
use allejo\stakx\Engines\RstEngine;
8
use allejo\stakx\Manager\TwigManager;
9
10
class ContentItem extends FrontMatterObject implements \JsonSerializable
11
{
12
    /**
13
     * The collection this Content Item belongs to
14
     *
15
     * @var string
16
     */
17
    private $parentCollection;
18
19
    /**
20
     * The Page View that will be used to render this Content Item
21
     *
22
     * @var PageView
23
     */
24
    private $parentPageView;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 29
    public function createJail ()
30
    {
31 29
        return (new JailObject($this, array_merge(self::$whiteListFunctions, array(
32
            'getCollection'
33 29
        )), array('getPageView' => 'getJailedPageView')));
34
    }
35
36 1
    public function getCollection ()
37
    {
38 1
        return $this->parentCollection;
39
    }
40
41 30
    public function setCollection ($collection)
42
    {
43 30
        $this->parentCollection = $collection;
44 30
    }
45
46
    /**
47
     * Return the body of the Content Item parsed as markdown
48
     *
49
     * @return string
50
     */
51 4
    public function getContent ()
52
    {
53 4
        if (!$this->bodyContentEvaluated)
54
        {
55 4
            $this->parseTwig();
56 4
            $this->parseEngines();
57
58 4
            $this->bodyContentEvaluated = true;
59
        }
60
61 4
        return (string)$this->bodyContent;
62
    }
63
64
    /**
65
     * Parse the Twig that is embedded inside a ContentItem's body
66
     */
67 4
    private function parseTwig ()
68
    {
69 4
        $twig = TwigManager::getInstance();
70
71 4
        if ($twig instanceof \Twig_Environment)
72
        {
73
            $template = $twig->createTemplate($this->bodyContent);
74
            $this->bodyContent = $template->render(array());
75
        }
76 4
    }
77
78
    /**
79
     * Parse the ContentItem's body based on the extension of the file
80
     */
81 4
    private function parseEngines ()
82
    {
83 4
        switch ($this->getExtension())
84
        {
85 4
            case "md":
86 3
            case "markdown":
87 1
                $pd = new MarkdownEngine();
88 1
                break;
89
90 3
            case "rst":
91 1
                $pd = new RstEngine();
92 1
                $pd->setIncludePolicy(true, getcwd());
93 1
                break;
94
95
            default:
96 2
                $pd = new PlainTextEngine();
97 2
                break;
98
        }
99
100 4
        $this->bodyContent = $pd->parse($this->bodyContent);
101 4
    }
102
103
    /**
104
     * @return PageView
105
     */
106
    public function &getPageView ()
107
    {
108
        return $this->parentPageView;
109
    }
110
111
    public function getJailedPageView ()
112
    {
113
        return $this->parentPageView->createJail();
114
    }
115
116
    /**
117
     * Set the parent Page View that this Content Item will have be assigned to
118
     *
119
     * @param PageView $pageView
120
     */
121
    public function setPageView (&$pageView)
122
    {
123
        $this->parentPageView = &$pageView;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function jsonSerialize()
130
    {
131
        return array_merge($this->getFrontMatter(), array(
132
            'content' => $this->getContent(),
133
            'permalink' => $this->getPermalink(),
134
            'redirects' => $this->getRedirects()
135
        ));
136
    }
137
}