Completed
Push — master ( b3d024...7ceb75 )
by ARCANEDEV
04:24
created

VerifyJsonRequest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 76
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 8 2
A isJsonRequestValid() 0 9 2
A getMethods() 0 12 4
1
<?php namespace Arcanedev\Support\Middleware;
2
3
use Arcanedev\Support\Bases\Middleware;
4
use Closure;
5
use Illuminate\Http\Request;
6
7
/**
8
 * Class     VerifyJsonRequest
9
 *
10
 * @package  Arcanedev\Support\Middleware
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class VerifyJsonRequest extends Middleware
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     *
21
     * @var array
22
     */
23
    protected $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Main Functions
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * Handle an incoming request.
31
     *
32
     * @param  \Illuminate\Http\Request  $request
33
     * @param  \Closure                  $next
34
     * @param  string|array|null         $methods
35
     *
36
     * @return mixed
37
     */
38 24
    public function handle(Request $request, Closure $next, $methods = null)
39
    {
40 24
        if ($this->isJsonRequestValid($request, $methods)) {
41 16
            return $next($request);
42
        }
43
44 8
        return response()->json('Request must be json', 400);
45
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Check Functions
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * Validate json Request.
53
     *
54
     * @param  Request            $request
55
     * @param  string|array|null  $methods
56
     *
57
     * @return bool
58
     */
59 24
    private function isJsonRequestValid(Request $request, $methods)
60
    {
61 24
        $methods = $this->getMethods($methods);
62
63
        return ! (
64 24
            in_array($request->method(), $methods) &&
65 24
            ! $request->isJson()
66 18
        );
67
    }
68
69
    /**
70
     * Get request methods.
71
     *
72
     * @param  string|array|null  $methods
73
     *
74
     * @return array
75
     */
76 24
    private function getMethods($methods)
77
    {
78 24
        if (is_null($methods)) {
79 8
            $methods = $this->methods;
80 6
        }
81
82 24
        if (is_string($methods)) {
83 16
            $methods = (array) $methods;
84 12
        }
85
86 24
        return is_array($methods) ? array_map('strtoupper', $methods) : [];
87
    }
88
}
89