Passed
Push — master ( 459a10...bba38e )
by Andreas
20:19
created

net_nehmer_static_viewer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 71.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 81
ccs 33
cts 46
cp 0.7174
rs 10
c 1
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_topic_qb() 0 20 5
A index() 0 10 1
A _populate_node_toolbar() 0 26 5
A _on_handle() 0 5 1
1
<?php
2
/**
3
 * @package net.nehmer.static
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
use midcom\datamanager\datamanager;
10
use midcom\datamanager\schemadb;
11
12
/**
13
 * n.n.static site interface class
14
 *
15
 * @package net.nehmer.static
16
 */
17
class net_nehmer_static_viewer extends midcom_baseclasses_components_viewer
18
{
19
    /**
20
     * Indexes an article.
21
     *
22
     * @param midcom_db_topic|midcom_core_dbaproxy $topic The topic which we are bound to. If this is not an object, the code
23
     *     tries to load a new topic instance from the database identified by this parameter.
24
     */
25
    public static function index(datamanager $dm, midcom_services_indexer $indexer, $topic)
26
    {
27
        $nav = new midcom_helper_nav();
28
        $node = $nav->get_node($topic->id);
29
30
        $document = $indexer->new_document($dm);
31
        $document->topic_guid = $topic->guid;
32
        $document->topic_url = $node[MIDCOM_NAV_FULLURL];
33
        $document->component = $topic->component;
0 ignored issues
show
Bug Best Practice introduced by
The property component does not exist on midcom_core_dbaproxy. Since you implemented __get, consider adding a @property annotation.
Loading history...
34
        $indexer->index($document);
35
    }
36
37
    /**
38
     * Populates the node toolbar depending on the user's rights.
39
     */
40 8
    private function _populate_node_toolbar()
41
    {
42 8
        $buttons = [];
43 8
        $workflow = $this->get_workflow('datamanager');
44 8
        if ($this->_topic->can_do('midgard:create')) {
45 7
            foreach ($this->_request_data['schemadb']->all() as $name => $schema) {
46 7
                $buttons[] = $workflow->get_button($this->router->generate('create', ['schema' => $name]), [
47 7
                    MIDCOM_TOOLBAR_LABEL => sprintf(
48 7
                        $this->_l10n_midcom->get('create %s'),
49 7
                        $this->_l10n->get($schema->get('description'))
50 7
                    ),
51 7
                    MIDCOM_TOOLBAR_GLYPHICON => 'file-o',
52 7
                    MIDCOM_TOOLBAR_ACCESSKEY => 'n',
53 7
                ]);
54
            }
55
        }
56
57 8
        if (   $this->_topic->can_do('midgard:update')
58 8
            && $this->_topic->can_do('midcom:component_config')) {
59 7
                $buttons[] = $workflow->get_button($this->router->generate('config'), [
60 7
                MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
61 7
                MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'),
62 7
                MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
63 7
            ]);
64
        }
65 8
        $this->_node_toolbar->add_items($buttons);
66
    }
67
68
    /**
69
     * The handle callback populates the toolbars.
70
     */
71 8
    public function _on_handle($handler, array $args)
72
    {
73 8
        $this->_request_data['schemadb'] = schemadb::from_path($this->_config->get('schemadb'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('schemadb') can also be of type false; however, parameter $path of midcom\datamanager\schemadb::from_path() 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

73
        $this->_request_data['schemadb'] = schemadb::from_path(/** @scrutinizer ignore-type */ $this->_config->get('schemadb'));
Loading history...
74
75 8
        $this->_populate_node_toolbar();
76
    }
77
78 12
    public static function get_topic_qb(int $id, ?string $sort_property = null) : midcom_core_querybuilder
79
    {
80 12
        $qb = midcom_db_article::new_query_builder();
81 12
        $qb->add_constraint('topic', '=', $id);
82
83 12
        if ($sort_property) {
84 12
            $sort_order = 'ASC';
85 12
            if (str_starts_with($sort_property, 'reverse ')) {
86
                $sort_order = 'DESC';
87
                $sort_property = substr($sort_property, strlen('reverse '));
88
            }
89 12
            if (!str_contains($sort_property, 'metadata.')) {
90
                $ref = midcom_helper_reflector::get('midgard_article');
91
                if (!$ref->property_exists($sort_property)) {
92
                    $sort_property = 'metadata.' . $sort_property;
93
                }
94
            }
95 12
            $qb->add_order($sort_property, $sort_order);
96
        }
97 12
        return $qb;
98
    }
99
}
100