Completed
Push — master ( e4bfce...b9fe45 )
by Vladimir
02:19
created

ContentItem::getContent()   C

Complexity

Conditions 7
Paths 17

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 7.0541

Importance

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