Passed
Push — master ( fb282f...fe0d43 )
by Terry
01:45
created

RequestFactory::fromNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\RequestFactoryInterface;
16
use Psr\Http\Message\RequestInterface;
17
use Shieldon\Psr17\UriFactory;
18
use Shieldon\Psr17\StreamFactory;
19
use Shieldon\Psr17\Utils\SuperGlobal;
20
use Shieldon\Psr7\Request;
21
22
use function str_replace;
23
use function extract;
24
25
/**
26
 * PSR-17 Request Factory
27
 */
28
class RequestFactory implements RequestFactoryInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function createRequest(string $method, $uri): RequestInterface
34
    {
35
        extract(SuperGlobal::extract());
36
37
        $protocol = $server['SERVER_PROTOCOL'] ?? '1.1';
38
        $protocol = str_replace('HTTP/', '',  $protocol);
39
40
        $uriFactory = new UriFactory();
41
        $streamFactory = new StreamFactory();
42
43
        $uri = $uriFactory->createUri($uri);
44
        $body = $streamFactory->createStream();
45
46
        return new Request(
47
            $method,
48
            $uri,
49
            $body,
50
            $header, // from extract.
51
            $protocol
52
        );
53
    }
54
55
    /*
56
    |--------------------------------------------------------------------------
57
    | Non PSR-7 Methods.
58
    |--------------------------------------------------------------------------
59
    */
60
61
    /**
62
     * Create a new Request.
63
     *
64
     * @return RequestInterface
65
     */
66
    public static function fromNew(): RequestInterface
67
    {
68
        return new Request();
69
    }
70
}
71