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

AuthScope   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 13
c 1
b 0
f 1
dl 0
loc 82
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 3 1
A isClosed() 0 3 1
A getToken() 0 3 1
A getActor() 0 3 1
A getTransport() 0 3 1
A __construct() 0 3 1
A getAuthContext() 0 6 2
A close() 0 3 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 Psr\Container\ContainerInterface;
15
use Psr\Container\NotFoundExceptionInterface;
16
use Spiral\Core\Exception\ScopeException;
17
18
/**
19
 * Provides global access to temporary authentication scope.
20
 */
21
final class AuthScope implements AuthContextInterface
22
{
23
    /** @var ContainerInterface */
24
    private $container;
25
26
    /**
27
     * @param ContainerInterface $container
28
     */
29
    public function __construct(ContainerInterface $container)
30
    {
31
        $this->container = $container;
32
    }
33
34
    /**
35
     * @inheritDoc
36
     *
37
     * @throws ScopeException
38
     */
39
    public function start(TokenInterface $token, string $transport = null): void
40
    {
41
        $this->getAuthContext()->start($token, $transport);
42
    }
43
44
    /**
45
     * @inheritDoc
46
     *
47
     * @throws ScopeException
48
     */
49
    public function getToken(): ?TokenInterface
50
    {
51
        return $this->getAuthContext()->getToken();
52
    }
53
54
    /**
55
     * @inheritDoc
56
     *
57
     * @throws ScopeException
58
     */
59
    public function getTransport(): ?string
60
    {
61
        return $this->getAuthContext()->getTransport();
62
    }
63
64
    /**
65
     * @inheritDoc
66
     *
67
     * @throws ScopeException
68
     */
69
    public function getActor(): ?object
70
    {
71
        return $this->getAuthContext()->getActor();
72
    }
73
74
    /**
75
     * @inheritDoc
76
     *
77
     * @throws ScopeException
78
     */
79
    public function close(): void
80
    {
81
        $this->getAuthContext()->close();
82
    }
83
84
    /**
85
     * @inheritDoc
86
     *
87
     * @throws ScopeException
88
     */
89
    public function isClosed(): bool
90
    {
91
        return $this->getAuthContext()->isClosed();
92
    }
93
94
    /**
95
     * @return AuthContextInterface
96
     */
97
    private function getAuthContext(): AuthContextInterface
98
    {
99
        try {
100
            return $this->container->get(AuthContextInterface::class);
101
        } catch (NotFoundExceptionInterface $e) {
102
            throw new ScopeException('Unable to resolve auth context, invalid scope', $e->getCode(), $e);
103
        }
104
    }
105
}
106