Passed
Push — master ( 67760a...0ff2cc )
by Andreas
17:17
created

midcom_helper_nav_item   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 88.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 102
ccs 37
cts 42
cp 0.881
rs 10
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 8 2
A translate_name() 0 7 2
A __isset() 0 6 1
A __get() 0 8 2
A get_cache() 0 3 1
A is_visible() 0 26 6
A get_data() 0 7 2
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 141
    protected function get_cache() : midcom_services_cache_module_nap
36
    {
37 141
        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 325
    public function __get($name)
41
    {
42 325
        $name = $this->translate_name($name);
43 325
        $data = $this->get_data();
44 325
        if (!array_key_exists($name, $data)) {
45
            return null;
46
        }
47 325
        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 93
    public function __set($name, $value)
57
    {
58 93
        $name = $this->translate_name($name);
59 93
        if (!$this->loaded) {
60
            $this->data = $this->prepare_data();
61
            $this->loaded = true;
62
        }
63 93
        $this->data[$name] = $value;
64 93
    }
65
66 39
    public function  __isset($name)
67
    {
68 39
        $data = $this->get_data();
69 39
        $name = $this->translate_name($name);
70
71 39
        return array_key_exists($name, $data);
72
    }
73
74 325
    private function translate_name(string $name)
75
    {
76 325
        $const = 'MIDCOM_NAV_' . strtoupper($name);
77 325
        if (defined($const)) {
78 325
            $name = constant($const);
79
        }
80 325
        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 137
    public function is_visible() : bool
93
    {
94 137
        if (empty($this->get_data())) {
95 10
            debug_add('Got a null value as napdata, so this object does not have any NAP info, so we cannot display it.');
96 10
            return false;
97
        }
98
99 132
        $ret = true;
100
101 132
        if (is_object($this->object)) {
102
            // Check Hiding, Scheduling and Approval
103 126
            if ($this->object->metadata) {
104 126
                $ret = $this->object->metadata->is_object_visible_onsite();
105
            } else {
106
                // For some reason, the metadata for this object could not be retrieved. so we skip
107
                // Approval/Visibility checks.
108
                debug_add("Warning, no Metadata available for the {$this->type} {$this->guid}.", MIDCOM_LOG_INFO);
109
            }
110
        }
111
112 132
        if ($ret) {
113 132
            $user_id = midcom::get()->auth->admin ? false : midcom::get()->auth->acl->get_user_id();
114 132
            return $this->is_readable_by($user_id);
115
        }
116
117
        return $ret;
118
    }
119
120 330
    public function get_data() : array
121
    {
122 330
        if (!$this->loaded) {
123 135
            $this->data = $this->prepare_data();
124 135
            $this->loaded = true;
125
        }
126 330
        return $this->data;
127
    }
128
}
129