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

RenderDataStream   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 20
c 1
b 0
f 0
dl 0
loc 48
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 14 2
A getConverter() 0 3 1
A __construct() 0 4 1
A convertData() 0 4 1
A getRelevantType() 0 4 1
A addHeaders() 0 6 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\Data\PrintRConverter;
9
use App\Stream\DataStream;
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
    private ContainerInterface $container;
22
    private StreamFactoryInterface $streamFactory;
23
24
    public function __construct(ContainerInterface $container, StreamFactoryInterface $streamFactory)
25
    {
26
        $this->container = $container;
27
        $this->streamFactory = $streamFactory;
28
    }
29
30
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
31
    {
32
        $response = $handler->handle($request);
33
        $stream = $response->getBody();
34
        if (!$stream instanceof DataStream) {
35
            return $response;
36
        }
37
38
        $data = $stream->getData();
39
        $converter = $this->getConverter($data->getFormat(), $request);
40
41
        $response = $response->withBody($this->convertData($data, $converter));
42
43
        return $this->addHeaders($response->withHeader('Content-Type', $converter::getFormat()), $data->getHeaders());
44
    }
45
46
    private function addHeaders(ResponseInterface $response, array $headers): ResponseInterface
47
    {
48
        foreach ($headers as $header => $value) {
49
            $response = $response->withHeader($header, $value);
50
        }
51
        return $response;
52
    }
53
    private function convertData(DataResponseProvider $data, Converter $converter): StreamInterface
54
    {
55
        $result = $converter->convert($data->getData(), $data->getParams());
56
        return $this->streamFactory->createStream($result);
57
    }
58
59
    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

59
    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...
60
    {
61
        # todo
62
        return PrintRConverter::class;
63
    }
64
    private function getConverter(?string $format, ServerRequestInterface $request): Converter
65
    {
66
        return $this->container->get($format ?? $this->getRelevantType($request));
67
    }
68
}
69