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 23

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 4
dl 0
loc 126
ccs 70
cts 70
cp 1
rs 10
c 0
b 0
f 0

11 Methods

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