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 22

Size/Duplication

Total Lines 105
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 20
loc 105
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrigin() 10 10 2
A setExposeHeaders() 0 10 3
A setMaxAge() 0 5 2
A setAllowCredentials() 0 5 3
A setAllowMethods() 10 10 3
A setAllowHeaders() 0 14 4
A setCorsHeaders() 0 16 2
A handle() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PhpBoot\Controller\Hooks;
4
use PhpBoot\Controller\ExceptionRenderer;
5
use PhpBoot\DI\Traits\EnableDIAnnotations;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
9
/**
10
 * Class Cors
11
 * copy from https://github.com/palanik/lumen-cors/blob/master/LumenCors.php
12
 */
13
class Cors
14
{
15
    use EnableDIAnnotations;
16
17
    protected $settings = array(
18
        'origin' => '*',    // Wide Open!
19
        'allowMethods' => 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
20
    );
21 View Code Duplication
    protected function setOrigin(Request $req, Response $rsp) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
        $origin = $this->settings['origin'];
23
        if (is_callable($origin)) {
24
            // Call origin callback with request origin
25
            $origin = call_user_func($origin,
26
                $req->headers->get("Origin")
27
            );
28
        }
29
        $rsp->headers->set('Access-Control-Allow-Origin', $origin);
30
    }
31
    protected function setExposeHeaders(Request $req, Response $rsp) {
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed.

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

Loading history...
32
        if (isset($this->settings->exposeHeaders)) {
33
            $exposeHeaders = $this->settings->exposeHeaders;
34
            if (is_array($exposeHeaders)) {
35
                $exposeHeaders = implode(", ", $exposeHeaders);
36
            }
37
38
            $rsp->headers->set('Access-Control-Expose-Headers', $exposeHeaders);
39
        }
40
    }
41
    protected function setMaxAge(Request $req, Response $rsp) {
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed.

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

Loading history...
42
        if (isset($this->settings['maxAge'])) {
43
            $rsp->headers->set('Access-Control-Max-Age', $this->settings['maxAge']);
44
        }
45
    }
46
    protected function setAllowCredentials(Request $req, Response $rsp) {
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed.

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

Loading history...
47
        if (isset($this->settings['allowCredentials']) && $this->settings['allowCredentials'] === True) {
48
            $rsp->headers->set('Access-Control-Allow-Credentials', 'true');
49
        }
50
    }
51 View Code Duplication
    protected function setAllowMethods(Request $req, Response $rsp) {
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed.

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

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
        if (isset($this->settings['allowMethods'])) {
53
            $allowMethods = $this->settings['allowMethods'];
54
            if (is_array($allowMethods)) {
55
                $allowMethods = implode(", ", $allowMethods);
56
            }
57
58
            $rsp->headers->set('Access-Control-Allow-Methods', $allowMethods);
59
        }
60
    }
61
    protected function setAllowHeaders(Request $req, Response $rsp) {
62
        if (isset($this->settings['allowHeaders'])) {
63
            $allowHeaders = $this->settings['allowHeaders'];
64
            if (is_array($allowHeaders)) {
65
                $allowHeaders = implode(", ", $allowHeaders);
66
            }
67
        }
68
        else {  // Otherwise, use request headers
69
            $allowHeaders = $req->headers->get("Access-Control-Request-Headers", null ,false);
70
        }
71
        if (isset($allowHeaders)) {
72
            $rsp->headers->set('Access-Control-Allow-Headers', $allowHeaders);
73
        }
74
    }
75
    protected function setCorsHeaders(Request $req, Response $rsp) {
76
        // http://www.html5rocks.com/static/images/cors_server_flowchart.png
77
        // Pre-flight
78
        if ($req->isMethod('OPTIONS')) {
79
            $this->setOrigin($req, $rsp);
80
            $this->setMaxAge($req, $rsp);
81
            $this->setAllowCredentials($req, $rsp);
82
            $this->setAllowMethods($req, $rsp);
83
            $this->setAllowHeaders($req, $rsp);
84
        }
85
        else {
86
            $this->setOrigin($req, $rsp);
87
            $this->setExposeHeaders($req, $rsp);
88
            $this->setAllowCredentials($req, $rsp);
89
        }
90
    }
91
    /**
92
     * Handle an incoming request.
93
     *
94
     * @param  \Closure  $next
95
     * @return mixed
96
     */
97
    public function handle(Request $request, \Closure $next) {
98
        if ($request->getMethod() == 'OPTIONS') {
99
            $response = new Response("", 200);
100
        }
101
        else {
102
            try{
103
                $response = $next($request);
104
            }catch(\Exception $e){
105
                $response = $this->exceptionRenderer->render($e);
106
            }
107
        }
108
        $this->setCorsHeaders($request, $response);
109
        return $response;
110
    }
111
112
    /**
113
     * @inject
114
     * @var ExceptionRenderer
115
     */
116
    private $exceptionRenderer;
117
}