BucketStreamMiddleware::process()   B
last analyzed

Complexity

Conditions 10
Paths 11

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 15
c 1
b 0
f 0
nc 11
nop 2
dl 0
loc 30
ccs 16
cts 16
cp 1
crap 10
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Message\StreamFactoryInterface;
10
use Psr\Http\Message\StreamInterface;
11
use Psr\Http\Server\MiddlewareInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
use roxblnfk\SmartStream\Stream\BucketStream;
14
use Yiisoft\Http\Header;
15
16
final class BucketStreamMiddleware implements MiddlewareInterface
17
{
18
    private StreamFactoryInterface $streamFactory;
19
20 7
    public function __construct(StreamFactoryInterface $streamFactory)
21
    {
22 7
        $this->streamFactory = $streamFactory;
23 7
    }
24
25 7
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
26
    {
27 7
        $response = $handler->handle($request);
28 7
        $stream = $response->getBody();
29 7
        if (!$stream instanceof BucketStream) {
30 1
            return $response;
31
        }
32
33 6
        $bucket = $stream->getBucket();
34
35
        // Bucket has been detached
36 6
        if ($bucket === null) {
37 1
            return $stream->isReadable() ? $response : $response->withBody($this->streamFactory->createStream(''));
38
        }
39
40 5
        if (!$stream->hasConverter() && !$bucket->isConvertable() && !$stream->isReadable()) {
41 3
            $response = $response->withBody($this->createReadableStream($bucket->getData()));
42
        }
43
44
        // Set MIME type
45 5
        $matching = $stream->getMatchedResult();
46 5
        if ($matching !== null && $matching->getMimeType() !== null) {
47 2
            $response = $response->withHeader(Header::CONTENT_TYPE, $matching->getMimeType());
48
        }
49
50
        // Update request
51 5
        if ($bucket->getStatusCode() !== null) {
52 1
            $response = $response->withStatus($bucket->getStatusCode());
53
        }
54 5
        return $this->injectHeaders($response, $bucket->getHeaders());
55
    }
56
57 5
    private function injectHeaders(ResponseInterface $response, array $headers): ResponseInterface
58
    {
59 5
        foreach ($headers as $header => $value) {
60 4
            $response = $response->withHeader($header, $value);
61
        }
62 5
        return $response;
63
    }
64 3
    private function createReadableStream($data): StreamInterface
65
    {
66 3
        if ($data instanceof \SplFileInfo) {
67 1
            return $this->streamFactory->createStreamFromFile($data->getRealPath());
68
        }
69 2
        if (is_resource($data)) {
70 1
            return $this->streamFactory->createStreamFromResource($data);
71
        }
72 1
        return $this->streamFactory->createStream(is_string($data) ? $data : '');
73
    }
74
}
75