VerifyJsonRequest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 8 2
A isJsonRequestValid() 0 10 2
A getMethods() 0 10 3
A jsonErrorResponse() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Support\Middleware;
6
7
use Closure;
8
use Illuminate\Http\{JsonResponse, Request, Response};
9
10
/**
11
 * Class     VerifyJsonRequest
12
 *
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class VerifyJsonRequest
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Supported request method verbs.
24
     *
25
     * @var array
26
     */
27
    protected $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
28
29
    /* -----------------------------------------------------------------
30
     |  Main Methods
31
     | -----------------------------------------------------------------
32
     */
33
34
    /**
35
     * Handle an incoming request.
36
     *
37
     * @param  \Illuminate\Http\Request  $request
38
     * @param  \Closure                  $next
39
     * @param  string|array|null         $methods
40
     *
41
     * @return mixed
42
     */
43 18
    public function handle(Request $request, Closure $next, $methods = null)
44
    {
45 18
        if ($this->isJsonRequestValid($request, $methods)) {
46 12
            return $next($request);
47
        }
48
49 6
        return $this->jsonErrorResponse();
50
    }
51
52
    /* -----------------------------------------------------------------
53
     |  Check Methods
54
     | -----------------------------------------------------------------
55
     */
56
57
    /**
58
     * Validate json Request.
59
     *
60
     * @param  \Illuminate\Http\Request  $request
61
     * @param  string|array|null         $methods
62
     *
63
     * @return bool
64
     */
65 18
    protected function isJsonRequestValid(Request $request, $methods)
66
    {
67 18
        $methods = $this->getMethods($methods);
68
69 18
        if ( ! in_array($request->method(), $methods)) {
70
            return false;
71
        }
72
73 18
        return $request->isJson();
74
    }
75
76
    /* -----------------------------------------------------------------
77
     |  Other Methods
78
     | -----------------------------------------------------------------
79
     */
80
81
    /**
82
     * Get the error as json response.
83
     *
84
     * @return \Illuminate\Http\JsonResponse
85
     */
86 6
    protected function jsonErrorResponse()
87
    {
88 2
        $data = [
89 6
            'status'  => 'error',
90 6
            'code'    => $statusCode = Response::HTTP_BAD_REQUEST,
91 6
            'message' => 'Request must be JSON',
92
        ];
93
94 6
        return new JsonResponse($data, $statusCode);
95
    }
96
97
    /**
98
     * Get request methods.
99
     *
100
     * @param  string|array|null  $methods
101
     *
102
     * @return array
103
     */
104 18
    protected function getMethods($methods): array
105
    {
106 18
        $methods = $methods ?? $this->methods;
107
108 18
        if (is_string($methods)) {
109 12
            $methods = (array) $methods;
110
        }
111
112 18
        return is_array($methods) ? array_map('strtoupper', $methods) : [];
113
    }
114
}
115