Passed
Push — master ( 3d72ea...5b8c59 )
by Andreas
23:46
created

midcom_helper_toolbar_view::bind_to()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 8.0069

Importance

Changes 0
Metric Value
cc 8
eloc 35
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 52
ccs 40
cts 42
cp 0.9524
crap 8.0069
rs 8.1155

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
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 view toolbar class.
11
 *
12
 * @package midcom.helper
13
 */
14
class midcom_helper_toolbar_view extends midcom_helper_toolbar
15
{
16
    /**
17
     * @param string $class_style The class style tag for the UL.
18
     * @param string $id_style The id style tag for the UL.
19
     */
20 351
    public function __construct(?string $class_style = null, ?string $id_style = null)
21
    {
22 351
        $config = midcom::get()->config;
23 351
        $class_style = $class_style ?: $config->get('toolbars_view_style_class');
24 351
        $id_style = $id_style ?: $config->get('toolbars_view_style_id');
25 351
        parent::__construct($class_style, $id_style);
0 ignored issues
show
Bug introduced by
It seems like $class_style can also be of type null; however, parameter $class_style of midcom_helper_toolbar::__construct() does only seem to accept string, 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

25
        parent::__construct(/** @scrutinizer ignore-type */ $class_style, $id_style);
Loading history...
26 351
        $this->label = midcom::get()->i18n->get_string('page', 'midcom');
27
    }
28
29
    /**
30
     * Binds the a toolbar to a DBA object. This will append a number of globally available
31
     * toolbar options. For example, expect Metadata- and Version Control-related options
32
     * to be added.
33
     *
34
     * Repeated bind calls are intercepted, you can only bind a toolbar to a single object.
35
     */
36 52
    public function bind_to(midcom_core_dbaobject $object)
37
    {
38 52
        if (!midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX)) {
39
            debug_add("Toolbar for object {$object->guid} was called before topic prefix was available, skipping global items.", MIDCOM_LOG_WARN);
40
            return;
41
        }
42 52
        if (array_key_exists('midcom_services_toolbars_bound_to_object', $this->customdata)) {
43
            // We already processed this toolbar, skipping further adds.
44 1
            return;
45
        }
46 52
        $this->customdata['midcom_services_toolbars_bound_to_object'] = true;
47
48 52
        $reflector = new midcom_helper_reflector($object);
0 ignored issues
show
Bug introduced by
$object of type midcom_core_dbaobject is incompatible with the type midgard\portable\api\mgdobject|string expected by parameter $src of midcom_helper_reflector::__construct(). ( Ignorable by Annotation )

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

48
        $reflector = new midcom_helper_reflector(/** @scrutinizer ignore-type */ $object);
Loading history...
49 52
        $this->set_label($reflector->get_class_label());
50
51 52
        $buttons = $this->get_approval_controls($object);
52
53 52
        if ($object->can_do('midgard:update')) {
54 49
            $workflow = new midcom\workflow\datamanager;
55 49
            $buttons[] = $workflow->get_button("__ais/folder/metadata/{$object->guid}/", [
56 49
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit metadata', 'midcom.admin.folder'),
57 49
                MIDCOM_TOOLBAR_GLYPHICON => 'database',
58 49
                MIDCOM_TOOLBAR_ACCESSKEY => 'm',
59 49
            ]);
60 49
            $viewer = new midcom\workflow\viewer;
61 49
            $buttons = array_merge($buttons, [
62 49
                $viewer->get_button("__ais/folder/move/{$object->guid}/", [
63 49
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('move', 'midcom.admin.folder'),
64 49
                    MIDCOM_TOOLBAR_GLYPHICON => 'arrows',
65 49
                    MIDCOM_TOOLBAR_ENABLED => $object instanceof midcom_db_article
66 49
                ]),
67 49
                [
68 49
                    MIDCOM_TOOLBAR_URL => midcom_connection::get_url('self') . "__mfa/asgard/object/open/{$object->guid}/",
69 49
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('manage object', 'midgard.admin.asgard'),
70 49
                    MIDCOM_TOOLBAR_GLYPHICON => 'cog',
71 49
                    MIDCOM_TOOLBAR_ENABLED =>    midcom::get()->auth->can_user_do('midgard.admin.asgard:access', class: 'midgard_admin_asgard_plugin')
72 49
                                              && midcom::get()->auth->can_user_do('midgard.admin.asgard:manage_objects', class: 'midgard_admin_asgard_plugin'),
73 49
                ]
74 49
            ]);
75
        }
76
77 52
        if (   midcom::get()->config->get('midcom_services_rcs_enable')
78 52
            && $object->can_do('midgard:update')
79 52
            && $object->_use_rcs) {
80 48
            $buttons[] = [
81 48
                MIDCOM_TOOLBAR_URL => "__ais/rcs/{$object->guid}/",
82 48
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('show history', 'midcom.admin.rcs'),
83 48
                MIDCOM_TOOLBAR_GLYPHICON => 'history',
84 48
                MIDCOM_TOOLBAR_ACCESSKEY => 'v',
85 48
            ];
86
        }
87 52
        $this->add_items($buttons);
88
    }
89
90 139
    public function get_approval_controls(midcom_core_dbaobject $object, bool $add_accesskey = false) : array
91
    {
92 139
        $buttons = [];
93 139
        if (midcom::get()->config->get('metadata_approval')) {
94
            $published = midcom::get()->config->get('show_hidden_objects') || $object->metadata->is_visible();
95
            if ($object->metadata->is_approved()) {
96
                $action = 'unapprove';
97
                $helptext = 'approved';
98
                $accesskey = 'u';
99
                $icon = $published ? 'check-square-o' : 'calendar-check-o';
100
            } else {
101
                $action = 'approve';
102
                $helptext = 'unapproved';
103
                $accesskey = 'a';
104
                $icon = $published ? 'times-rectangle-o' : 'calendar-times-o';
105
            }
106
107
            $buttons[] = [
108
                MIDCOM_TOOLBAR_URL => "__ais/folder/" . $action . "/",
109
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string($action, 'midcom'),
110
                MIDCOM_TOOLBAR_HELPTEXT => midcom::get()->i18n->get_string($helptext, 'midcom'),
111
                MIDCOM_TOOLBAR_GLYPHICON => $icon,
112
                MIDCOM_TOOLBAR_POST => true,
113
                MIDCOM_TOOLBAR_POST_HIDDENARGS => [
114
                    'guid' => $object->guid,
115
                    'return_to' => $_SERVER['REQUEST_URI'],
116
                ],
117
                MIDCOM_TOOLBAR_ACCESSKEY => ($add_accesskey) ? $accesskey : null,
118
                MIDCOM_TOOLBAR_ENABLED => $object->can_do('midcom:approve'),
119
            ];
120
        }
121 139
        return $buttons;
122
    }
123
}
124