Completed
Pull Request — master (#61)
by Vladimir
03:02
created

ContentItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 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\MarkupEngine\MarkupEngine;
12
use allejo\stakx\MarkupEngine\MarkupEngineManager;
13
use allejo\stakx\Templating\TemplateErrorInterface;
14
15
class ContentItem extends PermalinkFrontMatterDocument implements CollectableItem, TemplateReadyDocument
16
{
17
    use CollectableItemTrait;
18
    use TemplateEngineDependent;
19
20
    /** @var MarkupEngine */
21
    private $markupEngine;
22
23 57
    public function __construct(File $file)
24
    {
25 57
        $this->noReadOnConstructor = true;
26
27 57
        parent::__construct($file);
28 56
    }
29
30
    public function setMarkupEngine(MarkupEngineManager $manager)
31
    {
32
        $this->markupEngine = $manager->getMarkupEngine($this->getExtension());
33
        $this->readContent();
34
    }
35
36
    ///
37
    // Permalink management
38
    ///
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function handleSpecialRedirects()
44
    {
45
        $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...
46
47
        if (isset($fm['redirect_from']))
48
        {
49
            $redirects = $fm['redirect_from'];
50
51
            if (!is_array($redirects))
52
            {
53
                $redirects = [$redirects];
54
            }
55
56
            $this->redirects = array_merge($this->redirects, $redirects);
57
        }
58
    }
59
60
    ///
61
    // Document body transformation
62
    ///
63
64
    /**
65
     * @throws TemplateErrorInterface
66
     *
67
     * @return string
68
     */
69 5
    public function getContent()
70
    {
71 5
        if (!$this->bodyContentEvaluated)
72
        {
73 5
            $this->bodyContent = $this->parseTemplateLanguage($this->bodyContent);
74 5
            $this->bodyContent = $this->markupEngine->parse($this->bodyContent);
75
76
            $this->bodyContentEvaluated = true;
77
        }
78
79
        return (string)$this->bodyContent;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 6 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...
86
    {
87 6
        $whiteListedFunctions = array_merge(self::$whiteListedFunctions, [
88 6
        ]);
89
90
        $jailedFunctions = [
91 6
            'getPageView'   => 'getJailedPageView',
92
            'getCollection' => 'getNamespace',
93
        ];
94
95 6
        return (new JailedDocument($this, $whiteListedFunctions, $jailedFunctions));
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 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...
102
    {
103
        return array_merge($this->getFrontMatter(), [
104
            'content'   => $this->getContent(),
105
            'permalink' => $this->getPermalink(),
106
            'redirects' => $this->getRedirects(),
107
        ]);
108
    }
109
}
110