SetFormat   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B process() 0 15 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace roxblnfk\SmartStream\Middleware;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use roxblnfk\SmartStream\Stream\BucketStream;
12
13
final class SetFormat implements MiddlewareInterface
14
{
15
    private string $format;
16
    private ?array $params;
17
    /** Replace existing format */
18
    private bool $force;
19
20 6
    public function __construct(string $format, ?array $params = [], bool $force = false)
21
    {
22 6
        $this->format = $format;
23 6
        $this->params = $params;
24 6
        $this->force = $force;
25 6
    }
26
27 6
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
28
    {
29 6
        $response = $handler->handle($request);
30 6
        $stream = $response->getBody();
31 6
        if (!$stream instanceof BucketStream) {
32 1
            return $response;
33
        }
34 5
        $data = $stream->getBucket();
35 5
        if ($data === null || !$data->isConvertable()) {
36 1
            return $response;
37
        }
38 4
        if ((!$stream->hasBucketFormat() && !$stream->isRenderStarted()) || $this->force) {
39 3
            return $response->withBody($stream->withBucket($data->withFormat($this->format, $this->params)));
40
        }
41 1
        return $response;
42
    }
43
}
44