Passed
Push — master ( 00c924...08f7da )
by Akmal
01:32 queued 11s
created

UriFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createUri() 0 13 4
A getScheme() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace OpenEngine\Mika\Core\Components\Http\Message\Uri;
4
5
use Psr\Http\Message\UriFactoryInterface;
6
use Psr\Http\Message\UriInterface;
7
8
class UriFactory implements UriFactoryInterface
9
{
10
    /**
11
     * Create a new URI.
12
     *
13
     * @param string $uri
14
     * @return UriInterface
15
     * @throws \InvalidArgumentException If the given URI cannot be parsed.
16
     */
17
    public function createUri(string $uri = ''): UriInterface
18
    {
19
        if (empty($uri)) {
20
            if (!isset($_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI'])) {
21
                throw new \RuntimeException('Can not create Uri');
22
            }
23
24
            $uri .= $this->getScheme() . '://' . $_SERVER['SERVER_NAME'] . '';
25
            $uri .= empty($_SERVER['SERVER_PORT']) ? '' : ':' . $_SERVER['SERVER_PORT'];
26
            $uri .= $_SERVER['REQUEST_URI'] ?? '';
27
        }
28
29
        return new Uri($uri);
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    private function getScheme(): string
36
    {
37
        return $_SERVER['REQUEST_SCHEME'] ?? 'http';
38
    }
39
}
40