Passed
Push — master ( e6142c...5854f2 )
by Alexpts
01:43
created

Psr17Factory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 86
ccs 34
cts 34
cp 1
rs 10
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createStreamFromResource() 0 3 1
A createServerRequest() 0 7 2
A createStream() 0 3 1
A createResponse() 0 7 2
A createUploadedFile() 0 12 2
A createUri() 0 3 1
A fromGlobals() 0 8 2
A createRequest() 0 7 2
A createStreamFromFile() 0 12 4
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\Psr7\Factory;
5
6
use InvalidArgumentException;
7
use Nyholm\Psr7Server\ServerRequestCreator;
8
use Psr\Http\Message\RequestFactoryInterface;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseFactoryInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestFactoryInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\StreamFactoryInterface;
15
use Psr\Http\Message\StreamInterface;
16
use Psr\Http\Message\UploadedFileFactoryInterface;
17
use Psr\Http\Message\UploadedFileInterface;
18
use Psr\Http\Message\UriFactoryInterface;
19
use Psr\Http\Message\UriInterface;
20
use PTS\Psr7\Request;
21
use PTS\Psr7\Response;
22
use PTS\Psr7\ServerRequest;
23
use PTS\Psr7\Stream;
24
use PTS\Psr7\UploadedFile;
25
use PTS\Psr7\Uri;
26
use RuntimeException;
27
use function fopen;
28
use function func_num_args;
29
use function in_array;
30
31
class Psr17Factory
32
    implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface,
33
    UploadedFileFactoryInterface, UriFactoryInterface
34
{
35
36
    /** @var ServerRequestCreator|null */
37
    protected ?ServerRequestCreator $creatorFromGlobal = null;
38
39 1
    public function createRequest(string $method, $uri): RequestInterface
40
    {
41 1
        if (is_string($uri)) {
42 1
            $uri = $this->createUri($uri);
43
        }
44
45 1
        return new Request($method, $uri);
46
    }
47
48 1
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
49
    {
50 1
        if (2 > func_num_args()) {
51 1
            $reasonPhrase = null;
52
        }
53
54 1
        return new Response($code, [], null, '1.1', $reasonPhrase);
55
    }
56
57 10
    public function createStream(string $content = ''): StreamInterface
58
    {
59 10
        return Stream::create($content);
60
    }
61
62 3
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
63
    {
64 3
        $resource = @fopen($filename, $mode);
65 3
        if (false === $resource) {
66 2
            if ('' === $mode || false === in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
67 1
                throw new InvalidArgumentException('The mode ' . $mode . ' is invalid.');
68
            }
69
70 1
            throw new RuntimeException('The file ' . $filename . ' cannot be opened.');
71
        }
72
73 1
        return Stream::create($resource);
74
    }
75
76 2
    public function createStreamFromResource($resource): StreamInterface
77
    {
78 2
        return Stream::create($resource);
79
    }
80
81 1
    public function createUploadedFile(
82
        StreamInterface $stream,
83
        int $size = null,
84
        int $error = \UPLOAD_ERR_OK,
85
        string $clientFilename = null,
86
        string $clientMediaType = null
87
    ): UploadedFileInterface {
88 1
        if (null === $size) {
89 1
            $size = $stream->getSize();
90
        }
91
92 1
        return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
93
    }
94
95 3
    public function createUri(string $uri = ''): UriInterface
96
    {
97 3
        return new Uri($uri);
98
    }
99
100 2
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
101
    {
102 2
        if (is_string($uri)) {
103 1
            $uri = $this->createUri($uri);
104
        }
105
106 2
        return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
107
    }
108
109 1
    public function fromGlobals(): ServerRequestInterface
110
    {
111 1
        if ($this->creatorFromGlobal === null) {
112 1
            $psr17 = new static;
113 1
            $this->creatorFromGlobal = new ServerRequestCreator($psr17, $psr17, $psr17, $psr17);
114
        }
115
116 1
        return $this->creatorFromGlobal->fromGlobals();
0 ignored issues
show
Bug introduced by
The method fromGlobals() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        return $this->creatorFromGlobal->/** @scrutinizer ignore-call */ fromGlobals();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
    }
118
}