Completed
Push — master ( 4e07a2...96fcad )
by Tobias
03:18
created

UriFactory::createUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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