Completed
Push — master ( e33e66...44de45 )
by Tobias
03:12
created

UriFactory::createUriFromArray()   D

Complexity

Conditions 9
Paths 72

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 10.0551

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 13
cts 17
cp 0.7647
rs 4.909
c 0
b 0
f 0
cc 9
eloc 17
nc 72
nop 1
crap 10.0551
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7\Factory;
6
7
use Interop\Http\Factory\UriFactoryInterface;
8
use Nyholm\Psr7\Uri;
9
use Psr\Http\Message\UriInterface;
10
11
/**
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class UriFactory implements \Http\Message\UriFactory, UriFactoryInterface
15
{
16 18
    public function createUri($uri = ''): UriInterface
17
    {
18 18
        if ($uri instanceof UriInterface) {
19
            return $uri;
20
        }
21
22 18
        return new Uri($uri);
23
    }
24
25
    /**
26
     * Create a new uri from server variable.
27
     *
28
     * @param array $server Typically $_SERVER or similar structure.
29
     *
30
     * @return UriInterface
31
     */
32 22
    public function createUriFromArray(array $server): UriInterface
33
    {
34 22
        $uri = new Uri('');
35
36 22
        if (isset($server['REQUEST_SCHEME'])) {
37
            $uri = $uri->withScheme($server['REQUEST_SCHEME']);
38 22
        } elseif (isset($server['HTTPS'])) {
39
            $uri = $uri->withScheme('on' === $server['HTTPS'] ? 'https' : 'http');
40
        }
41
42 22
        if (isset($server['HTTP_HOST'])) {
43 7
            $uri = $uri->withHost($server['HTTP_HOST']);
44 15
        } elseif (isset($server['SERVER_NAME'])) {
45
            $uri = $uri->withHost($server['SERVER_NAME']);
46
        }
47
48 22
        if (isset($server['SERVER_PORT'])) {
49
            $uri = $uri->withPort($server['SERVER_PORT']);
50
        }
51
52 22
        if (isset($server['REQUEST_URI'])) {
53 7
            $uri = $uri->withPath(current(explode('?', $server['REQUEST_URI'])));
54
        }
55
56 22
        if (isset($server['QUERY_STRING'])) {
57 7
            $uri = $uri->withQuery($server['QUERY_STRING']);
58
        }
59
60 22
        return $uri;
61
    }
62
}
63