Passed
Push — master ( 546f84...80d964 )
by Dev
04:01
created

PageExtendedMainContentTrait::manageMainContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Michelf\Markdown;
6
7
trait PageExtendedMainContentTrait
8
{
9
    protected $chapeau;
10
11
    protected $readableContent;
12
13
    /**
14
     * @ORM\Column(type="boolean", nullable=true)
15
     */
16
    protected $mainContentIsMarkdown;
17
18
    public static function removeHtmlComments(string $content)
19
    {
20
        return preg_replace('/<!--(.|\s)*?-->/', '', $content);
21
    }
22
23
    protected function manageMainContent()
24
    {
25
        $content = (string) $this->mainContentIsMarkdown ? Markdown::defaultTransform($this->getMainContent()) : $this->getMainContent();
0 ignored issues
show
Bug introduced by
It seems like getMainContent() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        $content = (string) $this->mainContentIsMarkdown ? Markdown::defaultTransform($this->/** @scrutinizer ignore-call */ getMainContent()) : $this->getMainContent();
Loading history...
26
        $content = explode('<!--break-->', $content);
27
28
        $this->chapeau = isset($content[1]) ? self::removeHtmlComments($content[0]) : null;
29
        $this->readableContent = self::removeHtmlComments(isset($content[1]) ? $content[1] : $content[0]);
30
    }
31
32
    public function getReadableContent()
33
    {
34
        if (null === $this->readableContent) {
35
            $this->manageMainContent();
36
        }
37
38
        return $this->readableContent;
39
    }
40
41
    public function getChapeau()
42
    {
43
        if (null === $this->readableContent) {
44
            $this->manageMainContent();
45
        }
46
47
        return $this->chapeau;
48
    }
49
50
    /**
51
     * Shortcut, will be destroy soon.
52
     */
53
    public function getSubtitle()
54
    {
55
        return $this->getEmc('subtitle');
56
    }
57
58
    public function getEmc($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
    public function getEmc(/** @scrutinizer ignore-unused */ $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        if (preg_match('/<!--"subtitle"--(.*)--\/-->/s', $this->getMainContent(), $match)) {
61
            return $match[1];
62
        }
63
    }
64
65
    public function mainContentIsMarkdown(): bool
66
    {
67
        return null === $this->mainContentIsMarkdown ? false : $this->mainContentIsMarkdown;
68
    }
69
70
    public function setMainContentIsMarkdown(bool $mainContentIsMarkdown): self
71
    {
72
        $this->mainContentIsMarkdown = $mainContentIsMarkdown;
73
74
        return $this;
75
    }
76
}
77