Completed
Push — master ( 2b5a31...b9c679 )
by BENOIT
01:22
created

UriFactory::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\UriFactory;
4
5
use BenTools\UriFactory\Adapter\AdapterInterface;
6
use BenTools\UriFactory\Adapter\GuzzleAdapter;
7
use BenTools\UriFactory\Adapter\LeagueUriAdapter;
8
use BenTools\UriFactory\Adapter\ZendDiactorosAdapter;
9
use Psr\Http\Message\UriInterface;
10
11
class UriFactory implements UriFactoryInterface
12
{
13
14
    /**
15
     * @var AdapterInterface[]
16
     */
17
    private $adapters = [];
18
19
    /**
20
     * UriFactory constructor.
21
     */
22
    protected function __construct()
23
    {
24
        $this->adapters = $this->getDefaultAdapters();
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    private function getDefaultAdapters()
31
    {
32
        return [
33
            GuzzleAdapter::class,
34
            ZendDiactorosAdapter::class,
35
            LeagueUriAdapter::class,
36
        ];
37
    }
38
39
    /**
40
     * @param string $uri
41
     * @param UriFactoryInterface|null $factory
42
     * @return UriInterface
43
     */
44
    public function createUri(string $uri, UriFactoryInterface $factory = null): UriInterface
45
    {
46
        if (null !== $factory) {
47
            return $factory->createUri($uri);
48
        }
49
50
        // Automatic discovery
51
        foreach ($this->adapters as $adapter) {
52
            if ($adapter::isInstalled()) {
53
                return $adapter::factory()->createUri($uri);
54
            }
55
        }
56
        throw new \RuntimeException(
57
            "No adapter is installed. Please install guzzlehttp/psr7, zendframework/zend-diactoros or league/uri."
58
        );
59
    }
60
61
    /**
62
     * @param UriFactoryInterface|null $factory
63
     * @return UriInterface
64
     */
65
    public function createUriFromCurrentLocation(UriFactoryInterface $factory = null): UriInterface
66
    {
67
        if (!isset($_SERVER['HTTP_HOST'])) {
68
            throw new \RuntimeException('$_SERVER[\'HTTP_HOST\'] has not been set.');
69
        }
70
        if (!isset($_SERVER['REQUEST_URI'])) {
71
            throw new \RuntimeException('$_SERVER[\'REQUEST_URI\'] has not been set.');
72
        }
73
        $currentLocation = sprintf(
74
            '%s://%s%s',
75
            $this->getSchemeFromServer($_SERVER),
76
            $_SERVER['HTTP_HOST'],
77
            $_SERVER['REQUEST_URI']
78
        );
79
        return $this->createUri($currentLocation, $factory);
80
    }
81
82
    /**
83
     * @param array $server
84
     * @return string
85
     */
86
    private function getSchemeFromServer(array $server): string
87
    {
88
        if (!empty($server['REQUEST_SCHEME'])) {
89
            return $server['REQUEST_SCHEME'];
90
        }
91
92
        switch ($server['HTTPS'] ?? null) {
93
            case 'on':
94
            case '1':
95
                return 'https';
96
        }
97
        return 'http';
98
    }
99
100
    /**
101
     * @return UriFactory
102
     */
103
    public static function factory(): UriFactoryInterface
104
    {
105
        return new self;
106
    }
107
}
108