midgard_admin_asgard_toolbar::bind_to_object()   F
last analyzed

Complexity

Conditions 26
Paths 1153

Size

Total Lines 133
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 73
CRAP Score 31.2314

Importance

Changes 0
Metric Value
cc 26
eloc 88
nc 1153
nop 3
dl 0
loc 133
ccs 73
cts 91
cp 0.8022
crap 31.2314
rs 0
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 midgard.admin.asgard
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
 * Wrapper for Asgard's toolbar
11
 *
12
 * @package midgard.admin.asgard
13
 */
14
class midgard_admin_asgard_toolbar extends midcom_helper_toolbar_view
15
{
16 20
    private function _generate_url(string $action, midcom_core_dbaobject $object) : string
17
    {
18 20
        return '__mfa/asgard/object/' . $action . '/' . $object->guid . '/';
19
    }
20
21
    /**
22
     * Populate the object toolbar
23
     */
24 21
    public function bind_to_object(midcom_core_dbaobject $object, string $handler_id, array $data)
25
    {
26 21
        if ($handler_id == 'object_deleted') {
27 1
            return;
28
        }
29 20
        $buttons = [];
30
        // Show view toolbar button, if the user hasn't configured to use straight the edit mode
31 20
        if ($data['default_mode'] === 'view') {
32 20
            $buttons[] = [
33 20
                MIDCOM_TOOLBAR_URL => $this->_generate_url('view', $object),
34 20
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('view', 'midcom'),
35 20
                MIDCOM_TOOLBAR_GLYPHICON => 'eye',
36 20
                MIDCOM_TOOLBAR_ACCESSKEY => 'v',
37 20
            ];
38
        }
39 20
        $config = midcom_baseclasses_components_configuration::get('midgard.admin.asgard', 'config');
40 20
        $no_permalink = false;
41 20
        foreach ($config->get_array('no_permalinks_for') as $classname) {
42 20
            if ($object instanceof $classname) {
43
                $no_permalink = true;
44
                break;
45
            }
46
        }
47 20
        if (!$no_permalink) {
48 20
            $link = midcom::get()->permalinks->resolve_permalink($object->guid);
49 20
            if ($link) {
50
                $buttons[] = [
51
                    MIDCOM_TOOLBAR_URL => $link,
52
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('view on site', 'midgard.admin.asgard'),
53
                    MIDCOM_TOOLBAR_GLYPHICON => 'globe',
54
                ];
55
            }
56
        }
57
58 20
        if ($object->can_do('midgard:update')) {
59 20
            $buttons[] = [
60 20
                MIDCOM_TOOLBAR_URL => $this->_generate_url('edit', $object),
61 20
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit', 'midcom'),
62 20
                MIDCOM_TOOLBAR_GLYPHICON => 'pencil',
63 20
                MIDCOM_TOOLBAR_ACCESSKEY => 'e',
64 20
            ];
65
        }
66
67 20
        if ($object->can_do('midgard:create')) {
68 20
            $buttons[] = [
69 20
                MIDCOM_TOOLBAR_URL => $this->_generate_url('copy', $object),
70 20
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('copy', 'midcom'),
71 20
                MIDCOM_TOOLBAR_GLYPHICON => 'clone',
72 20
            ];
73
        }
74
75 20
        if ($object->can_do('midgard:update')) {
76 20
            $buttons = array_merge($buttons, $this->get_toolbar_update_items($object));
77
        }
78
79 20
        if ($object->can_do('midgard:create')) {
80
            // Find out what types of children the object can have and show create buttons for them
81 20
            $child_types = $data['tree_reflector']->get_child_classes();
82 20
            foreach ($child_types as $type) {
83 20
                $display_button = true;
84 20
                if ($object instanceof midcom_db_topic) {
85
                    // With topics we should check for component before populating create buttons as so many types can be children of topics
86
                    switch ($type) {
87 13
                        case 'midgard_topic':
88 13
                        case 'midgard_article':
89
                            // Articles and topics can always be created
90 13
                            break;
91
                        default:
92 13
                            if (!midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($type)) {
93
                                $display_button = false;
94
                                break;
95
                            }
96 13
                            $component = midcom::get()->dbclassloader->get_component_for_class($type);
97 13
                            if ($component != $object->component) {
98 13
                                $display_button = false;
99
                            }
100 13
                            break;
101
                    }
102 8
                } elseif ($object instanceof midcom_db_article) {
103
                    try {
104
                        $topic = new midcom_db_topic($object->topic);
105
                        // With articles we should check for topic component before populating create buttons as so many types can be children of topics
106
                        switch ($type) {
107
                            case 'midgard_article':
108
                                // Articles can always be created
109
                                break;
110
                            default:
111
                                $component = midcom::get()->dbclassloader->get_component_for_class($type);
112
                                if ($component != $topic->component) {
113
                                    $display_button = false;
114
                                }
115
                                break;
116
                        }
117
                    } catch (midcom_error $e) {
118
                        $e->log();
119
                    }
120
                }
121
122 20
                if (!$display_button) {
123
                    // Skip this type
124 13
                    continue;
125
                }
126
127 20
                $buttons[] = [
128 20
                    MIDCOM_TOOLBAR_URL => $this->_generate_url('create/' . $type, $object),
129 20
                    MIDCOM_TOOLBAR_LABEL => sprintf(midcom::get()->i18n->get_string('create %s', 'midcom'), midgard_admin_asgard_plugin::get_type_label($type)),
130 20
                    MIDCOM_TOOLBAR_GLYPHICON => $data['tree_reflector']->get_create_icon($type),
131 20
                ];
132
            }
133
        }
134
135 20
        if ($object->can_do('midgard:delete')) {
136 20
            $buttons[] = [
137 20
                MIDCOM_TOOLBAR_URL => $this->_generate_url('delete', $object),
138 20
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('delete', 'midcom'),
139 20
                MIDCOM_TOOLBAR_GLYPHICON => 'trash',
140 20
                MIDCOM_TOOLBAR_ACCESSKEY => 'd',
141 20
            ];
142
        }
143
144 20
        if (   midcom::get()->config->get('midcom_services_rcs_enable')
145 20
            && $object->can_do('midgard:update')
146 20
            && $object->_use_rcs) {
147 20
            $buttons[] = [
148 20
                MIDCOM_TOOLBAR_URL => $this->_generate_url('rcs', $object),
149 20
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('show history', 'midcom.admin.rcs'),
150 20
                MIDCOM_TOOLBAR_GLYPHICON => 'history',
151 20
                MIDCOM_TOOLBAR_ENABLED => !str_starts_with($handler_id, 'object_rcs'),
152 20
                MIDCOM_TOOLBAR_ACCESSKEY => 'h',
153 20
            ];
154
        }
155 20
        $this->add_items($buttons);
156 20
        $this->_disable_active_item($handler_id, $object, $data);
157
    }
158
159 20
    private function _disable_active_item(string $handler_id, midcom_core_dbaobject $object, array $data)
160
    {
161
        switch ($handler_id) {
162 20
            case 'object_view':
163 1
                $this->disable_item($this->_generate_url('view', $object));
164 1
                break;
165 19
            case 'object_edit':
166 1
                $this->disable_item($this->_generate_url('edit', $object));
167 1
                break;
168 18
            case 'object_copy':
169 2
                $this->disable_item($this->_generate_url('copy', $object));
170 2
                break;
171 16
            case 'components_configuration_edit_folder':
172 1
                $this->disable_item("__mfa/asgard/components/configuration/edit/{$object->component}/{$object->guid}/");
0 ignored issues
show
Bug Best Practice introduced by
The property component does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
173 1
                break;
174 15
            case 'object_metadata':
175 1
                $this->disable_item($this->_generate_url('metadata', $object));
176 1
                break;
177 14
            case 'object_attachments':
178 13
            case 'object_attachments_edit':
179 2
                $this->disable_item($this->_generate_url('attachments', $object));
180 2
                break;
181 12
            case 'object_parameters':
182 1
                $this->disable_item($this->_generate_url('parameters', $object));
183 1
                break;
184 11
            case 'object_permissions':
185 1
                $this->disable_item($this->_generate_url('permissions', $object));
186 1
                break;
187 10
            case 'object_create':
188 1
                $this->disable_item($this->_generate_url('create/' . $data['current_type'], $object));
189 1
                break;
190 9
            case 'object_delete':
191 1
                $this->disable_item($this->_generate_url('delete', $object));
192 1
                break;
193 8
            case '____mfa-asgard_midcom.helper.replicator-object':
194
                $this->disable_item("__mfa/asgard_midcom.helper.replicator/object/{$object->guid}/");
195
                break;
196
        }
197
    }
198
199 20
    private function get_toolbar_update_items(midcom_core_dbaobject $object) : array
200
    {
201 20
        $buttons = [];
202 20
        if (   $object instanceof midcom_db_topic
203 20
            && $object->component
204 20
            && $object->can_do('midcom:component_config')) {
205 13
            $buttons[] = [
206 13
                MIDCOM_TOOLBAR_URL => "__mfa/asgard/components/configuration/edit/{$object->component}/{$object->guid}/",
207 13
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('component configuration', 'midcom'),
208 13
                MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
209 13
            ];
210
        }
211
212 20
        $buttons[] = [
213 20
            MIDCOM_TOOLBAR_URL => $this->_generate_url('metadata', $object),
214 20
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('metadata', 'midcom'),
215 20
            MIDCOM_TOOLBAR_GLYPHICON => 'database',
216 20
            MIDCOM_TOOLBAR_ACCESSKEY => 'm',
217 20
        ];
218 20
        $buttons = array_merge($buttons, $this->get_approval_controls($object));
219
220 20
        $buttons[] = [
221 20
            MIDCOM_TOOLBAR_URL => $this->_generate_url('attachments', $object),
222 20
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('attachments', 'midgard.admin.asgard'),
223 20
            MIDCOM_TOOLBAR_GLYPHICON => 'paperclip',
224 20
        ];
225
226 20
        $buttons[] = [
227 20
            MIDCOM_TOOLBAR_URL => $this->_generate_url('parameters', $object),
228 20
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('parameters', 'midcom'),
229 20
            MIDCOM_TOOLBAR_GLYPHICON => 'sitemap',
230 20
            MIDCOM_TOOLBAR_ENABLED => $object->can_do('midgard:parameters'),
231 20
        ];
232
233 20
        $buttons[] = [
234 20
            MIDCOM_TOOLBAR_URL => $this->_generate_url('permissions', $object),
235 20
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('privileges', 'midcom'),
236 20
            MIDCOM_TOOLBAR_GLYPHICON => 'shield',
237 20
            MIDCOM_TOOLBAR_ENABLED => $object->can_do('midgard:privileges'),
238 20
        ];
239
240 20
        if (   midcom::get()->componentloader->is_installed('midcom.helper.replicator')
241 20
            && midcom::get()->auth->admin) {
242
            $buttons[] = [
243
                MIDCOM_TOOLBAR_URL => "__mfa/asgard_midcom.helper.replicator/object/{$object->guid}/",
244
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('replication information', 'midcom.helper.replicator'),
245
                MIDCOM_TOOLBAR_GLYPHICON => 'files-o',
246
                MIDCOM_TOOLBAR_ACCESSKEY => 'r',
247
            ];
248
        }
249 20
        return $buttons;
250
    }
251
}
252