Passed
Push — master ( 03df37...79e4b8 )
by Andreas
26:53 queued 07:50
created

_on_initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 2
    public function _on_initialize()
19
    {
20 2
        $this->add_stylesheet(MIDCOM_STATIC_URL . '/midgard.admin.asgard/components.css');
21 2
    }
22
23 2
    private function _load_component_data(midcom_core_manifest $manifest) : array
24
    {
25
        $data = [
26 2
            'name' => $manifest->name,
27 2
            'title' => $manifest->get_name_translated(),
28 2
            'purecode' => $manifest->purecode,
29 2
            'icon' => midcom::get()->componentloader->get_component_icon($manifest->name),
30 2
            'description' => $manifest->description,
31 2
            'toolbar' => new midcom_helper_toolbar()
32
        ];
33
34 2
        $data['toolbar']->add_item([
35 2
            MIDCOM_TOOLBAR_URL => $this->router->generate('components_configuration', ['component' => $manifest->name]),
36 2
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
37 2
            MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
38
        ]);
39 2
        $data['toolbar']->add_help_item($manifest->name);
40
41 2
        return $data;
42
    }
43
44 1
    private function _list_components()
45
    {
46 1
        $this->_request_data['components'] = [];
47 1
        $this->_request_data['libraries'] = [];
48
49 1
        foreach (midcom::get()->componentloader->get_manifests() as $manifest) {
50 1
            $type = ($manifest->purecode) ? 'libraries' : 'components';
51
52 1
            $this->_request_data[$type][$manifest->name] = $this->_load_component_data($manifest);
53
        }
54 1
    }
55
56
    /**
57
     * Component list view
58
     */
59 1
    public function _handler_list(array &$data)
60
    {
61 1
        $data['view_title'] = $this->_l10n->get('components');
62
63 1
        $this->_list_components();
64
65
        // Set the breadcrumb data
66 1
        $this->add_breadcrumb($this->router->generate('welcome'), $this->_l10n->get($this->_component));
67 1
        $this->add_breadcrumb($this->router->generate('components'), $this->_l10n->get('components'));
68 1
        return $this->get_response();
69
    }
70
71
    /**
72
     * Shows the loaded components
73
     *
74
     * @param array $data The local request data.
75
     */
76 1
    public function _show_list(string $handler_id, array &$data)
77
    {
78 1
        $this->_show_lists('components');
79 1
        $this->_show_lists('libraries');
80 1
    }
81
82 1
    private function _show_lists(string $type)
83
    {
84 1
        $this->_request_data['list_type'] = $type;
85 1
        midcom_show_style('midgard_admin_asgard_components_header');
86 1
        foreach ($this->_request_data[$type] as $component_data) {
87 1
            $this->_request_data['component_data'] = $component_data;
88 1
            midcom_show_style('midgard_admin_asgard_components_item');
89
        }
90 1
        midcom_show_style('midgard_admin_asgard_components_footer');
91 1
    }
92
93
    /**
94
     * Component display
95
     */
96 1
    public function _handler_component(string $component, array &$data)
97
    {
98 1
        $data['component'] = $component;
99 1
        if (!midcom::get()->componentloader->is_installed($component)) {
100
            throw new midcom_error_notfound("Component {$component} is not installed.");
101
        }
102
103 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

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