Completed
Push — master ( d89245...98d38e )
by Tobias
02:16
created

UriFactory::createUriFromArray()   D

Complexity

Conditions 9
Paths 72

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 11.0604

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 12
cts 17
cp 0.7059
rs 4.909
c 0
b 0
f 0
cc 9
eloc 17
nc 72
nop 1
crap 11.0604
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 7
    public function createUriFromArray(array $server): UriInterface
33
    {
34 7
        $uri = new Uri('');
35
36 7
        if (isset($server['REQUEST_SCHEME'])) {
37
            $uri = $uri->withScheme($server['REQUEST_SCHEME']);
38 7
        } elseif (isset($server['HTTPS'])) {
39
            $uri = $uri->withScheme($server['HTTPS'] === 'on' ? 'https' : 'http');
40
        }
41
42 7
        if (isset($server['HTTP_HOST'])) {
43 7
            $uri = $uri->withHost($server['HTTP_HOST']);
44
        } elseif (isset($server['SERVER_NAME'])) {
45
            $uri = $uri->withHost($server['SERVER_NAME']);
46
        }
47
48 7
        if (isset($server['SERVER_PORT'])) {
49
            $uri = $uri->withPort($server['SERVER_PORT']);
50
        }
51
52 7
        if (isset($server['REQUEST_URI'])) {
53 7
            $uri = $uri->withPath(current(explode('?', $server['REQUEST_URI'])));
54
        }
55
56 7
        if (isset($server['QUERY_STRING'])) {
57 7
            $uri = $uri->withQuery($server['QUERY_STRING']);
58
        }
59
60 7
        return $uri;
61
    }
62
}
63