Passed
Push — master ( 1c5582...77b393 )
by Andreas
18:18
created

_handler_component()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
crap 2.0014
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midgard.admin.asgard
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
 * Component display
11
 *
12
 * @package midgard.admin.asgard
13
 */
14
class midgard_admin_asgard_handler_components extends midcom_baseclasses_components_handler
15
{
16
    use midgard_admin_asgard_handler;
17
18
    private $components = [];
19
20
    private $libraries = [];
21
22 2
    public function _on_initialize()
23
    {
24 2
        $this->add_stylesheet(MIDCOM_STATIC_URL . '/midgard.admin.asgard/components.css');
25 2
    }
26
27 2
    private function _load_component_data(midcom_core_manifest $manifest) : array
28
    {
29
        $data = [
30 2
            'name' => $manifest->name,
31 2
            'title' => $manifest->get_name_translated(),
32 2
            'icon' => midcom::get()->componentloader->get_component_icon($manifest->name),
33 2
            'description' => $manifest->description,
34 2
            'toolbar' => new midcom_helper_toolbar()
35
        ];
36
37 2
        $data['toolbar']->add_item([
38 2
            MIDCOM_TOOLBAR_URL => $this->router->generate('components_configuration', ['component' => $manifest->name]),
39 2
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
40 2
            MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
41
        ]);
42 2
        $data['toolbar']->add_help_item($manifest->name);
43
44 2
        return $data;
45
    }
46
47 1
    private function _list_components()
48
    {
49 1
        foreach (midcom::get()->componentloader->get_manifests() as $manifest) {
50 1
            $type = ($manifest->purecode) ? 'libraries' : 'components';
51 1
            $this->$type[$manifest->name] = $this->_load_component_data($manifest);
52
        }
53 1
    }
54
55
    /**
56
     * Component list view
57
     */
58 1
    public function _handler_list(array &$data)
59
    {
60 1
        $data['view_title'] = $this->_l10n->get('components');
61
62 1
        $this->_list_components();
63
64
        // Set the breadcrumb data
65 1
        $this->add_breadcrumb($this->router->generate('welcome'), $this->_l10n->get($this->_component));
66 1
        $this->add_breadcrumb($this->router->generate('components'), $this->_l10n->get('components'));
67 1
        return $this->get_response();
68
    }
69
70
    /**
71
     * Shows the loaded components
72
     */
73 1
    public function _show_list(string $handler_id, array &$data)
74
    {
75 1
        $this->_show_lists('components');
76 1
        $this->_show_lists('libraries');
77 1
    }
78
79 1
    private function _show_lists(string $type)
80
    {
81 1
        $this->_request_data['list_type'] = $type;
82 1
        midcom_show_style('midgard_admin_asgard_components_header');
83 1
        foreach ($this->$type as $component_data) {
84 1
            $this->_request_data['component_data'] = $component_data;
85 1
            midcom_show_style('midgard_admin_asgard_components_item');
86
        }
87 1
        midcom_show_style('midgard_admin_asgard_components_footer');
88 1
    }
89
90
    /**
91
     * Component display
92
     */
93 1
    public function _handler_component(string $component, array &$data)
94
    {
95 1
        $data['component'] = $component;
96 1
        if (!midcom::get()->componentloader->is_installed($component)) {
97
            throw new midcom_error_notfound("Component {$component} is not installed.");
98
        }
99
100 1
        $data['component_data'] = $this->_load_component_data(midcom::get()->componentloader->get_manifest($component));
0 ignored issues
show
Bug introduced by
It seems like midcom::get()->component...et_manifest($component) can also be of type null; however, parameter $manifest of midgard_admin_asgard_han...:_load_component_data() does only seem to accept midcom_core_manifest, 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

100
        $data['component_data'] = $this->_load_component_data(/** @scrutinizer ignore-type */ midcom::get()->componentloader->get_manifest($component));
Loading history...
101
102 1
        $data['view_title'] = $data['component_data']['title'];
103
104 1
        $data['asgard_toolbar']->add_item([
105 1
            MIDCOM_TOOLBAR_URL => $this->router->generate('components_configuration', ['component' => $component]),
106 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
107 1
            MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
108
        ]);
109
110
        // Set the breadcrumb data
111 1
        $this->add_breadcrumb($this->router->generate('welcome'), $this->_l10n->get($this->_component));
112 1
        $this->add_breadcrumb($this->router->generate('components'), $this->_l10n->get('components'));
113 1
        $this->add_breadcrumb('', $data['component_data']['title']);
114 1
        return $this->get_response('midgard_admin_asgard_components_component');
115
    }
116
}
117