Passed
Push — master ( a3d401...21e2dd )
by Dominik
01:45
created

CorsPreflightRequestHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Cors;
6
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
12
final class CorsPreflightRequestHandler implements RequestHandlerInterface
13
{
14
    /**
15
     * @var ResponseFactoryInterface
16
     */
17
    private $responseFactory;
18
19
    /**
20
     * @var string[]
21
     */
22
    private $allowMethods;
23
24
    /**
25
     * @var string[]
26
     */
27
    private $allowHeaders;
28
29
    /**
30
     * @var int
31
     */
32
    private $maxAge;
33
34
    /**
35
     * @param ResponseFactoryInterface $responseFactory
36
     * @param string[]                 $allowMethods
37
     * @param string[]                 $allowHeaders
38
     * @param int                      $maxAge
39
     */
40 1
    public function __construct(
41
        ResponseFactoryInterface $responseFactory,
42
        array $allowMethods,
43
        array $allowHeaders,
44
        int $maxAge = 600
45
    ) {
46 1
        $this->responseFactory = $responseFactory;
47 1
        $this->allowMethods = $allowMethods;
48 1
        $this->allowHeaders = $allowHeaders;
49 1
        $this->maxAge = $maxAge;
50 1
    }
51
52
    /**
53
     * @param ServerRequestInterface $request
54
     *
55
     * @return ResponseInterface
56
     */
57 1
    public function handle(ServerRequestInterface $request): ResponseInterface
58
    {
59 1
        return $this->responseFactory->createResponse(204)
60 1
            ->withHeader('Access-Control-Allow-Methods', implode(', ', $this->allowMethods))
61 1
            ->withHeader('Access-Control-Allow-Headers', implode(', ', $this->allowHeaders))
62 1
            ->withHeader('Access-Control-Max-Age', (string) $this->maxAge)
63
        ;
64
    }
65
}
66