DispatcherMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Http\Middleware;
18
19
use Phauthentic\Infrastructure\Http\Dispatcher\DispatcherInterface;
20
use Phauthentic\Infrastructure\Http\Dispatcher\HandlerExtractorInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Psr\Http\Server\MiddlewareInterface;
24
use Psr\Http\Server\RequestHandlerInterface;
25
26
/**
27
 * A simple controller dispatcher middleware
28
 *
29
 * It will check if there is handler information is in the request. If there is
30
 * a it will take the resolved handler, this can be anything, a route object,
31
 * a string, a callable and pass it to the dispatcher.
32
 */
33
class DispatcherMiddleware implements MiddlewareInterface
34
{
35
    /**
36
     * @var \Phauthentic\Infrastructure\Http\Dispatcher\HandlerExtractorInterface
37
     */
38
    protected $handlerExtractor;
39
40
    /**
41
     * @var \Phauthentic\Infrastructure\Http\Dispatcher\DispatcherInterface
42
     */
43
    protected $dispatcher;
44
45
    /**
46
     * @param \Phauthentic\Infrastructure\Http\Dispatcher\HandlerExtractorInterface $handlerExtractor Handler Extractor
47
     * @param \Phauthentic\Infrastructure\Http\Dispatcher\DispatcherInterface $dispatcher Dispatcher
48
     */
49 2
    public function __construct(
50
        HandlerExtractorInterface $handlerExtractor,
51
        DispatcherInterface $dispatcher
52
    ) {
53 2
        $this->handlerExtractor = $handlerExtractor;
54 2
        $this->dispatcher = $dispatcher;
55 2
    }
56
57
    /**
58
     * Process an incoming server request.
59
     *
60
     * Processes an incoming server request in order to produce a response.
61
     * If unable to produce the response itself, it may delegate to the provided
62
     * request handler to do so.
63
     *
64
     * @param \Psr\Http\Message\ServerRequestInterface $request Server Request
65
     * @param \Psr\Http\Server\RequestHandlerInterface $handler Server Request Handler
66
     * @return \Psr\Http\Message\ResponseInterface
67
     */
68 2
    public function process(
69
        ServerRequestInterface $request,
70
        RequestHandlerInterface $handler
71
    ): ResponseInterface {
72 2
        $resolvedHandler = $this->handlerExtractor->extractHandler($request);
73
74 2
        if ($resolvedHandler === null) {
75 2
            $handler->handle($request);
76
        }
77
78 2
        $result = $this->dispatcher->dispatch($request, $resolvedHandler);
79
80 2
        if ($result instanceof ResponseInterface) {
81
            return $result;
82
        }
83
84 2
        return $handler->handle($request);
85
    }
86
}
87