Passed
Pull Request — master (#75)
by
unknown
11:38
created

SetStreamConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 2
b 0
f 0
dl 0
loc 24
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 10 4
A __construct() 0 5 1
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 SetStreamConverter 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->getConverter() === null || $this->force) {
33
                $stream->setConverter($this->converter, $this->params);
34
            }
35
        }
36
        return $response;
37
    }
38
}
39