Passed
Push — master ( 92128b...d5c2c1 )
by Andreas
17:19
created

net_nemein_rss_handler_admin::_subscribe_feed()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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