ServerRequestFactory   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 54
ccs 26
cts 26
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createServerRequest() 0 9 1
A fromGlobals() 0 24 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message;
13
14
use Psr\Http\Message\ServerRequestFactoryInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Sunrise\Http\Message\Stream\PhpInputStream;
17
18
/**
19
 * @psalm-suppress ClassMustBeFinal
20
 */
21
class ServerRequestFactory implements ServerRequestFactoryInterface
22
{
23
    /**
24
     * @param array<array-key, mixed>|null $serverParams
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed>|null at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>|null.
Loading history...
25
     * @param array<array-key, mixed>|null $queryParams
26
     * @param array<array-key, mixed>|null $cookieParams
27
     * @param array<array-key, mixed>|null $uploadedFiles
28
     * @param array<array-key, mixed>|null $parsedBody
29
     *
30
     * @link http://php.net/manual/en/language.variables.superglobals.php
31
     * @link https://www.php-fig.org/psr/psr-15/meta/
32
     */
33 40
    public static function fromGlobals(
34
        ?array $serverParams = null,
35
        ?array $queryParams = null,
36
        ?array $cookieParams = null,
37
        ?array $uploadedFiles = null,
38
        ?array $parsedBody = null
39
    ): ServerRequestInterface {
40 40
        $serverParams ??= $_SERVER;
41 40
        $queryParams ??= $_GET;
42 40
        $cookieParams ??= $_COOKIE;
43 40
        $uploadedFiles ??= $_FILES;
44 40
        $parsedBody ??= $_POST;
45
46 40
        return new ServerRequest(
47 40
            server_request_protocol_version($serverParams),
48 40
            server_request_method($serverParams),
49 40
            server_request_uri($serverParams),
50 40
            server_request_headers($serverParams),
51 40
            new PhpInputStream(),
52 40
            $serverParams,
53 40
            $queryParams,
54 40
            $cookieParams,
55 40
            server_request_files($uploadedFiles),
56 40
            $parsedBody
57 40
        );
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     *
63
     * @param mixed $uri
64
     * @param array<array-key, mixed> $serverParams
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
65
     */
66 19
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
67
    {
68 19
        return new ServerRequest(
69 19
            server_request_protocol_version($serverParams),
70 19
            $method,
71 19
            $uri,
72 19
            server_request_headers($serverParams),
73 19
            null, // body
74 19
            $serverParams
75 19
        );
76
    }
77
}
78