net_nehmer_comments_cron_atom   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 61
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 59 7
1
<?php
2
/**
3
 * @package net.nehmer.comments
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
/**
10
 * Cron handler for fetching comments from Atom-comments feeds
11
 *
12
 * @package net.nehmer.comments
13
 */
14
class net_nehmer_comments_cron_atom extends midcom_baseclasses_components_cron_handler
15
{
16
    public function execute()
17
    {
18
        if (!$this->_config->get('atom_comments_import_enable')) {
19
            debug_add('Import of Atom comment feeds disabled, aborting', MIDCOM_LOG_INFO);
20
            return;
21
        }
22
23
        if (!midcom::get()->auth->request_sudo($this->_component)) {
24
            debug_add('Could not get sudo, aborting operation', MIDCOM_LOG_ERROR);
25
            return;
26
        }
27
28
        // Get 50 latest articles so we can look for those
29
        $qb = midcom_db_article::new_query_builder();
30
        $qb->add_constraint('topic.guid', '=', $this->_config->get('atom_comments_topic'));
31
        $qb->add_order('metadata.published', 'DESC');
32
        $qb->set_limit(50);
33
34
        foreach ($qb->execute() as $article) {
35
            $replies_url = $article->get_parameter('net.nemein.rss', 'replies_url');
36
37
            if (empty($replies_url)) {
38
                // no replies-url for this article. skipping
39
                continue;
40
            }
41
42
            // fetch and parse Feed from URL
43
            $comments = net_nemein_rss_fetch::raw_fetch($replies_url)->get_items();
44
45
            foreach ($comments as $comment) {
46
                $qb = net_nehmer_comments_comment::new_query_builder();
47
                $qb->add_constraint('remoteid', '=', $comment->get_id());
48
                $db_comments = $qb->execute();
49
50
                if (!empty($db_comments)) {
51
                    $db_comment = $db_comments[0];
52
53
                    $db_comment->title = $comment->get_title();
54
                    $db_comment->content = $comment->get_description();
55
                    $db_comment->update();
56
                } else {
57
                    $author_info = net_nemein_rss_fetch::parse_item_author($comment);
58
59
                    $db_comment = new net_nehmer_comments_comment();
60
                    $db_comment->objectguid = $article->guid;
61
                    $db_comment->metadata->published = $comment->get_date('Y-m-d H:i:s');
62
                    $db_comment->author = $author_info['full_name'] ?? $author_info['username'];
63
                    $db_comment->status = $this->_config->get('atom_comments_initial_status');
64
65
                    $db_comment->remoteid = $comment->get_id();
66
                    $db_comment->title = $comment->get_title();
67
                    $db_comment->content = $comment->get_description();
68
69
                    $db_comment->create();
70
                }
71
            } // <-- comments
72
        } // <-- articles
73
74
        midcom::get()->auth->drop_sudo();
75
    }
76
}
77