midcom_helper_toolbar_node::add_commands()   C
last analyzed

Complexity

Conditions 16
Paths 151

Size

Total Lines 84
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 61
CRAP Score 16.1841

Importance

Changes 0
Metric Value
cc 16
eloc 59
c 0
b 0
f 0
nc 151
nop 0
dl 0
loc 84
ccs 61
cts 67
cp 0.9104
crap 16.1841
rs 5.1416

How to fix   Long Method    Complexity   

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
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
 * This class is a node (topic) toolbar class.
11
 *
12
 * @package midcom.helper
13
 */
14
class midcom_helper_toolbar_node extends midcom_helper_toolbar_view
15
{
16
    private midcom_db_topic $topic;
17
18 351
    public function __construct(midcom_db_topic $topic)
19
    {
20 351
        $this->topic = $topic;
21 351
        $config = midcom::get()->config;
22 351
        parent::__construct($config->get('toolbars_node_style_class'), $config->get('toolbars_node_style_id'));
23 351
        $this->label = midcom::get()->i18n->get_string('folder', 'midcom');
24
    }
25
26 2
    protected function _check_index($index, bool $raise_error = true) : ?int
27
    {
28 2
        $this->add_commands();
29 2
        return parent::_check_index($index, $raise_error);
30
    }
31
32 7
    public function render() : string
33
    {
34 7
        $this->add_commands();
35 7
        return parent::render();
36
    }
37
38 7
    public function is_rendered() : bool
39
    {
40 7
        $this->add_commands();
41 7
        return parent::is_rendered();
42
    }
43
44 100
    public function add_item(array $item, $before = -1)
45
    {
46 100
        $this->add_commands();
47 100
        parent::add_item($item, $before);
48
    }
49
50 107
    private function add_commands()
51
    {
52 107
        if (!empty($this->items) || empty($this->topic->id)) {
53 57
            return;
54
        }
55 107
        $buttons = [];
56 107
        $workflow = new midcom\workflow\datamanager;
57 107
        if (   $this->topic->can_do('midgard:update')
58 107
            && $this->topic->can_do('midcom.admin.folder:topic_management')) {
59 96
            $buttons[] = $workflow->get_button("__ais/folder/edit/", [
60 96
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit folder', 'midcom.admin.folder'),
61 96
                MIDCOM_TOOLBAR_ACCESSKEY => 'g',
62 96
            ]);
63 96
            $buttons[] = $workflow->get_button("__ais/folder/metadata/{$this->topic->guid}/", [
64 96
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit folder metadata', 'midcom.admin.folder'),
65 96
                MIDCOM_TOOLBAR_GLYPHICON => 'database',
66 96
            ]);
67
            // Allow to move other than root folder
68 96
            if ($this->topic->guid !== midcom::get()->config->get('midcom_root_topic_guid')) {
69 96
                $viewer = new midcom\workflow\viewer;
70 96
                $buttons[] = $viewer->get_button("__ais/folder/move/{$this->topic->guid}/", [
71 96
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('move', 'midcom.admin.folder'),
72 96
                    MIDCOM_TOOLBAR_GLYPHICON => 'arrows',
73 96
                ]);
74
            }
75
76 96
            $viewer = new midcom\workflow\viewer;
77 96
            $buttons[] = $viewer->get_button("__ais/folder/order/", [
78 96
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('order navigation', 'midcom.admin.folder'),
79 96
                MIDCOM_TOOLBAR_GLYPHICON => 'sort',
80 96
                MIDCOM_TOOLBAR_ACCESSKEY => 'o',
81 96
            ]);
82
83 96
            $buttons[] = [
84 96
                MIDCOM_TOOLBAR_URL => midcom_connection::get_url('self') . "__mfa/asgard/object/open/{$this->topic->guid}/",
85 96
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('manage object', 'midgard.admin.asgard'),
86 96
                MIDCOM_TOOLBAR_GLYPHICON => 'cog',
87 96
                MIDCOM_TOOLBAR_ENABLED =>    midcom::get()->auth->can_user_do('midgard.admin.asgard:access', class: 'midgard_admin_asgard_plugin')
88 96
                                          && midcom::get()->auth->can_user_do('midgard.admin.asgard:manage_objects', class: 'midgard_admin_asgard_plugin'),
89 96
            ];
90
        }
91 107
        $buttons = array_merge($buttons, $this->get_approval_controls($this->topic, false));
92 107
        if (   $this->topic->can_do('midcom.admin.folder:template_management')
93 107
            && midcom::get()->auth->can_user_do('midgard.admin.asgard:manage_objects', class: 'midgard_admin_asgard_plugin')) {
94 96
            $enabled = false;
95 96
            $styleeditor_url = '';
96 96
            if ($this->topic->style != '') {
97
                if ($style_id = midcom_db_style::id_from_path($this->topic->style)) {
98
                    try {
99
                        $style = midcom_db_style::get_cached($style_id);
100
                        $styleeditor_url = midcom_connection::get_url('self') . "__mfa/asgard/object/view/{$style->guid}/";
101
                        $enabled = true;
102
                    } catch (midcom_error $e) {
103
                        $e->log();
104
                    }
105
                }
106
            }
107
108 96
            $buttons[] = [
109 96
                MIDCOM_TOOLBAR_URL => $styleeditor_url,
110 96
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit layout template', 'midcom.admin.folder'),
111 96
                MIDCOM_TOOLBAR_GLYPHICON => 'file-o',
112 96
                MIDCOM_TOOLBAR_ACCESSKEY => 't',
113 96
                MIDCOM_TOOLBAR_ENABLED => $enabled,
114 96
            ];
115
        }
116
117 107
        if ($this->topic->can_do('midcom.admin.folder:topic_management')) {
118 96
            if ($this->topic->can_do('midgard:create')) {
119 96
                $buttons[] = $workflow->get_button("__ais/folder/create/", [
120 96
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('create subfolder', 'midcom.admin.folder'),
121 96
                    MIDCOM_TOOLBAR_GLYPHICON => 'folder',
122 96
                    MIDCOM_TOOLBAR_ACCESSKEY => 'f',
123 96
                ]);
124
            }
125 96
            if (   $this->topic->guid !== midcom::get()->config->get('midcom_root_topic_guid')
126 96
                && $this->topic->can_do('midgard:delete')) {
127 96
                $workflow = new midcom\workflow\delete(['object' => $this->topic, 'recursive' => true]);
128 96
                $buttons[] = $workflow->get_button("__ais/folder/delete/", [
129 96
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('delete folder', 'midcom.admin.folder')
130 96
                ]);
131
            }
132
        }
133 107
        $this->items = array_map($this->clean_item(...), $buttons);
134
    }
135
}
136