Passed
Push — master ( cb0392...409c3f )
by Andreas
09:17
created

midcom_helper_nav_leaf::prepare_data()   B

Complexity

Conditions 8
Paths 30

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 8.2078

Importance

Changes 0
Metric Value
cc 8
eloc 29
nc 30
nop 0
dl 0
loc 52
ccs 23
cts 27
cp 0.8519
crap 8.2078
rs 8.2114
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * @package midcom.helper.nav
11
 */
12
class midcom_helper_nav_leaf extends midcom_helper_nav_item
13
{
14
    private midcom_helper_nav_node $node;
15
16
    private $leafid;
17
18 31
    public function __construct(midcom_helper_nav_node $node, array $data, $leafid, bool $from_cache = false)
19
    {
20 31
        $this->loaded = $from_cache;
21 31
        $this->node = $node;
22 31
        $this->data = $data;
23 31
        $this->leafid = $leafid;
24
    }
25
26 31
    public function is_readable_by(string $user_id) : bool
27
    {
28 31
        return (   empty($this->object)
29 31
                || !$this->guid
30 31
                || !$user_id
31 31
                || midcom::get()->auth->acl->can_do_byguid('midgard:read', $this->guid, $this->object->__midcom_class_name__, $user_id));
32
    }
33
34 1
    public function write_to_cache()
35
    {
36 1
        if (is_object($this->object)) {
37 1
            $this->object = new midcom_core_dbaproxy($this->object->guid, get_class($this->object));
38 1
            $this->get_cache()->put_guid($this->object->guid, $this->get_data());
39
        }
40 1
        return $this->get_data();
41
    }
42
43 31
    protected function prepare_data() : array
44
    {
45 31
        $topic = $this->node->object;
46
47 31
        if (!empty($this->data[MIDCOM_NAV_OBJECT])) {
48 25
            $this->data[MIDCOM_NAV_GUID] = $this->data[MIDCOM_NAV_OBJECT]->guid;
49 6
        } elseif (!empty($this->data[MIDCOM_NAV_GUID])) {
50
            try {
51
                $this->data[MIDCOM_NAV_OBJECT] = midcom::get()->dbfactory->get_object_by_guid($this->data[MIDCOM_NAV_GUID]);
52
            } 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...
53
            }
54
        } else {
55 6
            debug_add("Warning: The leaf {$this->leafid} of topic {$topic->id} does set neither a GUID nor an object.");
56 6
            $this->data[MIDCOM_NAV_GUID] = null;
57 6
            $this->data[MIDCOM_NAV_OBJECT] = null;
58
59
            // Get the pseudo leaf score from the topic
60 6
            if ($score = $topic->get_parameter('midcom.helper.nav.score', "{$topic->id}-{$this->leafid}")) {
61
                $this->data[MIDCOM_NAV_SCORE] = (int) $score;
62
            }
63
        }
64
65 31
        $this->data[MIDCOM_NAV_SORTABLE] ??= true;
66
67
        // Score
68 31
        if (!isset($this->data[MIDCOM_NAV_SCORE])) {
69 31
            if (!empty($this->data[MIDCOM_NAV_OBJECT]->metadata->score)) {
70
                $this->data[MIDCOM_NAV_SCORE] = $this->data[MIDCOM_NAV_OBJECT]->metadata->score;
71
            } else {
72 31
                $this->data[MIDCOM_NAV_SCORE] = 0;
73
            }
74
        }
75
76
        // NAV_NOENTRY Flag
77 31
        $this->data[MIDCOM_NAV_NOENTRY] ??= false;
78
79
        // complete NAV_NAMES where necessary
80 31
        if (trim($this->data[MIDCOM_NAV_NAME]) == '') {
81 24
            $this->data[MIDCOM_NAV_NAME] = midcom::get()->i18n->get_string('unknown', 'midcom');
82
        }
83
84
        // Some basic information
85 31
        $this->data[MIDCOM_NAV_TYPE] = 'leaf';
86 31
        $this->data[MIDCOM_NAV_ID] = "{$this->node->id}-{$this->leafid}";
87 31
        $this->data[MIDCOM_NAV_NODEID] = $this->node->id;
88 31
        $this->data[MIDCOM_NAV_RELATIVEURL] = $this->node->relativeurl . $this->data[MIDCOM_NAV_URL];
89 31
        $this->data[MIDCOM_NAV_ICON] ??= null;
90
91
        // Save the original Leaf ID so that it is easier to query in topic-specific NAP code
92 31
        $this->data[MIDCOM_NAV_LEAFID] = $this->leafid;
93
94 31
        return $this->data;
95
    }
96
}
97