Passed
Push — master ( 0b9bea...5e64e2 )
by Andreas
11:34
created

midgard_admin_asgard_handler_components   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 98.31%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 51
c 2
b 0
f 0
dl 0
loc 108
ccs 58
cts 59
cp 0.9831
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A _handler_list() 0 10 1
A _on_initialize() 0 3 1
A _show_list() 0 4 1
A _show_lists() 0 9 2
A _list_components() 0 5 3
A _load_component_data() 0 25 1
A _handler_component() 0 22 2
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;
0 ignored issues
show
introduced by
The trait midgard_admin_asgard_handler requires some properties which are not provided by midgard_admin_asgard_handler_components: $dbclassloader, $dbfactory
Loading history...
17
18
    private array $components = [];
19
20
    private array $libraries = [];
21
22 2
    public function _on_initialize()
23
    {
24 2
        $this->add_stylesheet(MIDCOM_STATIC_URL . '/midgard.admin.asgard/components.css');
25
    }
26
27 2
    private function _load_component_data(midcom_core_manifest $manifest) : array
28
    {
29 2
        $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 2
        ];
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 2
        ]);
42 2
        $data['toolbar']->add_item([
43 2
            MIDCOM_TOOLBAR_URL => '__ais/help/' . $manifest->name . '/',
44 2
            MIDCOM_TOOLBAR_LABEL => $this->_i18n->get_string('help', 'midcom.admin.help'),
45 2
            MIDCOM_TOOLBAR_GLYPHICON => 'question',
46 2
            MIDCOM_TOOLBAR_OPTIONS => [
47 2
                'target' => '_blank',
48 2
            ]
49 2
        ]);
50
51 2
        return $data;
52
    }
53
54 1
    private function _list_components()
55
    {
56 1
        foreach (midcom::get()->componentloader->get_manifests() as $manifest) {
57 1
            $type = ($manifest->purecode) ? 'libraries' : 'components';
58 1
            $this->$type[$manifest->name] = $this->_load_component_data($manifest);
59
        }
60
    }
61
62
    /**
63
     * Component list view
64
     */
65 1
    public function _handler_list(array &$data)
66
    {
67 1
        $data['view_title'] = $this->_l10n->get('components');
68
69 1
        $this->_list_components();
70
71
        // Set the breadcrumb data
72 1
        $this->add_breadcrumb($this->router->generate('welcome'), $this->_l10n->get($this->_component));
73 1
        $this->add_breadcrumb($this->router->generate('components'), $this->_l10n->get('components'));
74 1
        return $this->get_response();
75
    }
76
77
    /**
78
     * Shows the loaded components
79
     */
80 1
    public function _show_list(string $handler_id, array &$data)
81
    {
82 1
        $this->_show_lists('components');
83 1
        $this->_show_lists('libraries');
84
    }
85
86 1
    private function _show_lists(string $type)
87
    {
88 1
        $this->_request_data['list_type'] = $type;
89 1
        midcom_show_style('midgard_admin_asgard_components_header');
90 1
        foreach ($this->$type as $component_data) {
91 1
            $this->_request_data['component_data'] = $component_data;
92 1
            midcom_show_style('midgard_admin_asgard_components_item');
93
        }
94 1
        midcom_show_style('midgard_admin_asgard_components_footer');
95
    }
96
97
    /**
98
     * Component display
99
     */
100 1
    public function _handler_component(string $component, array &$data)
101
    {
102 1
        $data['component'] = $component;
103 1
        if (!midcom::get()->componentloader->is_installed($component)) {
104
            throw new midcom_error_notfound("Component {$component} is not installed.");
105
        }
106
107 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

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