Passed
Push — master ( 5679ef...aad9e9 )
by Kirill
04:11
created

TransportRegistry::setDefaultTransport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Auth;
13
14
use Spiral\Auth\Exception\TransportException;
15
16
/**
17
 * Manages list of transports by their names, manages token storage association.
18
 */
19
final class TransportRegistry
20
{
21
    /** @var HttpTransportInterface[] */
22
    private $transports = [];
23
24
    /** @var string */
25
    private $default;
26
27
    /**
28
     * @param string $name
29
     */
30
    public function setDefaultTransport(string $name): void
31
    {
32
        $this->default = $name;
33
    }
34
35
    /**
36
     * @param string                 $name
37
     * @param HttpTransportInterface $transport
38
     */
39
    public function setTransport(string $name, HttpTransportInterface $transport): void
40
    {
41
        $this->transports[$name] = $transport;
42
    }
43
44
    /**
45
     * @param string|null $name
46
     * @return HttpTransportInterface
47
     */
48
    public function getTransport(string $name = null): HttpTransportInterface
49
    {
50
        $name = $name ?? $this->default;
51
52
        if (!isset($this->transports[$name])) {
53
            throw new TransportException("Undefined auth transport {$name}");
54
        }
55
56
        return $this->transports[$name];
57
    }
58
59
    /**
60
     * @return HttpTransportInterface[]
61
     */
62
    public function getTransports(): array
63
    {
64
        return $this->transports;
65
    }
66
}
67