Passed
Push — master ( 8c55af...d4950e )
by Andreas
22:23 queued 04:13
created

midcom_helper_toolbar_node::add_commands()   C

Complexity

Conditions 16
Paths 151

Size

Total Lines 83
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 16.283

Importance

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