Passed
Pull Request — master (#233)
by Dmitriy
02:13
created

DeferredResponseFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 8 3
A __construct() 0 3 1
1
<?php
2
3
namespace Yiisoft\Yii\Web\Middleware;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
use Yiisoft\Yii\Web\Formatter\ResponseFormatterInterface;
10
use Yiisoft\Yii\Web\Response;
11
12
class DeferredResponseFormatter implements MiddlewareInterface
13
{
14
    private ResponseFormatterInterface $responseFormatter;
15
16
    public function __construct(ResponseFormatterInterface $responseFormatter)
17
    {
18
        $this->responseFormatter = $responseFormatter;
19
    }
20
21
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
22
    {
23
        $response = $handler->handle($request);
24
        if ($response instanceof Response && !$response->hasResponseFormatter()) {
25
            $response = $response->withResponseFormatter($this->responseFormatter);
26
        }
27
28
        return $response;
29
    }
30
}
31