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

FormatWebResponse::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\WebResponse;
11
12
class FormatWebResponse 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 WebResponse && !$response->hasResponseFormatter()) {
25
            $response = $response->withResponseFormatter($this->responseFormatter);
26
        }
27
28
        return $response;
29
    }
30
}
31