Completed
Push — master ( 5f7a06...dfa657 )
by Andreas
14:47
created

midcom_response_styled::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 2
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
    /**
19
     *
20
     * @var string
21
     */
22
    private $root_element;
23
24
    /**
25
     *
26
     * @var midcom_core_context
27
     */
28
    private $context;
29
30 268
    public function __construct(midcom_core_context $context, $root_element = 'ROOT')
31
    {
32 268
        parent::__construct();
33 268
        $this->set_context($context);
34 268
        $this->root_element = $root_element;
35 268
        $this->content = $this->render();
36 268
    }
37
38 268
    private function render() : string
39
    {
40 268
        midcom::get()->style->enter_context($this->context);
41 268
        ob_start();
42 268
        if (midcom::get()->skip_page_style) {
43 249
            $this->context->show();
44
        } else {
45 19
            if ($this->context->id == 0) {
46
                // Let metadata service add its meta tags
47
                midcom::get()->metadata->populate_meta_head();
48
            }
49 19
            midcom_show_style($this->root_element);
50
        }
51
52 268
        midcom::get()->style->leave_context();
53 268
        return ob_get_clean();
54
    }
55
56 268
    private function set_context(midcom_core_context $context)
57
    {
58 268
        if (   $context->get_key(MIDCOM_CONTEXT_PERMALINKGUID) === null
59 268
            || $context->get_key(MIDCOM_CONTEXT_PAGETITLE) == '') {
60
            // Retrieve Metadata
61 250
            $nav = new midcom_helper_nav();
62 250
            if ($nav->get_current_leaf() === false) {
0 ignored issues
show
introduced by
The condition $nav->get_current_leaf() === false is always false.
Loading history...
63 225
                $meta = $nav->get_node($nav->get_current_node());
64
            } else {
65 25
                $meta = $nav->get_leaf($nav->get_current_leaf());
66
            }
67
68 250
            if ($meta) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $meta of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
69 248
                if ($context->get_key(MIDCOM_CONTEXT_PERMALINKGUID) === null) {
70 219
                    $context->set_key(MIDCOM_CONTEXT_PERMALINKGUID, $meta[MIDCOM_NAV_GUID]);
71
                }
72 248
                if ($context->get_key(MIDCOM_CONTEXT_PAGETITLE) == '') {
73 139
                    $context->set_key(MIDCOM_CONTEXT_PAGETITLE, $meta[MIDCOM_NAV_NAME]);
74
                }
75
            }
76
        }
77 268
        $this->context = $context;
78 268
    }
79
}
80