Passed
Push — master ( 4648d9...4b6b05 )
by Andreas
18:53
created

net_nemein_rss_handler_admin   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 44.29%

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 134
ccs 31
cts 70
cp 0.4429
rs 10
c 0
b 0
f 0
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _load_controller() 0 5 1
B _handler_subscribe() 0 43 7
A _handler_edit() 0 22 3
A _update_breadcrumb_line() 0 10 3
A _handler_delete() 0 8 1
A _subscribe_feed() 0 27 4
1
<?php
2
/**
3
 * @package net.nemein.rss
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
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * Feed management class.
14
 *
15
 * @package net.nemein.rss
16
 */
17
class net_nemein_rss_handler_admin extends midcom_baseclasses_components_handler
18
{
19 1
    private function _load_controller(array &$data)
20
    {
21 1
        $data['controller'] = datamanager::from_schemadb($this->_config->get('schemadb_feed'))
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('schemadb_feed') can also be of type false; however, parameter $path of midcom\datamanager\datamanager::from_schemadb() 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

21
        $data['controller'] = datamanager::from_schemadb(/** @scrutinizer ignore-type */ $this->_config->get('schemadb_feed'))
Loading history...
22 1
            ->set_storage($data['feed'])
23 1
            ->get_controller();
24 1
    }
25
26
    private function _subscribe_feed(string $feed_url, string $feed_title = null)
27
    {
28
        // Try to fetch the new feed
29
        $rss = net_nemein_rss_fetch::raw_fetch($feed_url);
30
        // TODO: display error on invalid feed
31
32
        if (!$feed_title) {
33
            // If we didn't get the channel title preset
34
            $feed_title = $rss->get_title() ?: '';
35
        }
36
37
        // Find out if the URL is already subscribed, and update it in that case
38
        $qb = net_nemein_rss_feed_dba::new_query_builder();
39
        $qb->add_constraint('node', '=', $this->_topic->id);
40
        $qb->add_constraint('url', '=', $feed_url);
41
        $feeds = $qb->execute();
42
        if (empty($feeds)) {
43
            $feed = new net_nemein_rss_feed_dba();
44
            $feed->node = $this->_topic->id;
45
            $feed->url = $feed_url;
46
            $feed->title = $feed_title;
47
            $feed->create();
48
        } else {
49
            // If we're updating existing feed
50
            $feed = $feeds[0];
51
            $feed->title = $feed_title;
52
            $feed->update();
53
        }
54
    }
55
56 1
    public function _handler_subscribe(Request $request, string $handler_id)
57
    {
58 1
        $this->_topic->require_do('midgard:create');
59
60
        // Single feed addition
61 1
        $post = $request->request->get('net_nemein_rss_manage_newfeed');
62 1
        if (!empty($post['url'])) {
63
            $this->_subscribe_feed($post['url']);
64
            // TODO: display error messages
65
            // TODO: redirect user to edit page if creation succeeded
66
67
            return new midcom_response_relocate($this->router->generate('feeds_list'));
68
        }
69
70
        // OPML subscription list import support
71 1
        if (   array_key_exists('net_nemein_rss_manage_opml', $_FILES)
72 1
            && is_uploaded_file($_FILES['net_nemein_rss_manage_opml']['tmp_name'])) {
73
            $opml_file = $_FILES['net_nemein_rss_manage_opml']['tmp_name'];
74
75
            // We have OPML file, parse it
76
            $opml_data = file_get_contents($opml_file);
77
            unlink($opml_file);
78
79
            $opml_parser = xml_parser_create();
80
            xml_parse_into_struct($opml_parser, $opml_data, $opml_values);
81
            foreach ($opml_values as $opml_element) {
82
                if ($opml_element['tag'] === 'OUTLINE') {
83
                    // Subscribe to found channels
84
                    if (isset($opml_element['attributes']['TITLE'])) {
85
                        $this->_subscribe_feed($opml_element['attributes']['XMLURL'], $opml_element['attributes']['TITLE']);
86
                    } else {
87
                        $this->_subscribe_feed($opml_element['attributes']['XMLURL']);
88
                    }
89
                }
90
            }
91
            xml_parser_free($opml_parser);
92
93
            return new midcom_response_relocate($this->router->generate('feeds_list'));
94
        }
95
96 1
        $this->_update_breadcrumb_line($handler_id);
97
98 1
        return $this->show('net-nemein-rss-feeds-subscribe');
99
    }
100
101 1
    public function _handler_edit(Request $request, string $handler_id, string $guid, array &$data)
102
    {
103 1
        $data['feed'] = new net_nemein_rss_feed_dba($guid);
104 1
        $data['feed']->require_do('midgard:update');
105
106 1
        $this->_load_controller($data);
107
108 1
        switch ($data['controller']->handle($request)) {
109 1
            case 'save':
110
                // TODO: Fetch the feed here?
111
                // *** FALL-THROUGH ***
112
113 1
            case 'cancel':
114
                return new midcom_response_relocate($this->router->generate('feeds_list'));
115
        }
116
117 1
        midcom::get()->metadata->set_request_metadata($data['feed']->metadata->revised, $data['feed']->guid);
118 1
        $this->bind_view_to_object($data['feed']);
119
120 1
        $this->_update_breadcrumb_line($handler_id);
121
122 1
        return $this->show('net-nemein-rss-feed-edit');
123
    }
124
125
    /**
126
     * Displays a delete confirmation view.
127
     */
128
    public function _handler_delete(Request $request, string $guid)
129
    {
130
        $feed = new net_nemein_rss_feed_dba($guid);
131
        $workflow = $this->get_workflow('delete', [
132
            'object' => $feed,
133
            'success_url' => $this->router->generate('feeds_list')
134
        ]);
135
        return $workflow->run($request);
136
    }
137
138
    /**
139
     * Update the context so that we get a complete breadcrumb line towards the current location.
140
     */
141 2
    private function _update_breadcrumb_line(string $handler_id)
142
    {
143 2
        $this->add_breadcrumb($this->router->generate('feeds_list'), $this->_l10n->get('manage feeds'));
144
145 2
        if ($handler_id == 'feeds_subscribe') {
146 1
            $this->add_breadcrumb($this->router->generate('feeds_subscribe'), $this->_l10n->get('subscribe feeds'));
147 1
        } elseif ($handler_id == 'feeds_edit') {
148 1
            $this->add_breadcrumb($this->router->generate('feeds_edit', ['guid' => $this->_request_data['feed']->guid]), $this->_l10n_midcom->get('edit'));
149
        }
150 2
        net_nemein_rss_manage::add_toolbar_buttons($this->_node_toolbar);
151 2
    }
152
}
153