Completed
Push — master ( d38dfd...18874d )
by Andreas
18:20
created

net_nehmer_blog_handler_feed::_create_feed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 12
ccs 8
cts 9
cp 0.8889
crap 2.0054
rs 9.9666
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 Feed handler
13
 *
14
 * Prints the various supported feeds using the FeedCreator library.
15
 *
16
 * @package net.nehmer.blog
17
 */
18
class net_nehmer_blog_handler_feed 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_feed: $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
     * The feedcreator instance used.
31
     *
32
     * @var UniversalFeedCreator
33
     */
34
    private $_feed;
35
36
    /**
37
     * Shows the autoindex list. Nothing to do in the handle phase except setting last modified
38
     * dates.
39
     *
40
     * @param mixed $handler_id The ID of the handler.
41
     * @param array $args The argument list.
42
     * @param array $data The local request data.
43
     */
44 1
    public function _handler_feed($handler_id, array $args, array &$data)
45
    {
46 1
        midcom::get()->cache->content->content_type("text/xml; charset=UTF-8");
47 1
        midcom::get()->skip_page_style = true;
48
49
        // Prepare control structures
50 1
        $data['datamanager'] = new datamanager($data['schemadb']);
51
52
        // Get the articles,
53 1
        $qb = midcom_db_article::new_query_builder();
54 1
        $this->article_qb_constraints($qb);
55
56 1
        $qb->add_order('metadata.published', 'DESC');
57
58 1
        if ($handler_id == 'feed-category-rss2') {
59 1
            if (!in_array($args[0], $this->_request_data['categories'])) {
60
                // This is not a predefined category from configuration, check if site maintainer allows us to show it
61 1
                if (!$this->_config->get('categories_custom_enable')) {
62
                    throw new midcom_error('Custom category support is disabled');
63
                }
64
            }
65
66
            // TODO: Check for ".xml" suffix
67 1
            $this->_request_data['category'] = trim(strip_tags($args[0]));
68
69 1
            $this->apply_category_constraint($qb, $this->_request_data['category']);
70
        }
71
72 1
        $qb->set_limit($this->_config->get('rss_count'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('rss_count') can also be of type false; however, parameter $limit of midcom_core_query::set_limit() does only seem to accept integer, 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

72
        $qb->set_limit(/** @scrutinizer ignore-type */ $this->_config->get('rss_count'));
Loading history...
73
74 1
        $this->_articles = $qb->execute();
75
76
        // Prepare the feed (this will also validate the handler_id)
77 1
        $this->_create_feed($handler_id);
78
79 1
        midcom::get()->metadata->set_request_metadata($this->get_last_modified(), $this->_topic->guid);
80 1
    }
81
82
    /**
83
     * Creates the Feedcreator instance.
84
     */
85 1
    private function _create_feed($handler_id)
86
    {
87 1
        $this->_feed = new UniversalFeedCreator();
88 1
        if ($this->_config->get('rss_title')) {
89
            $this->_feed->title = $this->_config->get('rss_title');
90
        } else {
91 1
            $this->_feed->title = $this->_topic->extra;
92
        }
93 1
        $this->_feed->description = $this->_config->get('rss_description');
94 1
        $this->_feed->language = $this->_config->get('rss_language');
95 1
        $this->_feed->editor = $this->_config->get('rss_webmaster');
96 1
        $this->_feed->link = midcom::get()->get_host_name() . midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
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

96
        $this->_feed->link = midcom::get()->get_host_name() . /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
Loading history...
97 1
    }
98
99
    /**
100
     * Displays the feed
101
     *
102
     * @param mixed $handler_id The ID of the handler.
103
     * @param array $data The local request data.
104
     */
105 1
    public function _show_feed($handler_id, array &$data)
106
    {
107 1
        $data['feedcreator'] = $this->_feed;
108
109
        // Add each article now.
110 1
        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...
111 1
            foreach ($this->_articles as $article) {
112 1
                $data['datamanager']->set_storage($article);
113 1
                $data['article'] = $article;
114 1
                midcom_show_style('feeds-item');
115
            }
116
        }
117
118
        switch ($handler_id) {
119 1
            case 'feed-category-rss2':
120 1
                $this->_feed->title = sprintf($this->_l10n->get('%s category %s'), $this->_feed->title, $data['category']);
121 1
                $this->_feed->syndicationURL = "{$this->_feed->link}feeds/category/{$data['category']}";
122
                // Fall-through
123
124 1
            case 'feed-rss2':
125 1
                echo $this->_feed->createFeed('RSS2.0');
126 1
                break;
127
128
            case 'feed-rss1':
129
                $this->_feed->syndicationURL = $this->_feed->link . 'rss1.xml';
130
                echo $this->_feed->createFeed('RSS1.0');
131
                break;
132
133
            case 'feed-rss091':
134
                echo $this->_feed->createFeed('RSS0.91');
135
                break;
136
137
            case 'feed-atom':
138
                $this->_feed->syndicationURL = $this->_feed->link . 'atom.xml';
139
                echo $this->_feed->createFeed('ATOM');
140
                break;
141
        }
142 1
    }
143
144
    /**
145
     * Shows a simple available-feeds page.
146
     */
147 1
    public function _handler_index()
148
    {
149 1
        $this->set_active_leaf(net_nehmer_blog_navigation::LEAFID_FEEDS);
150 1
        midcom::get()->metadata->set_request_metadata($this->_topic->metadata->revised, $this->_topic->guid);
151
152 1
        return $this->show('feeds');
153
    }
154
}
155