Passed
Push — master ( 436c58...98654e )
by Anton
06:21 queued 03:52
created

AuthConfig::wire()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
rs 9.6111
cc 5
nc 4
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\Config;
13
14
use Spiral\Core\Container\Autowire;
15
use Spiral\Core\Exception\ConfigException;
16
use Spiral\Core\InjectableConfig;
17
18
/**
19
 * Manages auth http transport configuration.
20
 */
21
final class AuthConfig extends InjectableConfig
22
{
23
    public const CONFIG = 'auth';
24
25
    protected $config = [
26
        'defaultTransport' => '',
27
        'transports'       => []
28
    ];
29
30
    /**
31
     * @return string
32
     */
33
    public function getDefaultTransport(): string
34
    {
35
        return $this->config['defaultTransport'];
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function getTransports(): array
42
    {
43
        $transports = [];
44
        foreach ($this->config['transports'] as $name => $transport) {
45
            if (is_object($transport) && !$transport instanceof Autowire) {
46
                $transports[$name] = $transport;
47
                continue;
48
            }
49
50
            $transports[$name] = $this->wire($transport);
51
        }
52
53
        return $transports;
54
    }
55
56
    /**
57
     * @param mixed $item
58
     * @return Autowire
59
     *
60
     * @throws ConfigException
61
     */
62
    private function wire($item): Autowire
63
    {
64
        if ($item instanceof Autowire) {
65
            return $item;
66
        }
67
68
        if (is_string($item)) {
69
            return new Autowire($item);
70
        }
71
72
        if (is_array($item) && isset($item['class'])) {
73
            return new Autowire($item['class'], $item['options'] ?? []);
74
        }
75
76
        throw new ConfigException('Invalid class reference in auth config');
77
    }
78
}
79