SchemeCollection::host()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Exception\SchemeException;
8
use Override;
9
10
use function array_key_exists;
11
12
final class SchemeCollection implements SchemeCollectionInterface
13
{
14
    private string $scheme = '';
15
    private string $appName = '';
16
17
    /** @var AdapterInterface[] */
18
    private array $collection = [];
19
20
    /**
21
     * {@inheritDoc}
22
     */
23
    #[Override]
24
    public function scheme(string $scheme): SchemeCollectionInterface
25
    {
26
        $this->scheme = $scheme;
27
28
        return $this;
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    #[Override]
35
    public function host(string $host): SchemeCollectionInterface
36
    {
37
        $this->appName = $host;
38
39
        return $this;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    #[Override]
46
    public function toAdapter(AdapterInterface $adapter): SchemeCollectionInterface
47
    {
48
        $this->collection[$this->scheme . '://' . $this->appName] = $adapter;
49
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     *
56
     * @throws SchemeException
57
     */
58
    #[Override]
59
    public function getAdapter(AbstractUri $uri): AdapterInterface
60
    {
61
        $schemeIndex = $uri->scheme . '://' . $uri->host;
62
        if (! array_key_exists($schemeIndex, $this->collection)) {
63
            if ($uri->scheme === 'http' || $uri->scheme === 'https') {
64
                return $this->collection['http://self'];
65
            }
66
67
            throw new SchemeException($uri->scheme . '://' . $uri->host);
68
        }
69
70
        return $this->collection[$schemeIndex];
71
    }
72
}
73