GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Cors   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 72
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 20 4
A isCorsRequest() 0 8 2
A isPreflightRequest() 0 4 1
A handlePreflightRequest() 0 8 2
A forbiddenResponse() 0 10 1
1
<?php
2
3
namespace Spatie\Cors;
4
5
use Closure;
6
use Spatie\Cors\CorsProfile\CorsProfile;
7
8
class Cors
9
{
10
    /** @var \Spatie\Cors\CorsProfile\CorsProfile */
11
    protected $corsProfile;
12
13
    public function __construct(CorsProfile $corsProfile)
14
    {
15
        $this->corsProfile = $corsProfile;
16
    }
17
18
    /**
19
     * Handle an incoming request.
20
     *
21
     * @param  \Illuminate\Http\Request  $request
22
     * @param  \Closure  $next
23
     * @return mixed
24
     */
25
    public function handle($request, Closure $next)
26
    {
27
        if (! $this->isCorsRequest($request)) {
28
            return $next($request);
29
        }
30
31
        $this->corsProfile->setRequest($request);
32
33
        if (! $this->corsProfile->isAllowed()) {
34
            return $this->forbiddenResponse();
35
        }
36
37
        if ($this->isPreflightRequest($request)) {
38
            return $this->handlePreflightRequest();
39
        }
40
41
        $response = $next($request);
42
43
        return $this->corsProfile->addCorsHeaders($response);
44
    }
45
46
    protected function isCorsRequest($request): bool
47
    {
48
        if (! $request->headers->has('Origin')) {
49
            return false;
50
        }
51
52
        return $request->headers->get('Origin') !== $request->getSchemeAndHttpHost();
53
    }
54
55
    protected function isPreflightRequest($request): bool
56
    {
57
        return $request->getMethod() === 'OPTIONS';
58
    }
59
60
    protected function handlePreflightRequest()
61
    {
62
        if (! $this->corsProfile->isAllowed()) {
63
            return $this->forbiddenResponse();
64
        }
65
66
        return $this->corsProfile->addPreflightHeaders(response(null, 204));
67
    }
68
69
    protected function forbiddenResponse()
70
    {
71
        $message = config('cors.default_profile.forbidden_response.message');
72
        $status = config('cors.default_profile.forbidden_response.status');
73
74
        return response(
75
            $message ?? 'Forbidden (cors).',
76
            $status ?? 403
77
        );
78
    }
79
}
80