Passed
Push — master ( fef947...832870 )
by Andreas
12:13
created

net_nemein_rss_handler_admin::_subscribe_feed()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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