AbstractFactory   A
last analyzed

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 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