1 | <?php |
||
2 | |||
3 | namespace VGirol\JsonApi\Middleware; |
||
4 | |||
5 | use Closure; |
||
6 | use Exception; |
||
7 | use VGirol\JsonApi\Exceptions\JsonApi400Exception; |
||
8 | use VGirol\JsonApi\Services\ResponseService; |
||
9 | |||
10 | class CheckQueryParameters |
||
11 | { |
||
12 | /** |
||
13 | * Undocumented variable |
||
14 | * |
||
15 | * @var ResponseService |
||
16 | */ |
||
17 | protected $responseService; |
||
18 | |||
19 | /** |
||
20 | * Class constructor. |
||
21 | * |
||
22 | * @param ResponseService $responseService |
||
23 | * |
||
24 | * @return void |
||
25 | */ |
||
26 | public function __construct(ResponseService $responseService) |
||
27 | { |
||
28 | $this->responseService = $responseService; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Handle an incoming request. |
||
33 | * |
||
34 | * @param \Illuminate\Http\Request $request |
||
35 | * @param \Closure $next |
||
36 | * @param string|null $guard |
||
37 | * |
||
38 | * @return mixed |
||
39 | */ |
||
40 | public function handle($request, Closure $next, $guard = null) |
||
0 ignored issues
–
show
|
|||
41 | { |
||
42 | try { |
||
43 | // Parse query parameters |
||
44 | $this->parseQueryParameters($request); |
||
45 | } catch (Exception $e) { |
||
46 | jsonapiError($e, false); |
||
47 | |||
48 | return $this->responseService->createErrorResponse(); |
||
49 | } |
||
50 | |||
51 | return $next($request); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Undocumented function |
||
56 | * |
||
57 | * @param \Illuminate\Http\Request $request |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | private function parseQueryParameters($request) |
||
62 | { |
||
63 | $services = [ |
||
64 | 'sort', 'filter', 'include', 'pagination', 'fields' |
||
65 | ]; |
||
66 | foreach ($services as $serviceName) { |
||
67 | $func = 'jsonapi' . ucfirst($serviceName); |
||
68 | $service = $func($request); |
||
69 | |||
70 | if (!$service->hasQuery($request)) { |
||
71 | continue; |
||
72 | } |
||
73 | |||
74 | if (!$service->allowedByServer()) { |
||
75 | throw new JsonApi400Exception( |
||
76 | constant( |
||
77 | 'VGirol\JsonApi\Messages\Messages::ERROR_QUERY_PARAMETER_' |
||
78 | . strtoupper($serviceName) |
||
79 | . '_NOT_ALLOWED_BY_SERVER' |
||
80 | ) |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | if (!$service->allowedForRoute($request)) { |
||
85 | throw new JsonApi400Exception( |
||
86 | constant( |
||
87 | 'VGirol\JsonApi\Messages\Messages::ERROR_QUERY_PARAMETER_' |
||
88 | . strtoupper($serviceName) |
||
89 | . '_NOT_ALLOWED_FOR_ROUTE' |
||
90 | ) |
||
91 | ); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.