Completed
Push — master ( b9a7c3...1619f7 )
by Sébastien
09:45 queued 07:42
created

ApiCorsMiddlewareFactory   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 22
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 20 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Middleware;
6
7
use Psr\Container\ContainerInterface;
8
use Tuupola\Middleware\CorsMiddleware;
9
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
10
11
class ApiCorsMiddlewareFactory
12
{
13
    public function __invoke(ContainerInterface $container): CorsMiddleware
14
    {
15
        $problemDetailsResponseFactory = $container->get(ProblemDetailsResponseFactory::class);
16
17
        return new CorsMiddleware([
18
            'origin'         => ['*'],
19
            'methods'        => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
20
            'headers.allow'  => ['Authorization', 'If-Match', 'If-Unmodified-Since', 'Content-Type'],
21
            'headers.expose' => ['Etag'],
22
            'credentials'    => true,
23
            'cache'          => 0,
24
            'error'          => function ($request, $response, $arguments) use ($problemDetailsResponseFactory) {
25
                //return $this->error($request, $response, $arguments);
26
                return $problemDetailsResponseFactory->createResponse(
27
                    $request,
28
                    401,
29
                    '',
30
                    $arguments['message'],
31
                    '',
32
                    []
33
                );
34
            }
35
        ]);
36
    }
37
}
38