Passed
Push — development ( 924c99...c29c1d )
by Spuds
01:04 queued 20s
created

ModerateAttachments::action_attachapprove()   C

Complexity

Conditions 12
Paths 95

Size

Total Lines 80
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 33
c 0
b 0
f 0
nc 95
nop 0
dl 0
loc 80
ccs 0
cts 28
cp 0
crap 156
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * All the moderation actions for attachments, basically approve them
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * This file contains code covered by:
11
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
12
 *
13
 * @version 2.0 Beta 1
14
 *
15
 */
16
17
namespace ElkArte\Controller;
18
19
use ElkArte\AbstractController;
20
use ElkArte\Cache\Cache;
21
use ElkArte\Exceptions\Exception;
22
23
/**
24
 * Class ModerateAttachments
25
 *
26
 * This class is responsible for moderating attachments.
27
 */
28
class ModerateAttachments extends AbstractController
29
{
30
	/**
31
	 * Forward to attachments approval method is the only responsibility
32
	 * of this controller.
33
	 *
34
	 * @see AbstractController::action_index
35
	 */
36
	public function action_index()
37
	{
38
		// Forward to our method(s) to do the job
39
		$this->action_attachapprove();
40
	}
41
42
	/**
43
	 * Approve an attachment
44
	 *
45
	 * - Called from a mouse click,
46
	 * - works out what we want to do with attachments and actions it.
47
	 * - Accessed by ?action=attachapprove
48
	 */
49
	public function action_attachapprove(): void
50
	{
51
		// Security is our primary concern...
52
		checkSession('get');
53
54
		// Is it approve or delete?
55
		$sa = $this->_req->getQuery('sa', 'trim|strval', '');
56
		$is_approve = $sa !== 'reject';
57
58
		$attachments = [];
59
		require_once(SUBSDIR . '/ManageAttachments.subs.php');
60
61
		// If we are approving all ID's in a message, get the ID's.
62
		if ($sa === 'all' && $this->_req->hasQuery('mid'))
63
		{
64
			$id_msg = $this->_req->getQuery('mid', 'intval', 0);
65
			if (!empty($id_msg))
66
			{
67
				$attachments = attachmentsOfMessage($id_msg);
68
			}
69
		}
70
		elseif ($this->_req->hasQuery('aid'))
71
		{
72
			$aid = $this->_req->getQuery('aid', 'intval', 0);
73
			if (!empty($aid))
74
			{
75
				$attachments[] = $aid;
76
			}
77
		}
78
79
		if (empty($attachments))
80
		{
81
			throw new Exception('no_access', false);
82
		}
83
84
		// @todo nb: this requires permission to approve posts, not manage attachments
85
		// Now we have some ID's cleaned and ready to approve, but first - let's check we have permission!
86
		$allowed_boards = empty($this->user->mod_cache['ap']) ? boardsAllowedTo('approve_posts') : $this->user->mod_cache['ap'];
87
88
		if ($allowed_boards == [0])
89
		{
90
			$approve_query = '';
91
		}
92
		elseif (!empty($allowed_boards))
93
		{
94
			$approve_query = ' AND m.id_board IN (' . implode(',', $allowed_boards) . ')';
95
		}
96
		else
97
		{
98
			$approve_query = ' AND 0';
99
		}
100
101
		// Validate the attachments exist and have the right approval state.
102
		$attachments = validateAttachments($attachments, $approve_query);
103
104
		// Set up a return link based off one of the attachments for this message
105
		$attach_home = attachmentBelongsTo($attachments[0]);
106
		$redirect = 'topic=' . $attach_home['id_topic'] . '.msg' . $attach_home['id_msg'] . '#msg' . $attach_home['id_msg'];
107
108
		if (empty($attachments))
109
		{
110
			throw new Exception('no_access', false);
111
		}
112
113
		// Finally, we are there. Follow through!
114
		if ($is_approve)
115
		{
116
			// Checked and deemed worthy.
117
			approveAttachments($attachments);
118
		}
119
		else
120
		{
121
			removeAttachments(['id_attach' => $attachments, 'do_logging' => true]);
122
		}
123
124
		// We approved or removed, either way we reset those numbers
125
		Cache::instance()->remove('num_menu_errors');
126
127
		// Return to the topic....
128
		redirectexit($redirect);
129
	}
130
}
131