Passed
Push — master ( 34a32b...42166e )
by Alexander
07:42
created

Router::withoutAutoResponseOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\Middleware;
6
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Http\Method;
13
use Yiisoft\Http\Status;
14
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
15
use Yiisoft\Router\CurrentRoute;
16
use Yiisoft\Router\CurrentRouteInterface;
17
use Yiisoft\Router\UrlMatcherInterface;
18
19
final class Router implements MiddlewareInterface
20
{
21
    private UrlMatcherInterface $matcher;
22
    private ResponseFactoryInterface $responseFactory;
23
    private MiddlewareDispatcher $dispatcher;
24
    private CurrentRoute $currentRoute;
25
    private ?bool $autoResponseOptions = true;
26
27 9
    public function __construct(
28
        UrlMatcherInterface $matcher,
29
        ResponseFactoryInterface $responseFactory,
30
        MiddlewareDispatcher $dispatcher,
31
        CurrentRouteInterface $currentRoute
32
    ) {
33 9
        $this->matcher = $matcher;
34 9
        $this->responseFactory = $responseFactory;
35 9
        $this->dispatcher = $dispatcher;
36 9
        $this->currentRoute = $currentRoute;
37 9
    }
38
39 9
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
40
    {
41 9
        $result = $this->matcher->match($request);
42
43 9
        $this->currentRoute->setUri($request->getUri());
44
45 9
        if ($result->isMethodFailure()) {
46 3
            if ($this->autoResponseOptions && $request->getMethod() === Method::OPTIONS) {
47 1
                return $this->responseFactory->createResponse(Status::NO_CONTENT)
48 1
                    ->withHeader('Allow', implode(', ', $result->methods()));
49
            }
50 2
            return $this->responseFactory->createResponse(Status::METHOD_NOT_ALLOWED)
51 2
                ->withHeader('Allow', implode(', ', $result->methods()));
52
        }
53
54 6
        if (!$result->isSuccess()) {
55 1
            return $handler->handle($request);
56
        }
57
58 5
        $this->currentRoute->setRoute($result->route());
59 5
        $this->currentRoute->setArguments($result->arguments());
60
61 5
        return $result->withDispatcher($this->dispatcher)->process($request, $handler);
62
    }
63
64
    public function withAutoResponseOptions(): self
65
    {
66
        $new = clone $this;
67
        $new->autoResponseOptions = true;
68
        return $new;
69
    }
70
71 1
    public function withoutAutoResponseOptions(): self
72
    {
73 1
        $new = clone $this;
74 1
        $new->autoResponseOptions = false;
75 1
        return $new;
76
    }
77
}
78