Cors   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 17 3
1
<?php
2
3
namespace App\Middleware;
4
5
use Slim\Http\Request;
6
use Slim\Http\Response;
7
8
class Cors implements MiddlewareInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $options;
14
15
    /**
16
     * Constructor.
17
     *
18
     * @param array $options
19
     */
20
    public function __construct(array $options)
21
    {
22
        $this->options = $options;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function __invoke(Request $request, Response $response, callable $next)
29
    {
30
        $headers = [
31
            'origin'         => 'Access-Control-Allow-Origin',
32
            'methods'        => 'Access-Control-Allow-Methods',
33
            'allow_headers'  => 'Access-Control-Allow-Headers',
34
            'expose_headers' => 'Access-Control-Expose-Headers',
35
            'max_age'        => 'Access-Control-Max-Age',
36
        ];
37
38
        foreach ($headers as $key => $name) {
39
            if (isset($this->options[$key])) {
40
                $response = $response->withHeader($name, $this->options[$key]);
41
            }
42
        }
43
44
        return $next($request, $response);
45
    }
46
}
47