Passed
Push — master ( 00519f...366a1b )
by Andreas
17:30
created

notify_moderators()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 1
dl 0
loc 23
ccs 0
cts 16
cp 0
crap 20
rs 9.7666
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
     * Comment we are currently working with.
20
     *
21
     * @var net_nehmer_comments_comment
22
     */
23
    private $_comment;
24
25
    /**
26
     * Marks comment as possible abuse
27
     */
28 1
    public function _handler_report_abuse(Request $request, string $guid)
29
    {
30 1
        $this->load_comment($guid, false);
31
32 1
        if ($this->_comment->status !== net_nehmer_comments_comment::MODERATED) {
33 1
            if ($this->_comment->can_do('net.nehmer.comments:moderation')) {
34 1
                $status = net_nehmer_comments_comment::ABUSE;
35
            } else {
36
                $status = net_nehmer_comments_comment::REPORTED_ABUSE;
37
            }
38 1
            $sudo = false;
39 1
            if (!$this->_comment->can_do('midgard:update')) {
40
                $sudo = midcom::get()->auth->request_sudo('net.nehmer.comments');
41
            }
42
43 1
            if ($this->_comment->moderate($status, 'reported_abuse')) {
44 1
                $moderators = $this->_config->get('moderators');
0 ignored issues
show
Unused Code introduced by
The assignment to $moderators is dead and can be removed.
Loading history...
45 1
                if ($moderators = $this->_config->get('moderators')) {
46
                    $this->notify_moderators($moderators);
47
                }
48
            }
49 1
            if ($sudo) {
50
                midcom::get()->auth->drop_sudo();
51
            }
52
        }
53
54 1
        return $this->reply($request);
55
    }
56
57
    private function notify_moderators(string $moderators)
58
    {
59
        // Prepare notification message
60
        $message = [];
61
        $message['title'] = sprintf($this->_l10n->get('comment %s reported as abuse'), $this->_comment->title);
62
        $message['content'] = '';
63
        $logs = $this->_comment->get_logs();
64
        if (!empty($logs)) {
65
            $message['content'] .= $this->_l10n->get('moderation history').":\n\n";
66
            foreach ($logs as $time => $log) {
67
                $reported = $this->_l10n->get_formatter()->datetime(strtotime("{$time}Z"));
68
                $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";
69
            }
70
        }
71
        $message['content'] = "\n\n" . midcom::get()->permalinks->create_permalink($this->_comment->objectguid);
72
73
        $message['abstract'] = sprintf($this->_l10n->get('comment %s reported as abuse'), $this->_comment->title);
74
        $message['abstract'] .= " " . midcom::get()->permalinks->create_permalink($this->_comment->objectguid);
75
76
        // Notify moderators
77
        $moderator_guids = explode('|', $moderators);
78
        foreach (array_filter($moderator_guids) as $moderator_guid) {
79
            org_openpsa_notifications::notify('net.nehmer.comments:report_abuse', $moderator_guid, $message);
80
        }
81
    }
82
83
    /**
84
     * Marks comment as not abuse
85
     */
86
    public function _handler_not_abuse(Request $request, string $guid)
87
    {
88
        $this->load_comment($guid);
89
        $this->_comment->moderate(net_nehmer_comments_comment::MODERATED, 'reported_not_abuse');
90
        return $this->reply($request);
91
    }
92
93
    public function _handler_confirm_report(Request $request, string $action, string $guid)
94
    {
95
        $this->load_comment($guid);
96
        if ($this->_comment->status !== net_nehmer_comments_comment::MODERATED) {
97
            if ($action == 'confirm_abuse') {
98
                $this->_comment->moderate(net_nehmer_comments_comment::ABUSE, 'confirmed_abuse');
99
            } else {
100
                $this->_comment->moderate(net_nehmer_comments_comment::JUNK, 'confirmed_junk');
101
            }
102
        }
103
        midcom::get()->indexer->delete($this->_comment->guid);
0 ignored issues
show
Bug introduced by
$this->_comment->guid of type string is incompatible with the type array expected by parameter $RIs of midcom_services_indexer::delete(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        midcom::get()->indexer->delete(/** @scrutinizer ignore-type */ $this->_comment->guid);
Loading history...
104
        return $this->reply($request);
105
    }
106
107 1
    private function load_comment(string $identifier, bool $require_moderation_privilege = true)
108
    {
109 1
        $this->_comment = new net_nehmer_comments_comment($identifier);
110
111 1
        if ($require_moderation_privilege) {
112
            $this->_comment->require_do('net.nehmer.comments:moderation');
113
        }
114 1
    }
115
116 1
    private function reply(Request $request) : midcom_response_relocate
117
    {
118 1
        return new midcom_response_relocate($request->request->get('return_url', ''));
119
    }
120
}
121