Completed
Push — master ( 39a400...eeaea7 )
by Andreas
29:53 queued 12:12
created

midcom_helper_nav_item::is_visible()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 2
b 0
f 0
nc 7
nop 0
dl 0
loc 25
ccs 11
cts 12
cp 0.9167
crap 6.0208
rs 9.2222
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(string $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 324
    public function __get($name)
41
    {
42 324
        $name = $this->translate_name($name);
43 324
        $data = $this->get_data();
44 324
        if (!array_key_exists($name, $data)) {
45
            return null;
46
        }
47 324
        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 324
    private function translate_name(string $name)
75
    {
76 324
        $const = 'MIDCOM_NAV_' . strtoupper($name);
77 324
        if (defined($const)) {
78 324
            $name = constant($const);
79
        }
80 324
        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() : bool
93
    {
94 138
        if (empty($this->get_data())) {
95 12
            debug_add('Got a null value as napdata, so this object does not have any NAP info, so we cannot display it.');
96 12
            return false;
97
        }
98
99 131
        $ret = true;
100
101 131
        if (is_object($this->object)) {
102
            // Check Hiding, Scheduling and Approval
103 125
            if ($this->object->metadata) {
104 125
                $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 131
        if ($ret && !midcom::get()->auth->admin) {
113 130
            return $this->is_readable_by(midcom::get()->auth->acl->get_user_id());
114
        }
115
116 1
        return $ret;
117
    }
118
119 331
    public function get_data() : array
120
    {
121 331
        if (!$this->loaded) {
122 136
            $this->data = $this->prepare_data();
123 136
            $this->loaded = true;
124
        }
125 331
        return $this->data;
126
    }
127
}
128