Test Failed
Push — master ( 766a04...43c9a3 )
by Andreas
07:41
created

net_nehmer_blog_handler_index   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 80.88%

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 135
ccs 55
cts 68
cp 0.8088
rs 10
c 0
b 0
f 0
wmc 18

3 Methods

Rating   Name   Duplication   Size   Complexity  
B _show_index() 0 45 8
A _handler_index() 0 37 4
A _process_category_constraint() 0 27 6
1
<?php
2
/**
3
 * @package net.nehmer.blog
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
11
/**
12
 * Blog Index handler page handler
13
 *
14
 * Shows the configured number of postings with their abstracts.
15
 *
16
 * @package net.nehmer.blog
17
 */
18
class net_nehmer_blog_handler_index extends midcom_baseclasses_components_handler
19
{
20
    use net_nehmer_blog_handler;
0 ignored issues
show
introduced by
The trait net_nehmer_blog_handler requires some properties which are not provided by net_nehmer_blog_handler_index: $name, $url, $guid, $revised, $metadata
Loading history...
21
22
    /**
23
     * @var midcom_db_article[]
24
     */
25
    private array $_articles;
26
27
    private string $category;
28
29
    /**
30
     * Shows the autoindex list. Nothing to do in the handle phase except setting last modified
31
     * dates.
32
     */
33 5
    public function _handler_index(string $handler_id, array $args, array &$data)
34
    {
35 5
        if ($handler_id == 'ajax-latest') {
36 1
            midcom::get()->skip_page_style = true;
37
        }
38
39 5
        $data['datamanager'] = new datamanager($data['schemadb']);
40 5
        $qb = new org_openpsa_qbpager(midcom_db_article::class, 'net_nehmer_blog_index');
41 5
        $data['qb'] = $qb;
42 5
        $this->article_qb_constraints($qb);
43
44
        // Set default page title
45 5
        $data['page_title'] = $this->_topic->extra;
46
47
        // Filter by categories
48 5
        if (in_array($handler_id, ['index-category', 'latest-category'])) {
49 2
            $this->category = trim(strip_tags($args[0]));
50
51 2
            $this->_process_category_constraint($qb);
52
        }
53
54 5
        $qb->add_order('metadata.published', 'DESC');
55
56 5
        $qb->results_per_page = match ($handler_id) {
57 2
            'latest', 'ajax-latest' => $args[0],
58 3
            'latest-category' => $args[1],
59 1
            default => $this->_config->get('index_entries')
60
        };
61 2
62
        $this->_articles = $qb->execute();
63
64 5
        midcom::get()->metadata->set_request_metadata($this->get_last_modified(), $this->_topic->guid);
65
66 5
        if ($qb->get_current_page() > 1) {
67
            $this->add_breadcrumb(
68 5
                midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX),
0 ignored issues
show
Bug introduced by
It seems like midcom_core_context::get...M_CONTEXT_ANCHORPREFIX) can also be of type false; however, parameter $url of midcom_baseclasses_compo...ndler::add_breadcrumb() 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

68
                /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX),
Loading history...
69
                sprintf($this->_i18n->get_string('page %s', 'org.openpsa.qbpager'), $qb->get_current_page())
70
            );
71
        }
72
    }
73
74
    private function _process_category_constraint(org_openpsa_qbpager $qb)
75
    {
76 2
        if (   !in_array($this->category, $this->_request_data['categories'])
77
            && !$this->_config->get('categories_custom_enable')) {
78 2
            throw new midcom_error('Custom categories are not allowed');
79 2
            // TODO: Check here if there are actually items in this cat?
80
        }
81
82
        $this->apply_category_constraint($qb, $this->category);
83
84 2
        // Add category to title
85
        $this->_request_data['page_title'] = sprintf($this->_l10n->get('%s category %s'), $this->_topic->extra, $this->category);
86
        midcom::get()->head->set_pagetitle($this->_request_data['page_title']);
87 2
88 2
        // Activate correct leaf
89
        if (   $this->_config->get('show_navigation_pseudo_leaves')
90
            && in_array($this->category, $this->_request_data['categories'])) {
91 2
            $this->set_active_leaf($this->_topic->id . '_CAT_' . $this->category);
92 2
        }
93
94
        // Add RSS feed to headers
95
        if ($this->_config->get('rss_enable')) {
96
            midcom::get()->head->add_link_head([
97 2
                'rel'   => 'alternate',
98 2
                'type'  => 'application/rss+xml',
99 2
                'title' => $this->_l10n->get('rss 2.0 feed') . ": {$this->category}",
100 2
                'href'  => midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . "feeds/category/{$this->category}/",
0 ignored issues
show
Bug introduced by
Are you sure midcom_core_context::get...M_CONTEXT_ANCHORPREFIX) of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
                'href'  => /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . "feeds/category/{$this->category}/",
Loading history...
101 2
            ]);
102 2
        }
103 2
    }
104
105
    /**
106
     * Displays the index page
107
     */
108
    public function _show_index(string $handler_id, array &$data)
109
    {
110 5
        $data['index_fulltext'] = $this->_config->get('index_fulltext');
111
112 5
        if ($this->_config->get('ajax_comments_enable')) {
113
            if ($node = net_nehmer_comments_interface::get_node($this->_topic, $this->_config->get('comments_topic'))) {
114 5
                $data['base_ajax_comments_url'] = $node[MIDCOM_NAV_RELATIVEURL] . "comment/";
115
            }
116
        }
117
118
        midcom_show_style('index-start');
119
120 5
        if ($this->_articles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_articles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
121
            $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
122 5
            $total_count = count($this->_articles);
123 3
            $data['article_count'] = $total_count;
124 3
            foreach ($this->_articles as $article_counter => $article) {
125 3
                try {
126 3
                    $data['datamanager']->set_storage($article);
127
                } catch (midcom_error $e) {
128 3
                    $e->log();
129
                    continue;
130
                }
131
132
                $data['article'] = $article;
133
                $data['article_counter'] = $article_counter;
134 3
135 3
                $data['local_view_url'] = $prefix . $this->get_url($article);
0 ignored issues
show
Bug introduced by
Are you sure $prefix of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
                $data['local_view_url'] = /** @scrutinizer ignore-type */ $prefix . $this->get_url($article);
Loading history...
136
                $data['view_url'] = $this->get_url($article, true);
137 3
                if (!preg_match('/^http(s):\/\//', $data['view_url'])) {
138 3
                    $data['view_url'] = $prefix . $data['view_url'];
139 3
                }
140 3
                $data['linked'] = ($article->topic !== $this->_topic->id);
141
                if ($data['linked']) {
142 3
                    $nap = new midcom_helper_nav();
143 3
                    $data['node'] = $nap->get_node($article->topic);
144
                }
145
146
                midcom_show_style('index-item');
147
            }
148 3
        } else {
149
            midcom_show_style('index-empty');
150
        }
151 2
152
        midcom_show_style('index-end');
153
    }
154
}
155