Passed
Push — master ( 1b6466...f0a80d )
by Andreas
24:57
created

dbloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @package midcom.templating
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/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\templating;
10
11
use midcom_services_cache_module_content;
12
use midcom_core_context;
13
use midgard_style;
14
use midgard_element;
15
use midcom_db_style;
16
use midcom_error_forbidden;
17
18
/**
19
 * Loader class for db-based templates
20
 *
21
 * @package midcom.templating
22
 */
23
class dbloader extends loader
24
{
25
    private $dbstyle;
26
27
    /**
28
     * @var midcom_services_cache_module_content
29
     */
30
    private $cache;
31
32
    public function __construct(midcom_services_cache_module_content $cache)
33
    {
34
        $this->cache = $cache;
35
    }
36
37
    public function get_element(string $name, bool $scope_from_path) : ?string
38
    {
39
        $scope = 0;
40
        if ($scope_from_path) {
41
            // we have full qualified path to element
42
            $matches = [];
43
            if (preg_match("|(.*)/(.*)|", $name, $matches)) {
44
                $styleid = midcom_db_style::id_from_path($matches[1]);
45
                $name = $matches[2];
46
            }
47
            $scope = $styleid ?? $this->dbstyle;
48
        } else {
49
            try {
50
                $root_topic = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC);
51
                if ($root_topic->style) {
52
                    $scope = midcom_db_style::id_from_path($root_topic->style);
53
                }
54
            } catch (midcom_error_forbidden $e) {
55
                $e->log();
56
            }
57
        }
58
59
        if ($scope && $content = $this->get_element_in_styletree($scope, $name)) {
60
            return $content;
61
        }
62
63
        return parent::get_element($name, $scope_from_path);
64
    }
65
66
    /**
67
     * Returns a style element that matches $name and is in style $id.
68
     * It also returns an element if it is not in the given style,
69
     * but in one of its parent styles.
70
     */
71
    private function get_element_in_styletree(int $id, string $name) : ?string
72
    {
73
        $cache_key = $id . '::' . $name;
74
        if (array_key_exists($cache_key, $this->cache)) {
0 ignored issues
show
Bug introduced by
$this->cache of type midcom_services_cache_module_content is incompatible with the type ArrayObject|array expected by parameter $array of array_key_exists(). ( Ignorable by Annotation )

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

74
        if (array_key_exists($cache_key, /** @scrutinizer ignore-type */ $this->cache)) {
Loading history...
75
            return $this->cache[$cache_key];
76
        }
77
78
        $element_mc = midgard_element::new_collector('style', $id);
79
        $element_mc->set_key_property('guid');
80
        $element_mc->add_value_property('value');
81
        $element_mc->add_constraint('name', '=', $name);
82
        $element_mc->execute();
83
84
        if ($element_guid = key($element_mc->list_keys())) {
85
            $this->cache->register($element_guid);
86
            return $this->add_to_cache($cache_key, $element_mc->get_subkey($element_guid, 'value'));
0 ignored issues
show
Bug introduced by
It seems like $element_mc->get_subkey($element_guid, 'value') can also be of type false; however, parameter $content of midcom\templating\loader::add_to_cache() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

86
            return $this->add_to_cache($cache_key, /** @scrutinizer ignore-type */ $element_mc->get_subkey($element_guid, 'value'));
Loading history...
87
        }
88
89
        // No such element on this level, check parents
90
        $style_mc = midgard_style::new_collector('id', $id);
91
        $style_mc->set_key_property('guid');
92
        $style_mc->add_value_property('up');
93
        $style_mc->add_constraint('up', '>', 0);
94
        $style_mc->execute();
95
96
        if ($style_guid = key($style_mc->list_keys())) {
97
            $this->cache->register($style_guid);
98
            $up = $style_mc->get_subkey($style_guid, 'up');
99
            return $this->get_element_in_styletree($up, $name);
0 ignored issues
show
Bug introduced by
It seems like $up can also be of type false; however, parameter $id of midcom\templating\dbload..._element_in_styletree() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

99
            return $this->get_element_in_styletree(/** @scrutinizer ignore-type */ $up, $name);
Loading history...
100
        }
101
102
        $this->cache[$cache_key] = null;
103
        return $this->cache[$cache_key];
104
    }
105
106
    /**
107
     * Initializes style sources
108
     */
109
    public function initialize(midcom_core_context $context, ?string $style)
110
    {
111
        $this->dbstyle = 0;
112
        // get user defined style for component
113
        // style inheritance
114
        // should this be cached somehow?
115
116
        if ($style && $this->dbstyle = midcom_db_style::id_from_path($style)) {
117
            if ($substyle = $context->get_key(MIDCOM_CONTEXT_SUBSTYLE)) {
118
                $chain = explode('/', $substyle);
119
                foreach ($chain as $stylename) {
120
                    if ($_subst_id = midcom_db_style::id_from_path($stylename, $this->dbstyle)) {
121
                        $this->dbstyle = $_subst_id;
122
                    }
123
                }
124
            }
125
        }
126
        parent::initialize($context, $style);
127
    }
128
}
129