Passed
Push — master ( a2ae32...b22f9f )
by Andreas
19:42
created

midcom_response_styled::render()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 0
dl 0
loc 16
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * Wrapper for style(engine)d responses
13
 *
14
 * @package midcom
15
 */
16
class midcom_response_styled extends Response
17
{
18
    private string $root_element;
19
20
    private midcom_core_context $context;
21
22 275
    public function __construct(midcom_core_context $context, string $root_element = 'ROOT')
23
    {
24 275
        $this->set_context($context);
25 275
        $this->root_element = $root_element;
26 275
        parent::__construct($this->render());
27
    }
28
29 275
    private function render() : string
30
    {
31 275
        midcom::get()->style->enter_context($this->context);
32 275
        ob_start();
33 275
        if (midcom::get()->skip_page_style) {
34 258
            $this->context->show();
35
        } else {
36 17
            if ($this->context->id == 0) {
37
                // Let metadata service add its meta tags
38
                midcom::get()->metadata->populate_meta_head();
39
            }
40 17
            midcom_show_style($this->root_element);
41
        }
42
43 275
        midcom::get()->style->leave_context();
44 275
        return ob_get_clean();
45
    }
46
47 275
    private function set_context(midcom_core_context $context)
48
    {
49 275
        if (   $context->get_key(MIDCOM_CONTEXT_PERMALINKGUID) === null
50 275
            || $context->get_key(MIDCOM_CONTEXT_PAGETITLE) == '') {
51
            // Retrieve Metadata
52 257
            $nav = new midcom_helper_nav();
53 257
            if ($nav->get_current_leaf() === null) {
54 232
                $meta = $nav->get_node($nav->get_current_node());
55
            } else {
56 25
                $meta = $nav->get_leaf($nav->get_current_leaf());
57
            }
58
59 257
            if ($meta) {
60 256
                if ($context->get_key(MIDCOM_CONTEXT_PERMALINKGUID) === null) {
61 227
                    $context->set_key(MIDCOM_CONTEXT_PERMALINKGUID, $meta[MIDCOM_NAV_GUID]);
62
                }
63 256
                if ($context->get_key(MIDCOM_CONTEXT_PAGETITLE) == '') {
64 140
                    $context->set_key(MIDCOM_CONTEXT_PAGETITLE, $meta[MIDCOM_NAV_NAME]);
65
                }
66
            }
67
        }
68 275
        $this->context = $context;
69
    }
70
}
71