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

UriFactory::getScheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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