Passed
Pull Request — master (#75)
by
unknown
10:54
created

RenderDataStream::getRelevantType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Middleware;
6
7
use App\Stream\Data\Converter;
8
use App\Stream\DataStream;
9
use App\Stream\GeneratorStream;
10
use App\Stream\Value\DataResponseProvider;
11
use Psr\Container\ContainerInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\StreamFactoryInterface;
15
use Psr\Http\Message\StreamInterface;
16
use Psr\Http\Server\MiddlewareInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
19
final class RenderDataStream implements MiddlewareInterface
20
{
21
    public string $defaultFormat = 'text/html';
22
    public array $converters = [];
23
    public bool $deferred = false;
24
25
    private ContainerInterface $container;
26
    private StreamFactoryInterface $streamFactory;
27
28
    public function __construct(ContainerInterface $container, StreamFactoryInterface $streamFactory)
29
    {
30
        $this->container = $container;
31
        $this->streamFactory = $streamFactory;
32
    }
33
34
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
35
    {
36
        $response = $handler->handle($request);
37
        $stream = $response->getBody();
38
        if (!$stream instanceof DataStream) {
39
            return $response;
40
        }
41
42
        $data = $stream->getData();
43
        $format = $data->getFormat() ?? $this->getRelevantType($request);
44
45
        if (!array_key_exists($format, $this->converters)) {
46
            throw new \Exception('Undefined format ' . $format);
47
        }
48
49
        $converter = $this->getConverter($this->converters[$format]);
50
51
        if ($this->deferred) {
52
            $response = $response->withBody(
53
                new GeneratorStream((fn () => yield $this->convertData($data, $converter))())
54
            );
55
        } else {
56
            $response = $response->withBody($this->convertData($data, $converter));
57
        }
58
59
        if ($data->getCode() !== null) {
60
            $response = $response->withStatus($data->getCode());
61
        }
62
63
        return $this->addHeaders($response->withHeader('Content-Type', $format), $data->getHeaders());
64
    }
65
66
    private function addHeaders(ResponseInterface $response, array $headers): ResponseInterface
67
    {
68
        foreach ($headers as $header => $value) {
69
            $response = $response->withHeader($header, $value);
70
        }
71
        return $response;
72
    }
73
    private function convertData(DataResponseProvider $data, Converter $converter): StreamInterface
74
    {
75
        $result = $converter->convert($data->getData(), $data->getParams());
76
        return $this->streamFactory->createStream($result);
77
    }
78
    private function getRelevantType(ServerRequestInterface $request): string
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

78
    private function getRelevantType(/** @scrutinizer ignore-unused */ ServerRequestInterface $request): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        # todo
81
        return $this->defaultFormat;
82
    }
83
    private function getConverter(?string $format): Converter
84
    {
85
        return $this->container->get($format);
86
    }
87
}
88