Completed
Push — master ( 6130d0...57338d )
by Dev
24:32 queued 11:23
created

PageExtendedMainContentTrait   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 53
dl 0
loc 165
ccs 0
cts 30
cp 0
rs 10
c 4
b 0
f 0
wmc 28

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getChapeau() 0 5 1
A removeHtmlComments() 0 3 1
A validateOtherProperties() 0 10 3
A __call() 0 19 5
A manageMainContent() 0 16 6
A getEmc() 0 4 2
A getOtherPropertiesParsed() 0 7 3
A mainContentIsMarkdown() 0 3 2
A setOtherProperties() 0 6 1
A getOtherProperties() 0 3 1
A getSubtitle() 0 3 1
A getReadableContent() 0 10 1
A setMainContentIsMarkdown() 0 5 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Symfony\Component\Validator\Constraints as Assert;
6
use Symfony\Component\Validator\Context\ExecutionContextInterface;
7
use Symfony\Component\Yaml\Exception\ParseException;
8
use Symfony\Component\Yaml\Yaml;
9
10
trait PageExtendedMainContentTrait
11
{
12
    protected $chapeau;
13
14
    protected $readableContent;
15
16
    /**
17
     * @ORM\Column(type="boolean", nullable=true)
18
     */
19
    protected $mainContentIsMarkdown;
20
21
    /**
22
     * @ORM\Column(type="text", nullable=true)
23
     */
24
    protected $otherProperties;
25
26
    protected $otherPropertiesParsed;
27
28
    abstract public function getMainContent(): ?string;
29
30
    public static function removeHtmlComments(string $content)
31
    {
32
        return preg_replace('/<!--(.|\s)*?-->/', '', $content);
33
    }
34
35
    protected function manageMainContent()
36
    {
37
        $content = (string) $this->getMainContent();
38
        $content = explode('<!--break-->', $content);
39
40
        $this->chapeau = isset($content[1]) ? self::removeHtmlComments($content[0]) : null;
41
        $this->readableContent = \PiedWeb\CMSBundle\Twig\AppExtension::convertMarkdownImage(
42
            self::removeHtmlComments(isset($content[1]) ? $content[1] : $content[0])
43
        );
44
45
        if ($this->mainContentIsMarkdown) {
46
            if ($this->chapeau) {
47
                $this->chapeau = '{% filter markdown %}'.$this->chapeau.'{% endfilter %}';
48
            }
49
            if ($this->readableContent) {
50
                $this->readableContent = '{% filter markdown %}'.$this->readableContent.'{% endfilter %}';
51
            }
52
        }
53
    }
54
55
    public function getReadableContent()
56
    {
57
        $this->manageMainContent();
58
        /* Disable cache because it's generate an error with StaticBundle
59
        if (null === $this->readableContent) {
60
            $this->manageMainContent();
61
        }
62
        /**/
63
64
        return $this->readableContent;
65
    }
66
67
    public function getChapeau()
68
    {
69
        $this->manageMainContent();
70
71
        return $this->chapeau;
72
    }
73
74
    /**
75
     * Shortcut, will be destroy soon.
76
     */
77
    public function getSubtitle()
78
    {
79
        return $this->getEmc('subtitle');
80
    }
81
82
    public function mainContentIsMarkdown(): bool
83
    {
84
        return null === $this->mainContentIsMarkdown ? false : $this->mainContentIsMarkdown;
85
    }
86
87
    public function setMainContentIsMarkdown(bool $mainContentIsMarkdown): self
88
    {
89
        $this->mainContentIsMarkdown = $mainContentIsMarkdown;
90
91
        return $this;
92
    }
93
94
    public function getOtherProperties()
95
    {
96
        return $this->otherProperties;
97
    }
98
99
    public function setOtherProperties($otherProperties)
100
    {
101
        $this->otherProperties = $otherProperties;
102
        $this->otherPropertiesParsed = null;
103
104
        return $otherProperties;
105
    }
106
107
    /**
108
     * @Assert\Callback
109
     */
110
    public function validateOtherProperties(ExecutionContextInterface $context)
111
    {
112
        if (!empty($this->otherProperties)) {
113
            // ou utiliser yaml_parse
114
            try {
115
                $validate = Yaml::parse($this->otherProperties);
0 ignored issues
show
Unused Code introduced by
The assignment to $validate is dead and can be removed.
Loading history...
116
            } catch (ParseException $exception) {
117
                $context->buildViolation('page.otherProperties.malformed') //'$exception->getMessage())
118
                    ->atPath('otherProperties')
119
                    ->addViolation();
120
            }
121
        }
122
    }
123
124
    protected function getOtherPropertiesParsed($name)
125
    {
126
        if (null === $this->otherPropertiesParsed) {
127
            $this->otherPropertiesParsed = $this->otherProperties ? Yaml::parse($this->otherProperties) : [];
128
        }
129
130
        return $this->otherPropertiesParsed[$name] ?? null;
131
        /*
132
        if (!isset($this->otherPropertiesParsed[$name])) {
133
            throw new \Exception('"'.$name.'" is not defined.');
134
        }
135
136
        return $this->otherPropertiesParsed[$name];
137
        /**/
138
    }
139
140
    /**
141
     * Magic getter for otherProperties.
142
     *
143
     * @param string $method
144
     * @param array  $arguments
145
     *
146
     * @return mixed
147
     */
148
    public function __call($method, $arguments)
149
    {
150
        if ('_action' == $method) {
151
            return; // avoid error with sonata
152
        }
153
        if (preg_match('/^get/', $method)) {
154
            $property = lcfirst(preg_replace('/^get/', '', $method));
155
            if (!property_exists(get_class($this), $property)) {
156
                return $this->getOtherPropertiesParsed($property);
157
            }
158
159
            return $this->$property;
160
        } else {
161
            $vars = array_keys(get_object_vars($this));
162
            if (in_array($method, $vars)) {
163
                return call_user_func_array([$this, 'get'.ucfirst($method)], $arguments);
164
            }
165
166
            return $this->getOtherPropertiesParsed(lcfirst($method));
167
        }
168
    }
169
170
    // To remove next release
171
    public function getEmc($name)
172
    {
173
        if (preg_match('/<!--"'.$name.'"--(.*)--\/-->/sU', $this->getMainContent(), $match)) {
174
            return $match[1];
175
        }
176
    }
177
}
178