AbstractFactory::stream()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Chuck\Psr;
6
7
use Conia\Chuck\Exception\RuntimeException;
8
use Conia\Chuck\Factory;
9
use Psr\Http\Message\ResponseFactoryInterface as PsrResponseFactory;
10
use Psr\Http\Message\ResponseInterface as PsrResponse;
11
use Psr\Http\Message\ServerRequestInterface as PsrServerRequest;
12
use Psr\Http\Message\StreamFactoryInterface as PsrStreamFactory;
13
use Psr\Http\Message\StreamInterface as PsrStream;
14
use Stringable;
15
16
abstract class AbstractFactory implements Factory
17
{
18
    protected PsrResponseFactory $responseFactory;
19
    protected PsrStreamFactory $streamFactory;
20
21
    abstract public function request(): PsrServerRequest;
22
23 57
    public function response(int $code = 200, string $reasonPhrase = ''): PsrResponse
24
    {
25 57
        return $this->responseFactory->createResponse($code, $reasonPhrase);
26
    }
27
28 50
    public function stream(mixed $content = ''): PsrStream
29
    {
30 50
        if (is_string($content) || $content instanceof Stringable) {
31 45
            return $this->streamFactory->createStream((string)$content);
32
        }
33
34 8
        if (is_resource($content)) {
35 4
            return $this->streamFactory->createStreamFromResource($content);
36
        }
37
38 4
        throw new RuntimeException('Only strings, Stringable or resources are allowed');
39
    }
40
41 7
    public function streamFromFile(string $filename, string $mode = 'r'): PsrStream
42
    {
43 7
        return $this->streamFactory->createStreamFromFile($filename, $mode);
44
    }
45
}
46