Passed
Push — master ( 30cd5d...5bfcaa )
by Andreas
23:09
created

midcom_db_style::id_from_path()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 7
nop 2
dl 0
loc 37
ccs 0
cts 24
cp 0
crap 30
rs 9.2408
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.db
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * MidCOM level replacement for the Midgard Style record with framework support.
11
 *
12
 * The uplink is the owning Style.
13
 *
14
 * @property string $name Path name of the style
15
 * @property integer $up Style the style is under
16
 * @package midcom.db
17
 */
18
class midcom_db_style extends midcom_core_dbaobject
19
{
20
    public $__midcom_class_name__ = __CLASS__;
21
    public $__mgdschema_class_name__ = 'midgard_style';
22
23
    /**
24
     * Returns the path of the style described by $id.
25
     *
26
     * @param int $id    Style id to look up path for
27
     */
28
    public static function path_from_id(int $id) : string
29
    {
30
        static $path_cache = [];
31
        if (isset($path_cache[$id])) {
32
            return $path_cache[$id];
33
        }
34
        // Construct the path
35
        $path_parts = [];
36
        $original_id = $id;
37
38
        try {
39
            while (($style = new self($id))) {
40
                $path_parts[] = $style->name;
41
                $id = $style->up;
42
43
                if ($style->up == 0) {
44
                    // Toplevel style
45
                    break;
46
                }
47
            }
48
        } catch (midcom_error $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
49
        }
50
51
        $path_parts = array_reverse($path_parts);
52
53
        $path_cache[$original_id] = '/' . implode('/', $path_parts);
54
55
        return $path_cache[$original_id];
56
    }
57
58
    /**
59
     * Returns the id of the style described by $path.
60
     *
61
     * Note: $path already includes the element name, so $path looks like
62
     * "/rootstyle/style/style/element".
63
     *
64
     * @todo complete documentation
65
     * @param string $path      The path to retrieve
66
     * @param int $rootstyle    ???
67
     * @return    int ID of the matching style or 0
68
     */
69
    public static function id_from_path($path, $rootstyle = 0) : int
70
    {
71
        static $cached = [];
72
73
        $cache_key = $rootstyle . '::' . $path;
74
75
        if (array_key_exists($cache_key, $cached)) {
76
            return $cached[$cache_key];
77
        }
78
79
        $path = preg_replace("/^\/(.*)/", "$1", $path); // leading "/"
80
        $cached[$cache_key] = 0;
81
        $current_style = 0;
82
83
        $path_array = array_filter(explode('/', $path));
84
        if (!empty($path_array)) {
85
            $current_style = $rootstyle;
86
        }
87
88
        foreach ($path_array as $path_item) {
89
            $mc = midgard_style::new_collector('up', $current_style);
90
            $mc->set_key_property('guid');
91
            $mc->add_value_property('id');
92
            $mc->add_constraint('name', '=', $path_item);
93
            $mc->execute();
94
            $styles = $mc->list_keys();
95
96
            if (!empty($styles)) {
97
                $style_guid = key($styles);
98
                $current_style = $mc->get_subkey($style_guid, 'id');
99
                midcom::get()->cache->content->register($style_guid);
100
            }
101
        }
102
103
        $cached[$cache_key] = $current_style;
104
105
        return $cached[$cache_key];
106
    }
107
}
108