Passed
Push — main ( 92bf86...8347d6 )
by Thomas
02:44
created

AbstractFactory::stream()   A

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