Raw   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 67
c 2
b 0
f 0
dl 0
loc 165
rs 10
wmc 24

14 Methods

Rating   Name   Duplication   Size   Complexity  
A parseToc() 0 17 3
A convertMarkdownImage() 0 22 3
A parse() 0 9 2
A getChapeau() 0 5 1
A render() 0 10 2
A applyRendering() 0 7 3
A getIntro() 0 5 1
A getContent() 0 5 1
A __construct() 0 5 1
A getBody() 0 5 2
A getToc() 0 5 1
A parseContentAfterRendering() 0 3 1
A getPostContent() 0 5 1
A parseContentBeforeRendering() 0 9 2
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service\PageMainContentManager;
4
5
use PiedWeb\CMSBundle\Entity\PageInterface;
6
use PiedWeb\CMSBundle\Service\App;
7
use PiedWeb\CMSBundle\Utils\HtmlBeautifer;
8
use TOC\MarkupFixer;
9
use TOC\TocGenerator;
10
use Twig\Environment as Twig;
11
12
// TODO remove APP and remove PAGE to use it on what i want string (like in a twig extension)
13
class Raw implements MainContentManagerInterface
14
{
15
    protected $parts = ['chapeau', 'intro', 'toc', 'content', 'postContent'];
16
17
    /**
18
     * @var Twig
19
     */
20
    protected $twig;
21
22
    /**
23
     * @var App
24
     *          Required only for markdown image render...
25
     *          remove it and set template in page
26
     */
27
    protected $app;
28
29
    protected $page;
30
    protected $chapeau = '';
31
    protected $intro = '';
32
    protected $toc = '';
33
    protected $content = '';
34
    protected $postContent = '';
35
36
    protected $parsed = false;
37
38
    public function __construct(App $app, Twig $twig, PageInterface $page)
39
    {
40
        $this->page = $page;
41
        $this->app = $app->switchCurrentApp($page->getHost());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->app is correct as $app->switchCurrentApp($page->getHost()) targeting PiedWeb\CMSBundle\Service\App::switchCurrentApp() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
42
        $this->twig = $twig;
43
    }
44
45
    protected function parse()
46
    {
47
        if (true === $this->parsed) {
48
            return;
49
        }
50
51
        $this->parseContentBeforeRendering();
52
        $this->applyRendering();
53
        $this->parseContentAfterRendering();
54
    }
55
56
    protected function applyRendering()
57
    {
58
        foreach ($this->parts as $part) {
59
            if ($this->page->mustParseTwig()) {
60
                $this->$part = $this->render($this->$part);
61
            }
62
            $this->$part = HtmlBeautifer::punctuationBeautifer($this->$part);
63
        }
64
    }
65
66
    protected function parseContentBeforeRendering()
67
    {
68
        $originalContent = (string) $this->page->getMainContent();
69
70
        $parsedContent = explode('<!--break-->', $originalContent, 3);
71
72
        $this->chapeau = isset($parsedContent[1]) ? $parsedContent[0] : '';
73
        $this->postContent = $parsedContent[2] ?? '';
74
        $this->content = $parsedContent[1] ?? $parsedContent[0];
75
    }
76
77
    protected function parseContentAfterRendering()
78
    {
79
        $this->parseToc();
80
    }
81
82
    protected function parseToc()
83
    {
84
        $this->content = (new MarkupFixer())->fix($this->content); // this work only on good html
85
86
        // this is a bit crazy
87
        $content = $this->content;
88
        $content = explode('<h', $content, 2);
89
        //var_dump($content);exit;
90
        if (isset($content[1])) {
91
            $this->intro = $content[0];
92
            $this->content = '<h'.$content[1];
93
        } else {
94
            $this->content = $content[0];
95
        }
96
97
        if ($this->page->getOtherProperty('toc')) {
98
            $this->toc = (new TocGenerator())->getHtmlMenu($this->content);
99
        }
100
    }
101
102
    public function getBody(bool $withChapeau = false)
103
    {
104
        $this->parse();
105
106
        return ($withChapeau ? $this->chapeau : '').$this->intro.$this->content.$this->postContent;
107
    }
108
109
    public function getChapeau()
110
    {
111
        $this->parse();
112
113
        return $this->chapeau;
114
    }
115
116
    public function getContent()
117
    {
118
        $this->parse();
119
120
        return $this->content;
121
    }
122
123
    public function getPostContent()
124
    {
125
        $this->parse();
126
127
        return $this->postContent;
128
    }
129
130
    public function getIntro()
131
    {
132
        $this->parse();
133
134
        return $this->intro;
135
    }
136
137
    public function getToc()
138
    {
139
        $this->parse();
140
141
        return $this->toc;
142
    }
143
144
    public function convertMarkdownImage(string $body)
145
    {
146
        preg_match_all('/(?:!\[(.*?)\]\((.*?)\))/', $body, $matches);
147
148
        if (! isset($matches[1])) {
149
            return;
150
        }
151
152
        $nbrMatch = \count($matches[0]);
153
        for ($k = 0; $k < $nbrMatch; ++$k) {
154
            $renderImg = $this->twig->render(
155
                $this->app->getTemplate('/component/_inline_image.html.twig', $this->twig),
156
                [
157
                    //"image_wrapper_class" : "mimg",'
158
                    'image_src' => $matches[2][$k],
159
                    'image_alt' => htmlspecialchars($matches[1][$k]),
160
            ]
161
            );
162
            $body = str_replace($matches[0][$k], $renderImg, $body);
163
        }
164
165
        return $body;
166
    }
167
168
    protected function render($string)
169
    {
170
        if (! $string) {
171
            return '';
172
        }
173
174
        $tmpl = $this->twig->createTemplate(HtmlBeautifer::removeHtmlComments($string));
175
        $string = $tmpl->render(['page' => $this->page]);
176
177
        return $string;
178
    }
179
}
180