Completed
Push — master ( c00a63...b9159b )
by Andreas
18:38
created

midcom_helper_nav_item::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.helper.nav
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
/**
10
 * @property string $guid
11
 * @property mixed $id
12
 * @property string $type
13
 * @property string $name
14
 * @property string $component
15
 * @property string $url
16
 * @property string $relativeurl
17
 * @property string $absoluteurl
18
 * @property string $fullurl
19
 * @property string $permalink
20
 * @property mixed $object
21
 * @property boolean $noentry
22
 * @property int $nodeid
23
 * @package midcom.helper.nav
24
 */
25
abstract class midcom_helper_nav_item
26
{
27
    protected $data;
28
29
    protected $loaded = false;
30
31
    abstract protected function prepare_data() : array;
32
33
    abstract public function is_readable_by($user_id) : bool;
34
35 142
    protected function get_cache() : midcom_services_cache_module_nap
36
    {
37 142
        return midcom::get()->cache->nap;
0 ignored issues
show
Bug introduced by
The property nap does not seem to exist on midcom_services_cache.
Loading history...
38
    }
39
40 304
    public function __get($name)
41
    {
42 304
        $name = $this->translate_name($name);
43 304
        $data = $this->get_data();
44 304
        if (!array_key_exists($name, $data)) {
45
            return null;
46
        }
47 304
        return $data[$name];
48
    }
49
50
    /**
51
     * Magic setter. Also allows setting arbitrary non-constant keys for backward compatibility
52
     *
53
     * @param string $name
54
     * @param mixed $value
55
     */
56 42
    public function __set($name, $value)
57
    {
58 42
        $name = $this->translate_name($name);
59 42
        if (!$this->loaded) {
60
            $this->data = $this->prepare_data();
61
            $this->loaded = true;
62
        }
63 42
        $this->data[$name] = $value;
64 42
    }
65
66 42
    public function  __isset($name)
67
    {
68 42
        $data = $this->get_data();
69 42
        $name = $this->translate_name($name);
70
71 42
        return array_key_exists($name, $data);
72
    }
73
74 304
    private function translate_name(string $name)
75
    {
76 304
        $const = 'MIDCOM_NAV_' . strtoupper($name);
77 304
        if (defined($const)) {
78 304
            $name = constant($const);
79
        }
80 304
        return $name;
81
    }
82
83
    /**
84
     * Checks if the NAP object is visible within the current runtime environment.
85
     * This includes checks for:
86
     *
87
     * - Nonexistent NAP information (null values)
88
     * - Scheduling/Hiding (only on-site)
89
     * - Approval (only on-site)
90
     * - ACL
91
     */
92 138
    public function is_visible_for($user_id) : bool
93
    {
94 138
        if (empty($this->get_data())) {
95 50
            debug_add('Got a null value as napdata, so this object does not have any NAP info, so we cannot display it.');
96 50
            return false;
97
        }
98
99 94
        $ret = true;
100
101 94
        if (is_object($this->object)) {
102
103
            // Check Hiding, Scheduling and Approval
104 84
            if ($this->object->metadata) {
105 84
                $ret = $this->object->metadata->is_object_visible_onsite();
106
            } else {
107
                // For some reason, the metadata for this object could not be retrieved. so we skip
108
                // Approval/Visibility checks.
109
                debug_add("Warning, no Metadata available for the {$this->type} {$this->guid}.", MIDCOM_LOG_INFO);
110
            }
111
        }
112
113 94
        return $ret && $this->is_readable_by($user_id);
114
    }
115
116 343
    public function get_data() : array
117
    {
118 343
        if (!$this->loaded) {
119 135
            $this->data = $this->prepare_data();
120 131
            $this->loaded = true;
121
        }
122 339
        return $this->data;
123
    }
124
}
125