NotAllowedMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A process() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Framework;
6
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
use Psr\Http\Message\ResponseFactoryInterface;
10
use Interop\Renderer\RendererInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Aidphp\Routing\Middleware\MethodMiddleware;
14
15
class NotAllowedMiddleware implements MiddlewareInterface
16
{
17
    public const TEMPLATE = 'not-allowed';
18
19
    protected $factory;
20
    protected $renderer;
21
    protected $template;
22
23 2
    public function __construct(ResponseFactoryInterface $factory, RendererInterface $renderer, string $template = self::TEMPLATE)
24
    {
25 2
        $this->factory  = $factory;
26 2
        $this->renderer = $renderer;
27 2
        $this->template = $template;
28 2
    }
29
30 2
    public function process(ServerRequestInterface $req, RequestHandlerInterface $handler): ResponseInterface
31
    {
32 2
        $methods = $req->getAttribute(MethodMiddleware::class);
33
34 2
        if (is_array($methods))
35
        {
36 1
            $res = $this->factory->createResponse(405)->withHeader('Allow', implode(',', $methods));
37
38 1
            $res->getBody()->write($this->renderer->render($this->template, ['method' => $req->getMethod()]));
39
40 1
            return $res;
41
        }
42
43 1
        return $handler->handle($req);
44
    }
45
}