Passed
Pull Request — master (#75)
by
unknown
22:37 queued 07:36
created

RenderDataStream   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 13
c 1
b 0
f 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 17 3
A __construct() 0 3 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\Data\PrintRConverter;
9
use App\Stream\DataStream;
10
use Psr\Container\ContainerInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Server\MiddlewareInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
16
final class RenderDataStream implements MiddlewareInterface
17
{
18
    private ContainerInterface $container;
19
20
    public function __construct(ContainerInterface $container)
21
    {
22
        $this->container = $container;
23
    }
24
25
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
26
    {
27
        $response = $handler->handle($request);
28
        $stream = $response->getBody();
29
        if (!$stream instanceof DataStream) {
30
            return $response;
31
        }
32
        $converterClass = $stream->getConverter();
33
        if ($converterClass === null) {
34
            # todo: get most relevant format from header
35
            $converterClass = PrintRConverter::class;
36
        }
37
        /** @var Converter $converter */
38
        $converter = $this->container->get($converterClass);
39
        $stream->render($converter);
40
41
        return $converter->setHeaders($response);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $converter->setHeaders($response) returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\ResponseInterface.
Loading history...
42
    }
43
}
44