Test Failed
Push — master ( de7dd5...42d657 )
by Alexpts
02:02
created

Psr17Factory::createServerRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 6
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 1
    implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface,
33
    UploadedFileFactoryInterface, UriFactoryInterface
34 1
{
35 1
36
    /** @var ServerRequestCreator|null */
37
    protected ?ServerRequestCreator $creatorFromGlobal = null;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
38 1
39
    public function createRequest(string $method, $uri): RequestInterface
40
    {
41 1
        if (is_string($uri)) {
42
            $uri = $this->createUri($uri);
43 1
        }
44 1
45
        return new Request($method, $uri);
46
    }
47 1
48
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
49
    {
50 10
        if (2 > func_num_args()) {
51
            $reasonPhrase = null;
52 10
        }
53
54
        return new Response($code, [], null, '1.1', $reasonPhrase);
55 3
    }
56
57 3
    public function createStream(string $content = ''): StreamInterface
58 3
    {
59 2
        return Stream::create($content);
60 1
    }
61
62
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
63 1
    {
64
        $resource = @fopen($filename, $mode);
65
        if (false === $resource) {
66 1
            if ('' === $mode || false === in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
67
                throw new InvalidArgumentException('The mode ' . $mode . ' is invalid.');
68
            }
69 1
70
            throw new RuntimeException('The file ' . $filename . ' cannot be opened.');
71 1
        }
72
73
        return Stream::create($resource);
74 1
    }
75
76 1
    public function createStreamFromResource($resource): StreamInterface
77 1
    {
78
        return Stream::create($resource);
79
    }
80 1
81
    public function createUploadedFile(
82
        StreamInterface $stream,
83 2
        int $size = null,
84
        int $error = \UPLOAD_ERR_OK,
85 2
        string $clientFilename = null,
86
        string $clientMediaType = null
87
    ): UploadedFileInterface {
88 1
        if (null === $size) {
89
            $size = $stream->getSize();
90 1
        }
91 1
92
        return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
93
    }
94 1
95
    public function createUri(string $uri = ''): UriInterface
96
    {
97
        return new Uri($uri);
98
    }
99
100
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
101
    {
102
        if (is_string($uri)) {
103
            $uri = $this->createUri($uri);
104
        }
105
106
        return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
107
    }
108
109
    public function fromGlobals(): ServerRequestInterface
110
    {
111
        if ($this->creatorFromGlobal === null) {
112
            $psr17 = new static;
113
            $this->creatorFromGlobal = new ServerRequestCreator($psr17, $psr17, $psr17, $psr17);
114
        }
115
116
        return $this->creatorFromGlobal->fromGlobals();
117
    }
118
}