ServerRequestFactory::createServerRequest()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 30
ccs 22
cts 22
cp 1
rs 9.584
cc 3
nc 4
nop 3
crap 3
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Psr17;
14
15
use Psr\Http\Message\ServerRequestFactoryInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Psr\Http\Message\UriInterface;
18
use Shieldon\Psr17\StreamFactory;
19
use Shieldon\Psr17\UriFactory;
20
use Shieldon\Psr17\Utils\SuperGlobal;
21
use Shieldon\Psr7\ServerRequest;
22
23
use function str_replace;
24
use function extract;
25
26
/**
27
 * PSR-17 Server Request Factory
28
 */
29
class ServerRequestFactory implements ServerRequestFactoryInterface
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
35
    {
36 1
        extract(SuperGlobal::extract());
37
38 1
        if ($serverParams !== []) {
39 1
            $server = $serverParams;
40
        }
41
42 1
        $protocol = $server['SERVER_PROTOCOL'] ?? '1.1';
43 1
        $protocol = str_replace('HTTP/', '',  $protocol);
44
45 1
        if (!($uri instanceof UriInterface)) {
46 1
            $uriFactory = new UriFactory();
47 1
            $uri = $uriFactory->createUri($uri);
48
        }
49
50 1
        $streamFactory = new StreamFactory();
51 1
        $body = $streamFactory->createStream();
52
53 1
        return new ServerRequest(
54 1
            $method,
55 1
            $uri,
56 1
            $body,
57 1
            $header, // from extract.
58 1
            $protocol,
59 1
            $server, // from extract.
60 1
            $cookie, // from extract.
61 1
            $post,   // from extract.
62 1
            $get,    // from extract.
63 1
            $files   // from extract.
64 1
        );
65
    }
66
67
    /*
68
    |--------------------------------------------------------------------------
69
    | Non PSR-7 Methods.
70
    |--------------------------------------------------------------------------
71
    */
72
73
    /**
74
     * Create a ServerRequestInterface instance from global variable.
75
     *
76
     * @return ServerRequestInterface
77
     */
78 4
    public static function fromGlobal(): ServerRequestInterface
79
    {
80 4
        extract(SuperGlobal::extract());
81
82
        // HTTP method.
83 4
        $method = $server['REQUEST_METHOD'] ?? 'GET';
84
85
        // HTTP protocal version.
86 4
        $protocol = $server['SERVER_PROTOCOL'] ?? '1.1';
87 4
        $protocol = str_replace('HTTP/', '',  $protocol);
88
89 4
        $uri = UriFactory::fromGlobal();
90
91 4
        $streamFactory = new StreamFactory();
92 4
        $body = $streamFactory->createStream();
93
94 4
        return new ServerRequest(
95 4
            $method,
96 4
            $uri,
97 4
            $body,
98 4
            $header, // from extract.
99 4
            $protocol,
100 4
            $server, // from extract.
101 4
            $cookie, // from extract.
102 4
            $post,   // from extract.
103 4
            $get,    // from extract.
104 4
            $files   // from extract.
105 4
        );
106
    }
107
}
108