Completed
Push — master ( da2115...dd0feb )
by Andreas
18:06
created

net_nehmer_blog_handler_index::_handler_index()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.0798

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 24
nop 3
dl 0
loc 39
ccs 20
cts 23
cp 0.8696
crap 6.0798
rs 8.9297
c 0
b 0
f 0
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: $admin, $name, $auth, $url, $guid, $revised, $metadata, $user
Loading history...
21
22
    /**
23
     * The articles to display
24
     *
25
     * @var Array
26
     */
27
    private $_articles;
28
29
    /**
30
     * Shows the autoindex list. Nothing to do in the handle phase except setting last modified
31
     * dates.
32
     *
33
     * @param mixed $handler_id The ID of the handler.
34
     * @param array $args The argument list.
35
     * @param array $data The local request data.
36
     */
37 5
    public function _handler_index($handler_id, array $args, array &$data)
38
    {
39 5
        if ($handler_id == 'ajax-latest') {
40 1
            midcom::get()->skip_page_style = true;
41
        }
42
43 5
        $data['datamanager'] = new datamanager($data['schemadb']);
44 5
        $qb = new org_openpsa_qbpager(midcom_db_article::class, 'net_nehmer_blog_index');
45 5
        $data['qb'] = $qb;
46 5
        $this->article_qb_constraints($qb);
0 ignored issues
show
Bug introduced by
$qb of type org_openpsa_qbpager is incompatible with the type midcom_core_querybuilder expected by parameter $qb of net_nehmer_blog_handler_...rticle_qb_constraints(). ( Ignorable by Annotation )

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

46
        $this->article_qb_constraints(/** @scrutinizer ignore-type */ $qb);
Loading history...
47
48
        // Set default page title
49 5
        $data['page_title'] = $this->_topic->extra;
50
51
        // Filter by categories
52 5
        if (in_array($handler_id, ['index-category', 'latest-category'])) {
53 2
            $data['category'] = trim(strip_tags($args[0]));
54
55 2
            $this->_process_category_constraint($qb);
56
        }
57
58 5
        $qb->add_order('metadata.published', 'DESC');
59
60 5
        if (in_array($handler_id, ['latest', 'ajax-latest'])) {
61 2
            $qb->results_per_page = $args[0];
62 3
        } elseif ($handler_id == 'latest-category') {
63 1
            $qb->results_per_page = $args[1];
64
        } else {
65 2
            $qb->results_per_page = $this->_config->get('index_entries');
66
        }
67
68 5
        $this->_articles = $qb->execute();
0 ignored issues
show
Documentation Bug introduced by
It seems like $qb->execute() can also be of type false. However, the property $_articles is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
70 5
        midcom::get()->metadata->set_request_metadata($this->get_last_modified(), $this->_topic->guid);
71
72 5
        if ($qb->get_current_page() > 1) {
73
            $this->add_breadcrumb(
74
                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

74
                /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX),
Loading history...
75
                sprintf($this->_i18n->get_string('page %s', 'org.openpsa.qbpager'), $qb->get_current_page())
76
            );
77
        }
78 5
    }
79
80 2
    private function _process_category_constraint($qb)
81
    {
82 2
        if (!in_array($this->_request_data['category'], $this->_request_data['categories'])) {
83 2
            if (!$this->_config->get('categories_custom_enable')) {
84
                throw new midcom_error('Custom categories are not allowed');
85
            }
86
            // TODO: Check here if there are actually items in this cat?
87
        }
88
89 2
        $this->apply_category_constraint($qb, $this->_request_data['category']);
90
91
        // Add category to title
92 2
        $this->_request_data['page_title'] = sprintf($this->_l10n->get('%s category %s'), $this->_topic->extra, $this->_request_data['category']);
93 2
        midcom::get()->head->set_pagetitle($this->_request_data['page_title']);
94
95
        // Activate correct leaf
96 2
        if (   $this->_config->get('show_navigation_pseudo_leaves')
97 2
            && in_array($this->_request_data['category'], $this->_request_data['categories'])) {
98
            $this->set_active_leaf($this->_topic->id . '_CAT_' . $this->_request_data['category']);
99
        }
100
101
        // Add RSS feed to headers
102 2
        if ($this->_config->get('rss_enable')) {
103 2
            midcom::get()->head->add_link_head([
104 2
                'rel'   => 'alternate',
105 2
                'type'  => 'application/rss+xml',
106 2
                'title' => $this->_l10n->get('rss 2.0 feed') . ": {$this->_request_data['category']}",
107 2
                'href'  => midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . "feeds/category/{$this->_request_data['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

107
                'href'  => /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . "feeds/category/{$this->_request_data['category']}/",
Loading history...
108
            ]);
109
        }
110 2
    }
111
112
    /**
113
     * Displays the index page
114
     *
115
     * @param mixed $handler_id The ID of the handler.
116
     * @param array $data The local request data.
117
     */
118 5
    public function _show_index($handler_id, array &$data)
119
    {
120 5
        $data['index_fulltext'] = $this->_config->get('index_fulltext');
121
122 5
        if ($this->_config->get('ajax_comments_enable')) {
123
            if ($comments_node = $this->_seek_comments()) {
124
                $data['ajax_comments_enable'] = true;
125
                $data['base_ajax_comments_url'] = $comments_node[MIDCOM_NAV_RELATIVEURL] . "comment/";
126
            }
127
        }
128
129 5
        midcom_show_style('index-start');
130
131 5
        $data['comments_enable'] = (bool) $this->_config->get('comments_enable');
132
133 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...
134 3
            $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
135 3
            $total_count = count($this->_articles);
136 3
            $data['article_count'] = $total_count;
137 3
            foreach ($this->_articles as $article_counter => $article) {
138
                try {
139 3
                    $data['datamanager']->set_storage($article);
140
                } catch (midcom_error $e) {
141
                    $e->log();
142
                    continue;
143
                }
144
145 3
                $data['article'] = $article;
146 3
                $data['article_counter'] = $article_counter;
147
148 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

148
                $data['local_view_url'] = /** @scrutinizer ignore-type */ $prefix . $this->get_url($article);
Loading history...
149 3
                $data['view_url'] = $this->get_url($article, true);
150 3
                if (!preg_match('/^http(s):\/\//', $data['view_url'])) {
151 3
                    $data['view_url'] = $prefix . $data['view_url'];
152
                }
153 3
                $data['linked'] = ($article->topic !== $this->_topic->id);
154 3
                if ($data['linked']) {
155
                    $nap = new midcom_helper_nav();
156
                    $data['node'] = $nap->get_node($article->topic);
157
                }
158
159 3
                midcom_show_style('index-item');
160
            }
161
        } else {
162 2
            midcom_show_style('index-empty');
163
        }
164
165 5
        midcom_show_style('index-end');
166 5
    }
167
168
    // helpers follow
169
    /**
170
     * Try to find a comments node (cache results)
171
     */
172
    private function _seek_comments()
173
    {
174
        if ($this->_config->get('comments_topic')) {
175
            try {
176
                $comments_topic = new midcom_db_topic($this->_config->get('comments_topic'));
177
            } catch (midcom_error $e) {
178
                return false;
179
            }
180
181
            // We got a topic. Make it a NAP node
182
            $nap = new midcom_helper_nav();
183
            return $nap->get_node($comments_topic->id);
184
        }
185
186
        // No comments topic specified, autoprobe
187
        $comments_node = midcom_helper_misc::find_node_by_component('net.nehmer.comments');
188
189
        // Cache the data
190
        if (midcom::get()->auth->request_sudo('net.nehmer.blog')) {
191
            $this->_topic->set_parameter('net.nehmer.blog', 'comments_topic', $comments_node[MIDCOM_NAV_GUID]);
192
            midcom::get()->auth->drop_sudo();
193
        }
194
195
        return $comments_node;
196
    }
197
}
198