Completed
Pull Request — master (#7)
by Lars
03:19
created

SecureCommentInputMiddleware::handle()   C

Complexity

Conditions 18
Paths 18

Size

Total Lines 56
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
cc 18
eloc 44
nc 18
nop 2
dl 0
loc 56
ccs 0
cts 44
cp 0
crap 342
rs 6.5661
c 0
b 0
f 0

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
namespace App\Http\Middleware;
4
5
use App\Blog;
6
use App\Comments;
7
use App\Helper\FormatHelper;
8
use Closure;
9
use Illuminate\Http\Request;
10
11
class SecureCommentInputMiddleware
12
{
13
    /**
14
     * Handle an incoming request.
15
     *
16
     * @param  Request $request
17
     * @param  Closure $next
18
     * @return mixed
19
     */
20
    public function handle(Request $request, Closure $next)
21
    {
22
        $blog = new Blog();
23
        $comment = new Comments();
24
        $method = $request->getMethod();
25
        $requestPath = $request->getRequestUri();
26
        $returnArray = array();
27
        $returnStatus = 0;
28
29
        $authorName = $request->input("authorName");
30
        $content = $request->input("content");
31
        $captcha = $request->input("captcha");
32
        $spam = $request->input("computer");
33
34
        if ($spam != null) {
35
            $returnArray["error-code"] = "bot-detected";
36
            $returnStatus = 403;
37
        } else if ($captcha == null) {
38
            $returnArray["error-code"] = "captcha-missing";
39
            $returnStatus = 400;
40
        } else if ($captcha != getenv("CAPTCHA_SECRET")) {
41
            $returnArray["error-code"] = "captcha-wrong";
42
            $returnStatus = 400;
43
        } else if ($method == "POST" && $requestPath == "/api/comment/add") {
44
            $blogHash = $request->input("blogHash");
45
            $articleHash = $request->input("articleHash");
46
            $blogResult = $blog->where("hash", $blogHash)->first();
47
48
            $articleTitle = $request->input("articleTitle");
49
            $articleAuthor = $request->input("articleAuthor");
50
            $articleUrl = $request->input("articleUrl");
51
52
            if ($blogHash == null && $articleHash == null && $articleTitle == null && $articleAuthor == null && $articleUrl == null && $authorName == null && $content == null) {
53
                $returnArray["error-code"] = "invalid-request";
54
                $returnStatus = 400;
55
            } else if ($blogResult == null) {
56
                $returnArray["error-code"] = "blog-not-found";
57
                $returnStatus = 404;
58
            }
59
        } else if ($method == "PUT" && strpos($requestPath, "/api/comment/edit/") !== false) {
60
            $hash = $request->route()[2]["hash"];
61
            $commentResult = $comment->where("hash", $hash)->first();
62
            if ($commentResult == null) {
63
                $returnArray["error-code"] = "comment-not-found";
64
                $returnStatus = 404;
65
            }
66
        } else {
67
            $returnArray["error-code"] = "request-not-found";
68
            $returnStatus = 400;
69
        }
70
71
        if (!empty($returnArray)) {
72
            return FormatHelper::formatData($returnArray, false, $returnStatus);
73
        }
74
75
        return $next($request);
76
    }
77
}
78