Passed
Pull Request — master (#75)
by
unknown
14:51
created

SetFormat::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Middleware;
6
7
use App\Stream\DataStream;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
13
final class SetFormat implements MiddlewareInterface
14
{
15
    private string $converter;
16
    private array $params = [];
17
    /** Replace existing format */
18
    private bool $force;
19
20
    public function __construct(string $converter, array $params = [], bool $force = false)
21
    {
22
        $this->converter = $converter;
23
        $this->params = $params;
24
        $this->force = $force;
25
    }
26
27
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
28
    {
29
        $response = $handler->handle($request);
30
        $stream = $response->getBody();
31
        if ($stream instanceof DataStream) {
32
            if ($stream->getData()->getFormat() === null || $this->force) {
33
                $stream->getData()->setFormat($this->converter, $this->params);
34
            }
35
        }
36
        return $response;
37
    }
38
}
39