SecureArticleInputMiddleware::handle()   B
last analyzed

Complexity

Conditions 11
Paths 10

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 25
nc 10
nop 2
dl 0
loc 35
ccs 0
cts 25
cp 0
crap 132
rs 7.3166
c 0
b 0
f 0

How to fix   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\BlogArticle;
6
use App\Helper\FormatHelper;
7
use Closure;
8
use Illuminate\Http\Request;
9
10
/**
11
 * Class SecureArticleInputMiddleware
12
 * @package App\Http\Middleware
13
 */
14
class SecureArticleInputMiddleware
15
{
16
    /**
17
     * Handle an incoming request.
18
     *
19
     * @param  Request $request
20
     * @param  Closure $next
21
     * @return mixed
22
     */
23
    public function handle(Request $request, Closure $next)
24
    {
25
        $article = new BlogArticle();
26
        $method = $request->getMethod();
27
        $requestPath = $request->getRequestUri();
28
        $returnArray = array();
29
        $returnStatus = 0;
30
31
        if ($method == "POST" && $requestPath == "/api/article") {
32
            $blogHash = $request->input("blogHash");
33
            $title = $request->input("title");
34
            $author = $request->input("author");
35
            $url = $request->input("url");
36
37
            if ($blogHash == null || $title == null || $author == null || $url == null) {
38
                $returnArray["error-code"] = "invalid-request";
39
                $returnStatus = 400;
40
            }
41
        } else if ($method == "PUT" && $requestPath == "/api/article") {
42
            $hash = $request->input("hash");
43
            $articleResult = $article->where("hash", $hash)->first();
44
            if ($articleResult == null) {
45
                $returnArray["error-code"] = "article-not-found";
46
                $returnStatus = 404;
47
            }
48
        } else {
49
            $returnArray["error-code"] = "request-not-found";
50
            $returnStatus = 400;
51
        }
52
53
        if (!empty($returnArray)) {
54
            return FormatHelper::formatData($returnArray, false, $returnStatus);
55
        }
56
57
        return $next($request);
58
    }
59
}