Passed
Push — master ( fd34e8...32315f )
by Andreas
11:37
created

net_nehmer_comments_handler_admin   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 154
ccs 60
cts 88
cp 0.6818
rs 10
c 0
b 0
f 0
wmc 26

9 Methods

Rating   Name   Duplication   Size   Complexity  
B _load_comments() 0 46 9
A _show_welcome() 0 5 1
A _init_display_datamanager() 0 4 1
A _handler_welcome() 0 3 1
A _on_initialize() 0 4 1
A _handler_moderate_ajax() 0 21 6
A _show_moderate_ajax() 0 6 2
A _show_moderate() 0 12 3
A _handler_moderate() 0 13 2
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 midcom\datamanager\datamanager;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * Comments welcome page handler
14
 *
15
 * @package net.nehmer.comments
16
 */
17
class net_nehmer_comments_handler_admin extends midcom_baseclasses_components_handler
18
{
19
    use net_nehmer_comments_handler;
0 ignored issues
show
introduced by
The trait net_nehmer_comments_handler requires some properties which are not provided by net_nehmer_comments_handler_admin: $auth, $guid
Loading history...
20
21
    /**
22
     * This datamanager instance is used to display an existing comment. only set
23
     * if there are actually comments to display.
24
     */
25
    private datamanager $_display_datamanager;
26
27
    /**
28
     * @var net_nehmer_comments_comment[]
29
     */
30
    private array $comments;
31
32
    private string $status;
33
34 7
    public function _on_initialize()
35
    {
36 7
        midcom::get()->auth->require_valid_user();
37 7
        $this->_topic->require_do('net.nehmer.comments:moderation');
38
    }
39
40
    /**
41
     * Prepares the _display_datamanager member.
42
     */
43
    private function _init_display_datamanager()
44
    {
45
        $this->_display_datamanager = datamanager::from_schemadb($this->_config->get('schemadb'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('schemadb') 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

45
        $this->_display_datamanager = datamanager::from_schemadb(/** @scrutinizer ignore-type */ $this->_config->get('schemadb'));
Loading history...
46
        $this->_request_data['display_datamanager'] = $this->_display_datamanager;
47
    }
48
49 1
    public function _handler_welcome(array &$data)
50
    {
51 1
        $data['topic'] = $this->_topic;
52
    }
53
54 1
    public function _show_welcome(string $handler_id, array &$data)
55
    {
56 1
        midcom_show_style('admin-start');
57 1
        midcom_show_style('admin-welcome');
58 1
        midcom_show_style('admin-end');
59
    }
60
61 6
    private function _load_comments() : array
62
    {
63 6
        $view_status = [];
64 6
        switch ($this->status) {
65 6
            case 'reported_abuse':
66 1
                $this->_request_data['status_to_show'] = 'reported abuse';
67 1
                $view_status[] = net_nehmer_comments_comment::REPORTED_ABUSE;
68 1
                break;
69 5
            case 'abuse':
70 1
                $this->_request_data['status_to_show'] = 'abuse';
71 1
                $view_status[] = net_nehmer_comments_comment::ABUSE;
72 1
                break;
73 4
            case 'junk':
74 1
                $this->_request_data['status_to_show'] = 'junk';
75 1
                $view_status[] = net_nehmer_comments_comment::JUNK;
76 1
                break;
77 3
            case 'latest':
78 1
                $this->_request_data['status_to_show'] = 'latest comments';
79 1
                $view_status[] = net_nehmer_comments_comment::NEW_ANONYMOUS;
80 1
                $view_status[] = net_nehmer_comments_comment::NEW_USER;
81 1
                $view_status[] = net_nehmer_comments_comment::MODERATED;
82 1
                if ($this->_config->get('show_reported_abuse_as_normal')) {
83 1
                    $view_status[] = net_nehmer_comments_comment::REPORTED_ABUSE;
84
                }
85 1
                break;
86 2
            case 'latest_new':
87 1
                $this->_request_data['status_to_show'] = 'latest comments, only new';
88 1
                $view_status[] = net_nehmer_comments_comment::NEW_ANONYMOUS;
89 1
                $view_status[] = net_nehmer_comments_comment::NEW_USER;
90 1
                if ($this->_config->get('show_reported_abuse_as_normal')) {
91 1
                    $view_status[] = net_nehmer_comments_comment::REPORTED_ABUSE;
92
                }
93 1
                break;
94 1
            case 'latest_approved':
95 1
                $this->_request_data['status_to_show'] = 'latest comments, only approved';
96 1
                $view_status[] = net_nehmer_comments_comment::MODERATED;
97 1
                break;
98
        }
99
100 6
        $qb = new org_openpsa_qbpager(net_nehmer_comments_comment::class, 'net_nehmer_comments_comments');
101 6
        $qb->results_per_page = $this->_config->get('items_to_show');
102 6
        $qb->display_pages = $this->_config->get('paging');
103 6
        $qb->add_constraint('status', 'IN', $view_status);
104 6
        $qb->add_order('metadata.revised', 'DESC');
105
106 6
        return $qb->execute();
107
    }
108
109
    /**
110
     * Checks if a button of the admin toolbar was pressed.
111
     */
112
    public function _handler_moderate_ajax(Request $request, string $status, array &$data)
113
    {
114
        if (   !$request->request->has('action')
115
            || !$request->request->has('guid')) {
116
            throw new midcom_error_notfound('Incomplete POST data');
117
        }
118
        if ($request->request->get('action') !== 'action_delete') {
119
            throw new midcom_error_notfound('Unsupported action');
120
        }
121
122
        $comment = new net_nehmer_comments_comment($request->request->get('guid'));
123
        if (!$comment->delete()) {
124
            throw new midcom_error("Failed to delete comment GUID '{$comment->guid}': " . midcom_connection::get_error_string());
125
        }
126
127
        $this->status = $status;
128
        if ($comments = $this->_load_comments()) {
129
            $data['comment'] = end($comments);
130
            $this->_init_display_datamanager();
131
        }
132
        midcom::get()->skip_page_style = true;
133
    }
134
135
    public function _show_moderate_ajax(string $handler_id, array &$data)
136
    {
137
        if (!empty($data['comment'])) {
138
            $this->_display_datamanager->set_storage($data['comment']);
139
            $data['comment_toolbar'] = $this->populate_post_toolbar($data['comment'], $this->status);
140
            midcom_show_style('admin-comments-item');
141
        }
142
    }
143
144 6
    public function _handler_moderate(string $status, array &$data)
145
    {
146 6
        $this->status = $status;
147
148 6
        if ($this->comments = $this->_load_comments()) {
149
            $this->_init_display_datamanager();
150
        }
151
152 6
        midcom::get()->head->enable_jquery();
153 6
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/net.nehmer.comments/moderate.js');
154
155 6
        net_nehmer_comments_viewer::add_head_elements();
156 6
        $this->add_breadcrumb('', $this->_l10n->get($data['status_to_show']));
157
    }
158
159 6
    public function _show_moderate(string $handler_id, array &$data)
160
    {
161 6
        midcom_show_style('admin-start');
162 6
        if ($this->comments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->comments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
163
            midcom_show_style('admin-comments-start');
164
            foreach ($this->comments as $comment) {
165
                $data['comment'] = $comment;
166
                $this->_show_moderate_ajax($handler_id, $data);
167
            }
168
            midcom_show_style('admin-comments-end');
169
        }
170 6
        midcom_show_style('admin-end');
171
    }
172
}
173