Completed
Push — master ( 25a785...8d412e )
by Alejandro
27s queued 11s
created

CrossDomainMiddleware::addOptionsHeaders()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Rest\Middleware;
5
6
use Fig\Http\Message\RequestMethodInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Shlinkio\Shlink\Rest\Authentication;
12
use Zend\Expressive\Router\RouteResult;
13
14
use function implode;
15
16
class CrossDomainMiddleware implements MiddlewareInterface, RequestMethodInterface
17
{
18 6
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
19
    {
20 6
        $response = $handler->handle($request);
21 6
        if (! $request->hasHeader('Origin')) {
22 1
            return $response;
23
        }
24
25
        // Add Allow-Origin header
26 5
        $response = $response->withHeader('Access-Control-Allow-Origin', $request->getHeader('Origin'))
27 5
                             ->withHeader('Access-Control-Expose-Headers', implode(', ', [
28 5
                                 Authentication\Plugin\ApiKeyHeaderPlugin::HEADER_NAME,
29
                                 Authentication\Plugin\AuthorizationHeaderPlugin::HEADER_NAME,
30
                             ]));
31 5
        if ($request->getMethod() !== self::METHOD_OPTIONS) {
32 1
            return $response;
33
        }
34
35 4
        return $this->addOptionsHeaders($request, $response);
36
    }
37
38 4
    private function addOptionsHeaders(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
39
    {
40
        /** @var RouteResult|null $matchedRoute */
41 4
        $matchedRoute = $request->getAttribute(RouteResult::class);
42 4
        $matchedMethods = $matchedRoute !== null ? $matchedRoute->getAllowedMethods() : [
43 2
            self::METHOD_GET,
44 2
            self::METHOD_POST,
45 2
            self::METHOD_PUT,
46 2
            self::METHOD_PATCH,
47 2
            self::METHOD_DELETE,
48 4
            self::METHOD_OPTIONS,
49
        ];
50
        $corsHeaders = [
51 4
            'Access-Control-Allow-Methods' => implode(',', $matchedMethods),
0 ignored issues
show
Bug introduced by
It seems like $matchedMethods can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            'Access-Control-Allow-Methods' => implode(',', /** @scrutinizer ignore-type */ $matchedMethods),
Loading history...
52 4
            'Access-Control-Max-Age' => '1000',
53 4
            'Access-Control-Allow-Headers' => $request->getHeaderLine('Access-Control-Request-Headers'),
54
        ];
55
56 4
        foreach ($corsHeaders as $key => $value) {
57 4
            $response = $response->withHeader($key, $value);
58
        }
59
60 4
        return $response;
61
    }
62
}
63