CORSMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace TH\Silex\CORS;
4
5
use Symfony\Component\HttpFoundation\Response;
6
use Symfony\Component\HttpFoundation\Request;
7
8
class CORSMiddleware
9
{
10
    private $origin;
11
    private $methods;
12
    private $headers;
13
14
    /**
15
     * @param string   $origin  orgin domain
16
     * @param string[] $methods methods
17
     * @param string[] $headers headers
18
     */
19
    public function __construct($origin, Array $methods, Array $headers)
20
    {
21
        $this->origin  = $origin;
22
        $this->methods = implode(', ', $methods);
23
        $this->headers = implode(', ', $headers);
24
    }
25
26
    public function __invoke(Request $request, Response $response)
27
    {
28
        $response->headers->set('Access-Control-Allow-Origin', $this->origin);
29
        $response->headers->set('Access-Control-Allow-Methods', $this->methods);
30
        $response->headers->set('Access-Control-Allow-Headers', $this->headers);
31
        return $response;
32
    }
33
}
34