Completed
Push — master ( 54991b...612227 )
by Dev
13:09
created

PageExtendedMainContentTrait::manageMainContent()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 6
eloc 10
c 3
b 1
f 0
nc 10
nop 0
dl 0
loc 16
ccs 0
cts 11
cp 0
crap 42
rs 9.2222

2 Methods

Rating   Name   Duplication   Size   Complexity  
A PageExtendedMainContentTrait::mainContentIsMarkdown() 0 3 2
A PageExtendedMainContentTrait::setMainContentIsMarkdown() 0 5 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Exception;
6
use Symfony\Component\Validator\Constraints as Assert;
7
use Symfony\Component\Validator\Context\ExecutionContextInterface;
8
use Symfony\Component\Yaml\Exception\ParseException;
9
use Symfony\Component\Yaml\Yaml;
10
11
trait PageExtendedMainContentTrait
12
{
13
    /**
14
     * @ORM\Column(type="boolean", nullable=true)
15
     */
16
    protected $mainContentIsMarkdown;
17
18
    /**
19
     * @ORM\Column(type="text", nullable=true)
20
     */
21
    protected $otherProperties;
22
23
    protected $otherPropertiesParsed;
24
25
    abstract public function getMainContent(): ?string;
26
27
    public function getReadableContent()
28
    {
29
        throw new Exception('You should use MainContentManager or twig function extract("mainContent", page)');
30
    }
31
32
    public function getChapeau()
33
    {
34
        throw new Exception('You should use MainContentManager or twig function extract("chapeau", page)');
35
    }
36
37
    public function mainContentIsMarkdown(): bool
38
    {
39
        return null === $this->mainContentIsMarkdown ? false : $this->mainContentIsMarkdown;
40
    }
41
42
    public function setMainContentIsMarkdown(bool $mainContentIsMarkdown): self
43
    {
44
        $this->mainContentIsMarkdown = $mainContentIsMarkdown;
45
46
        return $this;
47
    }
48
49
    public function getTemplate(): ?string
50
    {
51
        return $this->getOtherProperty('template');
52
    }
53
54
    public function getOtherProperties()
55
    {
56
        return $this->otherProperties;
57
    }
58
59
    public function setOtherProperties($otherProperties)
60
    {
61
        $this->otherProperties = $otherProperties;
62
        $this->otherPropertiesParsed = null;
63
64
        return $otherProperties;
65
    }
66
67
    /**
68
     * @Assert\Callback
69
     */
70
    public function validateOtherProperties(ExecutionContextInterface $context)
71
    {
72
        if (!empty($this->otherProperties)) {
73
            // ou utiliser yaml_parse
74
            try {
75
                Yaml::parse($this->otherProperties);
76
            } catch (ParseException $exception) {
77
                $context->buildViolation('page.otherProperties.malformed') //'$exception->getMessage())
78
                    ->atPath('otherProperties')
79
                    ->addViolation();
80
            }
81
        }
82
    }
83
84
    protected function getOtherProperty($name)
85
    {
86
        if (null === $this->otherPropertiesParsed) {
87
            $this->otherPropertiesParsed = $this->otherProperties ? Yaml::parse($this->otherProperties) : [];
88
        }
89
90
        return $this->otherPropertiesParsed[$name] ?? null;
91
        /*
92
        if (!isset($this->otherPropertiesParsed[$name])) {
93
            throw new \Exception('"'.$name.'" is not defined.');
94
        }
95
96
        return $this->otherPropertiesParsed[$name];
97
        /**/
98
    }
99
100
    /**
101
     * Magic getter for otherProperties.
102
     *
103
     * @param string $method
104
     * @param array  $arguments
105
     *
106
     * @return mixed
107
     */
108
    public function __call($method, $arguments)
109
    {
110
        if ('_action' == $method) {
111
            return; // avoid error with sonata
112
        }
113
114
        if (preg_match('/^get/', $method)) {
115
            $property = lcfirst(preg_replace('/^get/', '', $method));
116
            if (!property_exists(get_class($this), $property)) {
117
                return $this->getOtherProperty($property) ?? $this->getEmc($property);
118
                // todo remove the else next release
119
            }
120
121
            return $this->$property;
122
        } else {
123
            $vars = array_keys(get_object_vars($this));
124
            if (in_array($method, $vars)) {
125
                return call_user_func_array([$this, 'get'.ucfirst($method)], $arguments);
126
            }
127
128
            return $this->getOtherProperty(lcfirst($method)) ?? $this->getEmc($method);
129
        }
130
    }
131
132
    // To remove next release
133
    public function getEmc($name)
134
    {
135
        if (preg_match('/<!--"'.$name.'"--(.*)--\/-->/sU', $this->getMainContent(), $match)) {
136
            return $match[1];
137
        }
138
    }
139
}
140