Passed
Push — master ( 335385...121bf2 )
by Andreas
18:20
created

net_nehmer_blog_navigation::_add_pseudo_leaves()   C

Complexity

Conditions 12
Paths 32

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 52.856

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 34
c 1
b 0
f 0
nc 32
nop 1
dl 0
loc 49
ccs 12
cts 35
cp 0.3429
crap 52.856
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
/**
10
 * Blog navigation interface class
11
 *
12
 * @package net.nehmer.blog
13
 */
14
class net_nehmer_blog_navigation extends midcom_baseclasses_components_navigation
15
{
16
    const LEAFID_FEEDS = 2;
17
18
    /**
19
     * Returns a static leaf list with access to the archive.
20
     */
21 1
    public function get_leaves() : array
22
    {
23 1
        $leaves = [];
24
25 1
        if ($this->_config->get('show_navigation_pseudo_leaves')) {
26 1
            $this->_add_pseudo_leaves($leaves);
27
        }
28
29 1
        if ($this->_config->get('show_latest_in_navigation')) {
30
            $this->_add_article_leaves($leaves);
31
        }
32 1
        return $leaves;
33
    }
34
35
    private function _add_article_leaves(array &$leaves)
36
    {
37
        $qb = midcom_db_article::new_query_builder();
38
        self::add_scheduling_constraints($qb, $this->_config);
39
40
        $qb->add_constraint('topic', '=', $this->_topic->id);
41
        $qb->add_constraint('up', '=', 0);
42
        $qb->add_order('metadata.published', 'DESC');
43
        $qb->set_limit((int) $this->_config->get('index_entries'));
44
45
        foreach ($qb->execute() as $article) {
46
            $leaves[$article->id] = [
47
                MIDCOM_NAV_URL => $this->_get_url($article),
48
                MIDCOM_NAV_NAME => $article->title ?: $article->name,
49
                MIDCOM_NAV_GUID => $article->guid,
50
                MIDCOM_NAV_OBJECT => $article,
51
            ];
52
        }
53
    }
54
55
    private function _get_url(midcom_db_article $article) : string
56
    {
57
        $view_url = $article->name ?: $article->guid;
58
59
        if ($this->_config->get('view_in_url')) {
60
            $view_url = 'view/' . $view_url;
61
        }
62
        return $view_url . '/';
63
    }
64
65 1
    private function _add_pseudo_leaves(array &$leaves)
66
    {
67 1
        if (   $this->_config->get('archive_enable')
68 1
            && $this->_config->get('archive_in_navigation')) {
69 1
            $leaves["{$this->_topic->id}_ARCHIVE"] = [
70 1
                MIDCOM_NAV_URL => "archive/",
71 1
                MIDCOM_NAV_NAME => $this->_l10n->get('archive'),
72
            ];
73
        }
74 1
        if (   $this->_config->get('rss_enable')
75 1
            && $this->_config->get('feeds_in_navigation')) {
76
            $leaves[self::LEAFID_FEEDS] = [
77
                MIDCOM_NAV_URL => "feeds/",
78
                MIDCOM_NAV_NAME => $this->_l10n->get('available feeds'),
79
            ];
80
        }
81
82 1
        if (   $this->_config->get('categories_in_navigation')
83 1
            && $this->_config->get('categories') != '') {
84
            $categories = explode(',', $this->_config->get('categories'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('categories') can also be of type false; however, parameter $string of explode() 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

84
            $categories = explode(',', /** @scrutinizer ignore-type */ $this->_config->get('categories'));
Loading history...
85
            foreach ($categories as $category) {
86
                $leaves["{$this->_topic->id}_CAT_{$category}"] = [
87
                    MIDCOM_NAV_URL => "category/{$category}/",
88
                    MIDCOM_NAV_NAME => $category,
89
                ];
90
            }
91
        }
92
93 1
        if (   $this->_config->get('archive_years_in_navigation')
94 1
            && $this->_config->get('archive_years_enable')) {
95
            $qb = midcom_db_article::new_query_builder();
96
            $qb->add_constraint('topic', '=', $this->_topic->id);
97
            self::add_scheduling_constraints($qb, $this->_config);
98
99
            $qb->add_order('metadata.published');
100
            $qb->set_limit(1);
101
102
            if ($result = $qb->execute_unchecked()) {
103
                $first_year = (int) gmdate('Y', (int) $result[0]->metadata->published);
104
                $year = $first_year;
105
                $this_year = (int) gmdate('Y', time());
106
                while ($year <= $this_year) {
107
                    $leaves["{$this->_topic->id}_ARCHIVE_{$year}"] = [
108
                        MIDCOM_NAV_URL => "archive/year/{$year}/",
109
                        MIDCOM_NAV_NAME => $year,
110
                    ];
111
                    $year++;
112
                }
113
                $leaves = array_reverse($leaves);
114
            }
115
        }
116 1
    }
117
118
    /**
119
     * Hide the articles that have the publish time in the future if
120
     * the user is not administrator
121
     *
122
     * @param midcom_core_querybuilder $qb (or qbpager)
123
     */
124 10
    public static function add_scheduling_constraints($qb, midcom_helper_configuration $config)
125
    {
126 10
        if (   $config->get('enable_scheduled_publishing')
127 10
            && !midcom::get()->auth->admin) {
128
            // Show the article only if the publishing time has passed or the viewer
129
            // is the author
130
            $qb->begin_group('OR');
131
            $qb->add_constraint('metadata.published', '<', gmdate('Y-m-d H:i:s'));
132
133
            if (!empty(midcom::get()->auth->user->guid)) {
134
                $qb->add_constraint('metadata.authors', 'LIKE', '%|' . midcom::get()->auth->user->guid . '|%');
135
            }
136
            $qb->end_group();
137
        }
138 10
    }
139
}
140