Passed
Push — bantools ( 62b2c6...edcab0 )
by Simon
10:09
created

PageEditComment::main()   D

Complexity

Conditions 20
Paths 11

Size

Total Lines 78
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 46
c 5
b 0
f 0
dl 0
loc 78
ccs 0
cts 50
cp 0
rs 4.1666
cc 20
nc 11
nop 0
crap 420

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
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages;
10
11
use Exception;
12
use Waca\DataObjects\Comment;
13
use Waca\DataObjects\Request;
14
use Waca\DataObjects\User;
15
use Waca\Exceptions\AccessDeniedException;
16
use Waca\Exceptions\ApplicationLogicException;
17
use Waca\Helpers\Logger;
18
use Waca\SessionAlert;
19
use Waca\Tasks\InternalPageBase;
20
use Waca\WebRequest;
21
22
class PageEditComment extends InternalPageBase
23
{
24
    /**
25
     * Main function for this page, when no specific actions are called.
26
     * @throws ApplicationLogicException
27
     * @throws Exception
28
     */
29
    protected function main()
30
    {
31
        $commentId = WebRequest::getInt('id');
32
        if ($commentId === null) {
33
            throw new ApplicationLogicException('Comment ID not specified');
34
        }
35
36
        $database = $this->getDatabase();
37
38
        /** @var Comment|false $comment */
39
        $comment = Comment::getById($commentId, $database);
40
        if ($comment === false) {
0 ignored issues
show
introduced by
The condition $comment === false is always false.
Loading history...
41
            throw new ApplicationLogicException('Comment not found');
42
        }
43
44
        $currentUser = User::getCurrent($database);
45
        if ($comment->getUser() !== $currentUser->getId() && !$this->barrierTest('editOthers', $currentUser)) {
46
            throw new AccessDeniedException($this->getSecurityManager());
47
        }
48
49
        if ($comment->getVisibility() === 'admin'
50
            && !$this->barrierTest('seeRestrictedComments', $currentUser, 'RequestData')
51
            && $comment->getUser() !== $currentUser->getId()) {
52
            throw new AccessDeniedException($this->getSecurityManager());
53
        }
54
55
        if ($comment->getVisibility() === 'checkuser'
56
            && !$this->barrierTest('seeCheckuserComments', $currentUser, 'RequestData')
57
            && $comment->getUser() !== $currentUser->getId()) {
58
            throw new AccessDeniedException($this->getSecurityManager());
59
        }
60
61
        /** @var Request|false $request */
62
        $request = Request::getById($comment->getRequest(), $database);
63
64
        if ($request === false) {
0 ignored issues
show
introduced by
The condition $request === false is always false.
Loading history...
65
            throw new ApplicationLogicException('Request was not found.');
66
        }
67
68
        if (WebRequest::wasPosted()) {
69
            $this->validateCSRFToken();
70
            $newComment = WebRequest::postString('newcomment');
71
            $visibility = WebRequest::postString('visibility');
72
73
            if ($newComment === $comment->getComment() && ($comment->getVisibility() === 'requester' || $comment->getVisibility() === $visibility)) {
74
                // Only save and log if the comment changed
75
                $this->redirect('viewRequest', null, array('id' => $comment->getRequest()));
76
                return;
77
            }
78
79
            if ($comment->getVisibility() !== 'requester') {
80
                if ($visibility !== 'user' && $visibility !== 'admin' && $visibility !== 'checkuser') {
81
                    throw new ApplicationLogicException('Comment visibility is not valid');
82
                }
83
84
                $comment->setVisibility($visibility);
85
            }
86
87
            // optimistically lock from the load of the edit comment form
88
            $updateVersion = WebRequest::postInt('updateversion');
89
            $comment->setUpdateVersion($updateVersion);
90
91
            $comment->setComment($newComment);
92
93
            $comment->save();
94
95
            Logger::editComment($database, $comment, $request);
96
            $this->getNotificationHelper()->commentEdited($comment, $request);
97
            SessionAlert::success("Comment has been saved successfully");
98
99
            $this->redirect('viewRequest', null, array('id' => $comment->getRequest()));
100
        }
101
        else {
102
            $this->assignCSRFToken();
103
            $this->assign('comment', $comment);
104
            $this->assign('request', $request);
105
            $this->assign('user', User::getById($comment->getUser(), $database));
106
            $this->setTemplate('edit-comment.tpl');
107
        }
108
    }
109
}
110