Completed
Push — master ( e41e62...d22395 )
by Andreas
17:46
created

midcom_helper_toolbar_node::add_commands()   F

Complexity

Conditions 20
Paths 864

Size

Total Lines 92
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 59
CRAP Score 20.3145

Importance

Changes 0
Metric Value
cc 20
eloc 65
nc 864
nop 0
dl 0
loc 92
ccs 59
cts 65
cp 0.9077
crap 20.3145
rs 0.1888
c 0
b 0
f 0

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
     *
18
     * @var midcom_db_topic
19
     */
20
    private $topic;
21
22 338
    public function __construct(midcom_db_topic $topic)
23
    {
24 338
        $this->topic = $topic;
25 338
        $config = midcom::get()->config;
26 338
        parent::__construct($config->get('toolbars_node_style_class'), $config->get('toolbars_node_style_id'));
27 338
        $this->label = midcom::get()->i18n->get_string('folder', 'midcom');
28 338
    }
29
30 2
    protected function _check_index($index, $raise_error = true)
31
    {
32 2
        if (empty($this->items) && !empty($this->topic->id)) {
33 1
            $this->add_commands();
34
        }
35 2
        return parent::_check_index($index, $raise_error);
36
    }
37
38 1
    private function add_commands()
39
    {
40 1
        $topics = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_URLTOPICS);
41 1
        $urltopic = end($topics);
0 ignored issues
show
Bug introduced by
It seems like $topics can also be of type false; however, parameter $array of end() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $urltopic = end(/** @scrutinizer ignore-type */ $topics);
Loading history...
42 1
        if (!$urltopic) {
43 1
            $urltopic = $this->topic;
44
        }
45 1
        $buttons = [];
46 1
        $workflow = new midcom\workflow\datamanager;
47 1
        if (   $this->topic->can_do('midgard:update')
48 1
            && $this->topic->can_do('midcom.admin.folder:topic_management')) {
49 1
            $buttons[] = $workflow->get_button("__ais/folder/edit/", [
50 1
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit folder', 'midcom.admin.folder'),
51 1
                MIDCOM_TOOLBAR_ACCESSKEY => 'g',
52
            ]);
53 1
            $buttons[] = $workflow->get_button("__ais/folder/metadata/{$urltopic->guid}/", [
54 1
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit folder metadata', 'midcom.admin.folder'),
55 1
                MIDCOM_TOOLBAR_GLYPHICON => 'database',
56
            ]);
57
        }
58
59 1
        if (   $urltopic->can_do('midgard:update')
60 1
            && $urltopic->can_do('midcom.admin.folder:topic_management')) {
61
            // Allow to move other than root folder
62 1
            if ($urltopic->guid !== midcom::get()->config->get('midcom_root_topic_guid')) {
63 1
                $viewer = new midcom\workflow\viewer;
64 1
                $buttons[] = $viewer->get_button("__ais/folder/move/{$urltopic->guid}/", [
65 1
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('move', 'midcom.admin.folder'),
66 1
                    MIDCOM_TOOLBAR_GLYPHICON => 'arrows',
67
                ]);
68
            }
69
        }
70
71 1
        if (   $this->topic->can_do('midgard:update')
72 1
            && $this->topic->can_do('midcom.admin.folder:topic_management')) {
73 1
            $buttons[] = [
74 1
                MIDCOM_TOOLBAR_URL => "__ais/folder/order/",
75 1
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('order navigation', 'midcom.admin.folder'),
76 1
                MIDCOM_TOOLBAR_GLYPHICON => 'sort',
77 1
                MIDCOM_TOOLBAR_ACCESSKEY => 'o',
78
            ];
79
80 1
            $buttons[] = [
81 1
                MIDCOM_TOOLBAR_URL => midcom_connection::get_url('self') . "__mfa/asgard/object/open/{$this->topic->guid}/",
82 1
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('manage object', 'midgard.admin.asgard'),
83 1
                MIDCOM_TOOLBAR_GLYPHICON => 'cog',
84 1
                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'),
85
            ];
86
        }
87 1
        $buttons = array_merge($buttons, $this->get_approval_controls($this->topic, false));
88 1
        if (   $this->topic->can_do('midcom.admin.folder:template_management')
89 1
            && midcom::get()->auth->can_user_do('midgard.admin.asgard:manage_objects', null, 'midgard_admin_asgard_plugin')) {
90 1
            $enabled = false;
91 1
            $styleeditor_url = '';
92 1
            if ($this->topic->style != '') {
93
                if ($style_id = midcom::get()->style->get_style_id_from_path($this->topic->style)) {
94
                    try {
95
                        $style = midcom_db_style::get_cached($style_id);
96
                        $styleeditor_url = midcom_connection::get_url('self') . "__mfa/asgard/object/view/{$style->guid}/";
97
                        $enabled = true;
98
                    } catch (midcom_error $e) {
99
                        $e->log();
100
                    }
101
                }
102
            }
103
104 1
            $buttons[] = [
105 1
                MIDCOM_TOOLBAR_URL => $styleeditor_url,
106 1
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit layout template', 'midcom.admin.folder'),
107 1
                MIDCOM_TOOLBAR_GLYPHICON => 'file-o',
108 1
                MIDCOM_TOOLBAR_ACCESSKEY => 't',
109 1
                MIDCOM_TOOLBAR_ENABLED => $enabled,
110
            ];
111
        }
112
113 1
        if (   $this->topic->can_do('midgard:create')
114 1
            && $this->topic->can_do('midcom.admin.folder:topic_management')) {
115 1
            $buttons[] = $workflow->get_button("__ais/folder/create/", [
116 1
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('create subfolder', 'midcom.admin.folder'),
117 1
                MIDCOM_TOOLBAR_GLYPHICON => 'folder',
118 1
                MIDCOM_TOOLBAR_ACCESSKEY => 'f',
119
            ]);
120
        }
121 1
        if (   $urltopic->guid !== midcom::get()->config->get('midcom_root_topic_guid')
122 1
            && $urltopic->can_do('midgard:delete')
123 1
            && $urltopic->can_do('midcom.admin.folder:topic_management')) {
124 1
            $workflow = new midcom\workflow\delete(['object' => $urltopic, 'recursive' => true]);
125 1
            $buttons[] = $workflow->get_button("__ais/folder/delete/", [
126 1
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('delete folder', 'midcom.admin.folder')
127
            ]);
128
        }
129 1
        $this->add_items($buttons);
130 1
    }
131
}
132