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

UriFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 0
loc 49
ccs 16
cts 21
cp 0.7619
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createUri() 0 8 2
D createUriFromArray() 0 30 9
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