Passed
Push — master ( f0c99e...e6fe94 )
by Andreas
11:48
created

net_nehmer_blog_handler_feed::_handler_feed()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 35
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0032

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 3
nop 2
dl 0
loc 35
ccs 16
cts 17
cp 0.9412
crap 4.0032
rs 9.7333
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: $name, $url, $guid, $revised, $metadata
Loading history...
21
22
    /**
23
     * @var midcom_db_article[]
24
     */
25
    private array $_articles;
26
27
    private UniversalFeedCreator $_feed;
28
29
    private string $category;
30
31
    /**
32
     * Shows the autoindex list. Nothing to do in the handle phase except setting last modified
33
     * dates.
34
     */
35 1
    public function _handler_feed(array &$data, ?string $category = null)
36
    {
37 1
        midcom::get()->cache->content->content_type("text/xml; charset=UTF-8");
38 1
        midcom::get()->skip_page_style = true;
39
40
        // Prepare control structures
41 1
        $data['datamanager'] = new datamanager($data['schemadb']);
42
43
        // Get the articles,
44 1
        $qb = midcom_db_article::new_query_builder();
45 1
        $this->article_qb_constraints($qb);
46
47 1
        $qb->add_order('metadata.published', 'DESC');
48
49 1
        if ($category) {
50
            // This is not a predefined category from configuration, check if site maintainer allows us to show it
51 1
            if (   !in_array($category, $data['categories'])
52 1
                && !$this->_config->get('categories_custom_enable')) {
53
                throw new midcom_error('Custom category support is disabled');
54
            }
55
56
            // TODO: Check for ".xml" suffix
57 1
            $this->category = trim(strip_tags($category));
58
59 1
            $this->apply_category_constraint($qb, $this->category);
60
        }
61
62 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

62
        $qb->set_limit(/** @scrutinizer ignore-type */ $this->_config->get('rss_count'));
Loading history...
63
64 1
        $this->_articles = $qb->execute();
65
66
        // Prepare the feed
67 1
        $this->_create_feed();
68
69 1
        midcom::get()->metadata->set_request_metadata($this->get_last_modified(), $this->_topic->guid);
70
    }
71
72
    /**
73
     * Creates the Feedcreator instance.
74
     */
75 1
    private function _create_feed()
76
    {
77 1
        $this->_feed = new UniversalFeedCreator();
78 1
        if ($this->_config->get('rss_title')) {
79
            $this->_feed->title = $this->_config->get('rss_title');
80
        } else {
81 1
            $this->_feed->title = $this->_topic->extra;
82
        }
83 1
        $this->_feed->description = $this->_config->get('rss_description');
84 1
        $this->_feed->language = $this->_config->get('rss_language');
85 1
        $this->_feed->editor = $this->_config->get('rss_webmaster');
86 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

86
        $this->_feed->link = midcom::get()->get_host_name() . /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
Loading history...
87
    }
88
89
    /**
90
     * Displays the feed
91
     */
92 1
    public function _show_feed(string $handler_id, array &$data)
93
    {
94 1
        $data['feedcreator'] = $this->_feed;
95
96
        // Add each article now.
97 1
        foreach ($this->_articles as $article) {
98 1
            $data['datamanager']->set_storage($article);
99 1
            $data['article'] = $article;
100 1
            midcom_show_style('feeds-item');
101
        }
102
103
        switch ($handler_id) {
104 1
            case 'feed-category-rss2':
105 1
                $this->_feed->title = sprintf($this->_l10n->get('%s category %s'), $this->_feed->title, $this->category);
106 1
                $this->_feed->syndicationURL = "{$this->_feed->link}feeds/category/{$this->category}";
107
                // Fall-through
108
109 1
            case 'feed-rss2':
110 1
                echo $this->_feed->createFeed('RSS2.0');
111 1
                break;
112
113
            case 'feed-rss1':
114
                $this->_feed->syndicationURL = $this->_feed->link . 'rss1.xml';
115
                echo $this->_feed->createFeed('RSS1.0');
116
                break;
117
118
            case 'feed-rss091':
119
                echo $this->_feed->createFeed('RSS0.91');
120
                break;
121
122
            case 'feed-atom':
123
                $this->_feed->syndicationURL = $this->_feed->link . 'atom.xml';
124
                echo $this->_feed->createFeed('ATOM');
125
                break;
126
        }
127
    }
128
129
    /**
130
     * Shows a simple available-feeds page.
131
     */
132 1
    public function _handler_index()
133
    {
134 1
        $this->set_active_leaf(net_nehmer_blog_navigation::LEAFID_FEEDS);
135 1
        midcom::get()->metadata->set_request_metadata($this->_topic->metadata->revised, $this->_topic->guid);
136
137 1
        return $this->show('feeds');
138
    }
139
}
140