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

FormatWebResponse   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A process() 0 8 3
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