Completed
Push — master ( d3629a...99177e )
by Dusan
02:35
created

RequestHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1
ccs 14
cts 14
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 19 3
1
<?php
2
/**
3
 * This file is part of the http-middleware package.
4
 *
5
 * Copyright (c) Dusan Vejin
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace WeCodeIn\Http\Server;
14
15
use Interop\Http\Factory\ResponseFactoryInterface;
16
use Interop\Http\Server\RequestHandlerInterface;
17
use Interop\Http\Server\MiddlewareInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use WeCodeIn\Http\Server\Exception\InvalidMiddlewareResponseException;
21
22
class RequestHandler implements RequestHandlerInterface
23
{
24
    protected $responseFactory;
25
    protected $middlewares;
26
27 6
    public function __construct(ResponseFactoryInterface $responseFactory, MiddlewareInterface ...$middlewares)
28
    {
29 6
        $this->responseFactory = $responseFactory;
30 6
        $this->middlewares = $middlewares;
31 6
    }
32
33 5
    public function handle(ServerRequestInterface $request) : ResponseInterface
34
    {
35 5
        $handler = clone $this;
36
37 5
        if (null === key($handler->middlewares)) {
38 2
            return $this->responseFactory->createResponse();
39
        }
40
41 4
        $middleware = current($handler->middlewares);
42 4
        next($handler->middlewares);
43
44 4
        $response = $middleware->process($request, $handler);
45
46 4
        if (!$response instanceof ResponseInterface) {
47 1
            throw InvalidMiddlewareResponseException::forMiddleware($middleware);
48
        }
49
50 3
        return $response;
51
    }
52
}
53