Passed
Push — main ( a1a461...2097df )
by Thomas
12:50
created

AbstractFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 10
dl 0
loc 28
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A response() 0 3 1
A streamFromFile() 0 3 1
A stream() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Chuck\Psr;
6
7
use Conia\Chuck\Exception\RuntimeException;
8
use Psr\Http\Message\ResponseFactoryInterface as PsrResponseFactory;
9
use Psr\Http\Message\ResponseInterface as PsrResponse;
10
use Psr\Http\Message\ServerRequestInterface as PsrServerRequest;
11
use Psr\Http\Message\StreamFactoryInterface as PsrStreamFactory;
12
use Psr\Http\Message\StreamInterface as PsrStream;
13
use Stringable;
14
15
abstract class AbstractFactory implements Factory
16
{
17
    protected PsrResponseFactory $responseFactory;
18
    protected PsrStreamFactory $streamFactory;
19
20
    abstract public function request(): PsrServerRequest;
21
22 59
    public function response(int $code = 200, string $reasonPhrase = ''): PsrResponse
23
    {
24 59
        return $this->responseFactory->createResponse($code, $reasonPhrase);
25
    }
26
27 56
    public function stream(mixed $content = ''): PsrStream
28
    {
29 56
        if (is_string($content) || $content instanceof Stringable) {
30 50
            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'): PsrStream
41
    {
42 7
        return $this->streamFactory->createStreamFromFile($filename, $mode);
43
    }
44
}
45