Passed
Pull Request — master (#75)
by
unknown
24:56 queued 09:54
created

RenderDataStream::convertData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
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\Value\DataResponseProvider;
10
use Psr\Container\ContainerInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Message\StreamFactoryInterface;
14
use Psr\Http\Message\StreamInterface;
15
use Psr\Http\Server\MiddlewareInterface;
16
use Psr\Http\Server\RequestHandlerInterface;
17
18
final class RenderDataStream implements MiddlewareInterface
19
{
20
    public string $defaultFormat = 'text/html';
21
    public array $converters = [];
22
23
    private ContainerInterface $container;
24
    private StreamFactoryInterface $streamFactory;
25
26
    public function __construct(ContainerInterface $container, StreamFactoryInterface $streamFactory)
27
    {
28
        $this->container = $container;
29
        $this->streamFactory = $streamFactory;
30
    }
31
32
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
33
    {
34
        $response = $handler->handle($request);
35
        $stream = $response->getBody();
36
        if (!$stream instanceof DataStream) {
37
            return $response;
38
        }
39
40
        $data = $stream->getData();
41
        $format = $data->getFormat() ?? $this->getRelevantType($request);
42
43
        if (!array_key_exists($format, $this->converters)) {
44
            dd($this->converters);
45
            throw new \Exception('Undefined format ' . $format);
46
        }
47
48
        $converter = $this->getConverter($this->converters[$format]);
49
50
        $response = $response->withBody($this->convertData($data, $converter));
51
52
        if ($data->getCode() !== null) {
53
            $response = $response->withStatus($data->getCode());
54
        }
55
56
        return $this->addHeaders($response->withHeader('Content-Type', $format), $data->getHeaders());
57
    }
58
59
    private function addHeaders(ResponseInterface $response, array $headers): ResponseInterface
60
    {
61
        foreach ($headers as $header => $value) {
62
            $response = $response->withHeader($header, $value);
63
        }
64
        return $response;
65
    }
66
    private function convertData(DataResponseProvider $data, Converter $converter): StreamInterface
67
    {
68
        $result = $converter->convert($data->getData(), $data->getParams());
69
        return $this->streamFactory->createStream($result);
70
    }
71
    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

71
    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...
72
    {
73
        # todo
74
        return $this->defaultFormat;
75
    }
76
    private function getConverter(?string $format): Converter
77
    {
78
        return $this->container->get($format);
79
    }
80
}
81