Test Setup Failed
Push — master ( 5f75b5...a576a1 )
by Simon
03:23
created

PageFlagComment::main()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 58
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 58
ccs 0
cts 25
cp 0
rs 6.9666
c 1
b 0
f 0
cc 12
nc 9
nop 0
crap 156

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 Waca\DataObjects\Comment;
12
use Waca\DataObjects\Request;
13
use Waca\DataObjects\User;
14
use Waca\Exceptions\AccessDeniedException;
15
use Waca\Exceptions\ApplicationLogicException;
16
use Waca\Helpers\Logger;
17
use Waca\Tasks\InternalPageBase;
18
use Waca\WebRequest;
19
20
class PageFlagComment extends InternalPageBase
21
{
22
    /**
23
     * @inheritDoc
24
     */
25
    protected function main()
26
    {
27
        if (!WebRequest::wasPosted()) {
28
            throw new ApplicationLogicException('This page does not support GET methods.');
29
        }
30
31
        $this->validateCSRFToken();
32
33
        $flagState = WebRequest::postInt('flag');
34
        $commentId = WebRequest::postInt('comment');
35
        $updateVersion = WebRequest::postInt('updateversion');
36
37
        if ($flagState !== 0 && $flagState !== 1) {
38
            throw new ApplicationLogicException('Flag status not valid');
39
        }
40
41
        $database = $this->getDatabase();
42
43
        /** @var Comment|false $comment */
44
        $comment = Comment::getById($commentId, $database);
45
        if ($comment === false) {
0 ignored issues
show
introduced by
The condition $comment === false is always false.
Loading history...
46
            throw new ApplicationLogicException('Unknown comment');
47
        }
48
49
        $currentUser = User::getCurrent($database);
50
51
        if ($comment->getFlagged() && !$this->barrierTest('unflag', $currentUser)) {
52
            // user isn't allowed to unflag comments
53
            throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
54
        }
55
56
        /** @var Request $request */
57
        $request = Request::getById($comment->getRequest(), $database);
58
59
        if ($comment->getFlagged()
60
            && !$this->barrierTest('alwaysSeePrivateData', $currentUser, 'RequestData')
61
            && $request->getReserved() !== $currentUser->getId()
62
        ) {
63
            // can't unflag if you can't see it.
64
            throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
65
        }
66
67
        $comment->setFlagged($flagState == 1);
68
        $comment->setUpdateVersion($updateVersion);
69
        $comment->save();
70
71
        if ($flagState === 1) {
72
            Logger::flaggedComment($database, $comment, $request->getDomain());
73
        }
74
        else {
75
            Logger::unflaggedComment($database, $comment, $request->getDomain());
76
        }
77
78
        if (WebRequest::postString('return') == 'list') {
79
            $this->redirect('flaggedComments');
80
        }
81
        else {
82
            $this->redirect('viewRequest', null, ['id' => $comment->getRequest()]);
83
        }
84
    }
85
}