Passed
Push — master ( cd8ed8...ef1fc1 )
by Andreas
10:21
created

midcom_helper_toolbar_view::bind_to()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 8.0231

Importance

Changes 0
Metric Value
cc 8
eloc 34
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 51
ccs 26
cts 28
cp 0.9286
crap 8.0231
rs 8.1315

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 348
    public function __construct(string $class_style = null, string $id_style = null)
21
    {
22 348
        $config = midcom::get()->config;
23 348
        $class_style = $class_style ?: $config->get('toolbars_view_style_class');
24 348
        $id_style = $id_style ?: $config->get('toolbars_view_style_id');
25 348
        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 348
        $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
                MIDCOM_TOOLBAR_GLYPHICON => 'database',
58
                MIDCOM_TOOLBAR_ACCESSKEY => 'm',
59
            ]);
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
                    MIDCOM_TOOLBAR_GLYPHICON => 'arrows',
65
                    MIDCOM_TOOLBAR_ENABLED => $object instanceof midcom_db_article
66
                ]),
67
                [
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
                    MIDCOM_TOOLBAR_GLYPHICON => 'cog',
71 49
                    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'),
72
                ]
73
            ]);
74
        }
75
76 52
        if (   midcom::get()->config->get('midcom_services_rcs_enable')
77 52
            && $object->can_do('midgard:update')
78 52
            && $object->_use_rcs) {
79 48
            $buttons[] = [
80 48
                MIDCOM_TOOLBAR_URL => "__ais/rcs/{$object->guid}/",
81 48
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('show history', 'midcom.admin.rcs'),
82
                MIDCOM_TOOLBAR_GLYPHICON => 'history',
83
                MIDCOM_TOOLBAR_ACCESSKEY => 'v',
84
            ];
85
        }
86 52
        $this->add_items($buttons);
87
    }
88
89 139
    public function get_approval_controls(midcom_core_dbaobject $object, bool $add_accesskey = false) : array
90
    {
91 139
        $buttons = [];
92 139
        if (midcom::get()->config->get('metadata_approval')) {
93
            if ($object->metadata->is_approved()) {
94
                $action = 'unapprove';
95
                $helptext = 'approved';
96
                $icontype = 'approved';
97
                $accesskey = 'u';
98
            } else {
99
                $action = 'approve';
100
                $helptext = 'unapproved';
101
                $icontype = 'notapproved';
102
                $accesskey = 'a';
103
            }
104
105
            $icon = 'stock-icons/16x16/page-' . $icontype . '.png';
106
            if (   !midcom::get()->config->get('show_hidden_objects')
107
                && !$object->metadata->is_visible()) {
108
                // Take scheduling into account
109
                $icon = 'stock-icons/16x16/page-' . $icontype . '-notpublished.png';
110
            }
111
            $buttons[] = [
112
                MIDCOM_TOOLBAR_URL => "__ais/folder/" . $action . "/",
113
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string($action, 'midcom'),
114
                MIDCOM_TOOLBAR_HELPTEXT => midcom::get()->i18n->get_string($helptext, 'midcom'),
115
                MIDCOM_TOOLBAR_ICON => $icon,
116
                MIDCOM_TOOLBAR_POST => true,
117
                MIDCOM_TOOLBAR_POST_HIDDENARGS => [
118
                    'guid' => $object->guid,
119
                    'return_to' => $_SERVER['REQUEST_URI'],
120
                ],
121
                MIDCOM_TOOLBAR_ACCESSKEY => ($add_accesskey) ? $accesskey : null,
122
                MIDCOM_TOOLBAR_ENABLED => $object->can_do('midcom:approve'),
123
            ];
124
        }
125 139
        return $buttons;
126
    }
127
}
128