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

net_nehmer_comments_handler_moderate::reply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use Symfony\Component\HttpFoundation\Request;
10
11
/**
12
 * Comments moderation handler
13
 *
14
 * @package net.nehmer.comments
15
 */
16
class net_nehmer_comments_handler_moderate extends midcom_baseclasses_components_handler
17
{
18
    /**
19
     * Marks comment as possible abuse
20
     */
21 1
    public function _handler_report_abuse(Request $request, string $guid)
22
    {
23 1
        $comment = $this->load_comment($guid, false);
24
25 1
        if ($comment->status !== net_nehmer_comments_comment::MODERATED) {
26 1
            if ($comment->can_do('net.nehmer.comments:moderation')) {
27 1
                $status = net_nehmer_comments_comment::ABUSE;
28
            } else {
29
                $status = net_nehmer_comments_comment::REPORTED_ABUSE;
30
            }
31 1
            $sudo = false;
32 1
            if (!$comment->can_do('midgard:update')) {
33
                $sudo = midcom::get()->auth->request_sudo('net.nehmer.comments');
34
            }
35
36 1
            if ($comment->moderate($status, 'reported_abuse')) {
37 1
                if ($moderators = $this->_config->get('moderators')) {
38
                    $this->notify_moderators($moderators, $comment);
39
                }
40
            }
41 1
            if ($sudo) {
42
                midcom::get()->auth->drop_sudo();
43
            }
44
        }
45
46 1
        return $this->reply($request);
47
    }
48
49
    private function notify_moderators(string $moderators, net_nehmer_comments_comment $comment)
50
    {
51
        // Prepare notification message
52
        $message = [];
53
        $message['title'] = sprintf($this->_l10n->get('comment %s reported as abuse'), $comment->title);
54
        $message['content'] = '';
55
        if ($logs = $comment->get_logs()) {
56
            $message['content'] .= $this->_l10n->get('moderation history').":\n\n";
57
            foreach ($logs as $time => $log) {
58
                $reported = $this->_l10n->get_formatter()->datetime(strtotime("{$time}Z"));
59
                $message['content'] .= $this->_l10n->get(sprintf('%s: %s by %s (from %s)', "$reported:\n", $this->_l10n->get($log['action']), $log['reporter'], $log['ip'])) . "\n\n";
60
            }
61
        }
62
        $message['content'] = "\n\n" . midcom::get()->permalinks->create_permalink($comment->objectguid);
63
64
        $message['abstract'] = sprintf($this->_l10n->get('comment %s reported as abuse'), $comment->title);
65
        $message['abstract'] .= " " . midcom::get()->permalinks->create_permalink($comment->objectguid);
66
67
        // Notify moderators
68
        $moderator_guids = explode('|', $moderators);
69
        foreach (array_filter($moderator_guids) as $moderator_guid) {
70
            org_openpsa_notifications::notify('net.nehmer.comments:report_abuse', $moderator_guid, $message);
71
        }
72
    }
73
74
    /**
75
     * Marks comment as not abuse
76
     */
77
    public function _handler_not_abuse(Request $request, string $guid)
78
    {
79
        $comment = $this->load_comment($guid);
80
        $comment->moderate(net_nehmer_comments_comment::MODERATED, 'reported_not_abuse');
81
        return $this->reply($request);
82
    }
83
84
    public function _handler_confirm_report(Request $request, string $action, string $guid)
85
    {
86
        $comment = $this->load_comment($guid);
87
        if ($comment->status !== net_nehmer_comments_comment::MODERATED) {
88
            if ($action == 'confirm_abuse') {
89
                $comment->moderate(net_nehmer_comments_comment::ABUSE, 'confirmed_abuse');
90
            } else {
91
                $comment->moderate(net_nehmer_comments_comment::JUNK, 'confirmed_junk');
92
            }
93
        }
94
        midcom::get()->indexer->delete([$comment->guid]);
95
        return $this->reply($request);
96
    }
97
98 1
    private function load_comment(string $identifier, bool $require_moderation_privilege = true) : net_nehmer_comments_comment
99
    {
100 1
        $comment = new net_nehmer_comments_comment($identifier);
101
102 1
        if ($require_moderation_privilege) {
103
            $comment->require_do('net.nehmer.comments:moderation');
104
        }
105 1
        return $comment;
106
    }
107
108 1
    private function reply(Request $request) : midcom_response_relocate
109
    {
110 1
        return new midcom_response_relocate($request->request->get('return_url', ''));
111
    }
112
}
113