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

AuthBootloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
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\Bootloader\Auth;
13
14
use Spiral\Auth\ActorProviderInterface;
15
use Spiral\Auth\AuthScope;
16
use Spiral\Auth\Exception\AuthException;
17
use Spiral\Auth\TokenInterface;
18
use Spiral\Boot\Bootloader\Bootloader;
19
use Spiral\Core\Container\Autowire;
20
use Spiral\Core\Container\SingletonInterface;
21
use Spiral\Core\FactoryInterface;
22
23
/**
24
 * Manages the set of actor providers.
25
 */
26
final class AuthBootloader extends Bootloader implements ActorProviderInterface, SingletonInterface
27
{
28
    protected const SINGLETONS = [
29
        AuthScope::class              => AuthScope::class,
30
        ActorProviderInterface::class => self::class
31
    ];
32
33
    /** @var FactoryInterface */
34
    private $factory;
35
36
    /** @var ActorProviderInterface[]|string[] */
37
    private $actorProvider = [];
38
39
    /**
40
     * @param FactoryInterface $factory
41
     */
42
    public function __construct(FactoryInterface $factory)
43
    {
44
        $this->factory = $factory;
45
    }
46
47
    /**
48
     * Find actor by first matching actor provider.
49
     *
50
     * @param TokenInterface $token
51
     * @return object|null
52
     */
53
    public function getActor(TokenInterface $token): ?object
54
    {
55
        foreach ($this->getProviders() as $provider) {
56
            if (!$provider instanceof ActorProviderInterface) {
57
                throw new AuthException(sprintf(
58
                    'Expected `ActorProviderInterface`, got `%s`',
59
                    get_class($provider)
60
                ));
61
            }
62
63
            $actor = $provider->getActor($token);
64
            if ($actor !== null) {
65
                return $actor;
66
            }
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * Register new actor provider.
74
     *
75
     * @param ActorProviderInterface|Autowire|string $actorProvider
76
     */
77
    public function addActorProvider($actorProvider): void
78
    {
79
        $this->actorProvider[] = $actorProvider;
80
    }
81
82
    /**
83
     * @return \Generator|ActorProviderInterface[]
84
     */
85
    private function getProviders(): \Generator
86
    {
87
        foreach ($this->actorProvider as $provider) {
88
            if ($provider instanceof Autowire) {
89
                yield $provider->resolve($this->factory);
90
                continue;
91
            }
92
93
            if (is_object($provider)) {
94
                yield $provider;
95
                continue;
96
            }
97
98
            yield $this->factory->make($provider);
99
        }
100
    }
101
}
102