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.
Completed
Push — master ( dc4a9b...7bca64 )
by Jason
12:04
created

Cors   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 4
dl 0
loc 100
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 4 1
B corsHeaders() 0 31 5
A isCorsRequest() 0 4 1
A isPreflightRequest() 0 4 2
A isMethodAllowed() 0 6 2
A areHeadersAllowed() 0 9 2
A allowOrigin() 0 16 4
A domainToRegex() 0 4 1
A allowCredentials() 0 4 2
1
<?php
2
3
namespace JDesrosiers\Silex\Provider;
4
5
use Pimple\Container;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
9
class Cors
10
{
11
    private $app;
12
13 29
    public function __construct(Container $app)
14
    {
15 29
        $this->app = $app;
16 29
    }
17
18 25
    public function __invoke(Request $request, Response $response)
19
    {
20 25
        $response->headers->add($this->corsHeaders($request, $response->headers->get("Allow")));
21 25
    }
22
23 25
    private function corsHeaders(Request $request, $allow)
24
    {
25 25
        $headers = [];
26
27 25
        if (!$this->isCorsRequest($request)) {
28 2
            return [];
29
        }
30
31 23
        if ($this->isPreflightRequest($request)) {
32 8
            $requestMethod = $request->headers->get("Access-Control-Request-Method");
33 8
            if (!$this->isMethodAllowed($requestMethod, $allow)) {
34 2
                return [];
35
            }
36
37 6
            $requestHeaders = $request->headers->get("Access-Control-Request-Headers");
38 6
            if (!$this->areHeadersAllowed($requestHeaders)) {
39 1
                return [];
40
            }
41
42 5
            $headers["Access-Control-Allow-Headers"] = $requestHeaders;
43 5
            $headers["Access-Control-Allow-Methods"] = $requestMethod;
44 5
            $headers["Access-Control-Max-Age"] = $this->app["cors.maxAge"];
45 5
        } else {
46 15
            $headers["Access-Control-Expose-Headers"] = $this->app["cors.exposeHeaders"];
47
        }
48
49 20
        $headers["Access-Control-Allow-Origin"] = $this->allowOrigin($request);
50 20
        $headers["Access-Control-Allow-Credentials"] = $this->allowCredentials();
51
52 20
        return array_filter($headers);
53
    }
54
55 25
    private function isCorsRequest(Request $request)
56
    {
57 25
        return $request->headers->has("Origin");
58
    }
59
60 23
    private function isPreflightRequest(Request $request)
61
    {
62 23
        return $request->getMethod() === "OPTIONS" && $request->headers->has("Access-Control-Request-Method");
63
    }
64
65 8
    private function isMethodAllowed($requestMethod, $allow)
66
    {
67 8
        $commaSeparatedMethods = !is_null($this->app["cors.allowMethods"]) ? $this->app["cors.allowMethods"] : $allow;
68 8
        $allowedMethods = array_filter(preg_split("/\s*,\s*/", $commaSeparatedMethods));
69 8
        return in_array($requestMethod, $allowedMethods);
70
    }
71
72 6
    private function areHeadersAllowed($commaSeparatedRequestHeaders)
73
    {
74 6
        if ($this->app["cors.allowHeaders"] === null) {
75 5
            return true;
76
        }
77 1
        $requestHeaders = array_filter(preg_split("/\s*,\s*/", $commaSeparatedRequestHeaders));
78 1
        $allowedHeaders = array_filter(preg_split("/\s*,\s*/", $this->app["cors.allowHeaders"]));
79 1
        return array_diff($requestHeaders, $allowedHeaders) === [];
80
    }
81
82 20
    private function allowOrigin(Request $request)
83
    {
84 20
        $origin = $request->headers->get("Origin");
85 20
        if ($this->app["cors.allowOrigin"] === "*") {
86 14
            $this->app["cors.allowOrigin"] = $origin;
87 14
        }
88
89 20
        $origins = array_filter(preg_split('/\s+/', $this->app["cors.allowOrigin"]));
90 20
        foreach ($origins as $domain) {
91 20
            if (preg_match($this->domainToRegex($domain), $origin)) {
92 19
                return $origin;
93
            }
94 2
        }
95
96 1
        return "null";
97
    }
98
99 20
    private function domainToRegex($domain)
100
    {
101 20
        return "/^" . preg_replace("/^\\\\\*/", "[^.]+", preg_quote($domain, "/")) . "$/";
102
    }
103
104 20
    private function allowCredentials()
105
    {
106 20
        return $this->app["cors.allowCredentials"] === true ? "true" : null;
107
    }
108
}
109