Issues (115)

src/Middleware/CheckRequestHeaders.php (4 issues)

1
<?php
2
3
namespace VGirol\JsonApi\Middleware;
4
5
use Closure;
6
use Exception;
7
use Illuminate\Http\Request;
8
use VGirol\JsonApi\Exceptions\JsonApi400Exception;
9
use VGirol\JsonApi\Exceptions\JsonApi406Exception;
10
use VGirol\JsonApi\Exceptions\JsonApi415Exception;
11
use VGirol\JsonApi\Exceptions\JsonApi500Exception;
12
use VGirol\JsonApi\Messages\Messages;
13
use VGirol\JsonApi\Services\ResponseService;
14
15
class CheckRequestHeaders
16
{
17
    /**
18
     * Undocumented variable
19
     *
20
     * @var ResponseService
21
     */
22
    protected $responseService;
23
24
    /**
25
     * Class constructor.
26
     *
27
     * @param ResponseService $responseService
28
     *
29
     * @return void
30
     */
31
    public function __construct(ResponseService $responseService)
32
    {
33
        $this->responseService = $responseService;
34
    }
35
36
    /**
37
     * Handle an incoming request.
38
     *
39
     * @param Request     $request
40
     * @param Closure     $next
41
     * @param string|null $guard
42
     *
43
     * @return mixed
44
     */
45
    public function handle($request, Closure $next, $guard = null)
0 ignored issues
show
The parameter $guard is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

45
    public function handle($request, Closure $next, /** @scrutinizer ignore-unused */ $guard = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47
        try {
48
            // Content-Type header
49
            $this->checkContentTypeHeader($request);
50
51
            // Accept header
52
            $this->checkAcceptHeader($request);
53
        } catch (Exception $e) {
54
            jsonapiError($e, false);
55
56
            return $this->responseService->createErrorResponse();
57
        }
58
59
        return $next($request);
60
    }
61
62
    /**
63
     * Undocumented function
64
     *
65
     * @param Request $request
66
     *
67
     * @return void
68
     */
69
    private function checkContentTypeHeader($request)
70
    {
71
        $mediaType = config('jsonapi.media-type');
72
73
        // Content-Type header
74
        if (!$request->hasHeader('Content-Type')) {
0 ignored issues
show
The method hasHeader() does not exist on Illuminate\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        if (!$request->/** @scrutinizer ignore-call */ hasHeader('Content-Type')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            throw new JsonApi400Exception(
76
                sprintf(Messages::ERROR_CONTENT_TYPE_HEADER_MISSING, $mediaType)
77
            );
78
        }
79
80
        $contentType = $request->header('Content-Type');
0 ignored issues
show
The method header() does not exist on Illuminate\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        /** @scrutinizer ignore-call */ 
81
        $contentType = $request->header('Content-Type');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
        $matches = [];
82
        $count = preg_match_all('/' . preg_quote($mediaType, '/') . '[;]?(.*)/', $contentType, $matches);
83
        if ($count === false) {
84
            throw new JsonApi500Exception(
85
                Messages::ERROR_CONTENT_TYPE_HEADER_PARSING
86
            );
87
        }
88
        if ($count == 0) {
89
            throw new JsonApi400Exception(
90
                sprintf(Messages::ERROR_CONTENT_TYPE_HEADER_BAD_MEDIA_TYPE, $mediaType)
91
            );
92
        } else {
93
            $param = $matches[1][0];
94
            if ($param != '') {
95
                throw new JsonApi415Exception(
96
                    sprintf(Messages::ERROR_CONTENT_TYPE_HEADER_WITHOUT_PARAMETERS, $mediaType)
97
                );
98
            }
99
        }
100
    }
101
102
    /**
103
     * Undocumented function
104
     *
105
     * @param Request $request
106
     *
107
     * @return void
108
     */
109
    private function checkAcceptHeader($request)
110
    {
111
        $mediaType = config('jsonapi.media-type');
112
113
        // Accept header
114
        if ($request->hasHeader('Accept')) {
115
            $accept = $request->header('Accept');
116
            $count = preg_match_all('/' . preg_quote($mediaType, '/') . '[;]?([^,]*)/', $accept, $matches);
117
            if ($count === false) {
118
                throw new JsonApi500Exception(
119
                    Messages::ERROR_ACCEPT_HEADER_PARSING
120
                );
121
            }
122
            if ($count != 0) {
123
                $check = false;
124
                for ($i = 0; $i < $count; $i++) {
125
                    $param = $matches[1][$i];
126
                    if ($param == '') {
127
                        $check = true;
128
                    }
129
                }
130
                if (!$check) {
0 ignored issues
show
The condition $check is always false.
Loading history...
131
                    throw new JsonApi406Exception(
132
                        sprintf(Messages::ERROR_ACCEPT_HEADER_WITHOUT_PARAMETERS, $mediaType)
133
                    );
134
                }
135
            }
136
        }
137
    }
138
}
139