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.

DefaultProfile   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 101
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequest() 0 4 1
A allowCredentials() 0 4 1
A allowOrigins() 0 4 1
A allowMethods() 0 4 1
A allowHeaders() 0 4 1
A exposeHeaders() 0 4 1
A maxAge() 0 4 1
A addCorsHeaders() 0 11 2
A addPreflightHeaders() 0 13 2
A isAllowed() 0 16 3
A toString() 0 4 1
A allowedOriginsToString() 0 12 4
1
<?php
2
3
namespace Spatie\Cors\CorsProfile;
4
5
class DefaultProfile implements CorsProfile
6
{
7
    /** @var \Illuminate\Http\Request */
8
    protected $request;
9
10
    public function setRequest($request)
11
    {
12
        $this->request = $request;
13
    }
14
15
    public function allowCredentials(): bool
16
    {
17
        return config('cors.default_profile.allow_credentials') ?? false;
18
    }
19
20
    public function allowOrigins(): array
21
    {
22
        return config('cors.default_profile.allow_origins');
23
    }
24
25
    public function allowMethods(): array
26
    {
27
        return config('cors.default_profile.allow_methods');
28
    }
29
30
    public function allowHeaders(): array
31
    {
32
        return config('cors.default_profile.allow_headers');
33
    }
34
35
    public function exposeHeaders(): array
36
    {
37
        return config('cors.default_profile.expose_headers') ?? [];
38
    }
39
40
    public function maxAge(): int
41
    {
42
        return config('cors.default_profile.max_age');
43
    }
44
45
    public function addCorsHeaders($response)
46
    {
47
        if ($this->allowCredentials()) {
48
            $response->headers->set('Access-Control-Allow-Credentials', 'true');
49
        }
50
51
        $response->headers->set('Access-Control-Allow-Origin', $this->allowedOriginsToString());
52
        $response->headers->set('Access-Control-Expose-Headers', $this->toString($this->exposeHeaders()));
53
54
        return $response;
55
    }
56
57
    public function addPreflightHeaders($response)
58
    {
59
        if ($this->allowCredentials()) {
60
            $response->headers->set('Access-Control-Allow-Credentials', 'true');
61
        }
62
63
        $response->headers->set('Access-Control-Allow-Methods', $this->toString($this->allowMethods()));
64
        $response->headers->set('Access-Control-Allow-Headers', $this->toString($this->allowHeaders()));
65
        $response->headers->set('Access-Control-Allow-Origin', $this->allowedOriginsToString());
66
        $response->headers->set('Access-Control-Max-Age', $this->maxAge());
67
68
        return $response;
69
    }
70
71
    public function isAllowed(): bool
72
    {
73
        if (! in_array($this->request->method(), $this->allowMethods())) {
74
            return false;
75
        }
76
77
        if (in_array('*', $this->allowOrigins())) {
78
            return true;
79
        }
80
81
        $matches = collect($this->allowOrigins())->filter(function ($allowedOrigin) {
82
            return fnmatch($allowedOrigin, $this->request->header('Origin'));
83
        });
84
85
        return $matches->count() > 0;
86
    }
87
88
    protected function toString(array $array): string
89
    {
90
        return implode(', ', $array);
91
    }
92
93
    protected function allowedOriginsToString(): string
94
    {
95
        if (! $this->isAllowed()) {
96
            return '';
97
        }
98
99
        if (in_array('*', $this->allowOrigins()) && ! $this->allowCredentials()) {
100
            return '*';
101
        }
102
103
        return $this->request->header('Origin');
104
    }
105
}
106