Passed
Push — master ( d2a547...ae8014 )
by butschster
10:20
created

TokenStorageScope::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Auth;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Container\NotFoundExceptionInterface;
9
use Spiral\Auth\Exception\TokenStorageException;
10
use Spiral\Core\Container\SingletonInterface;
11
use Spiral\Core\Exception\ScopeException;
12
13
final class TokenStorageScope implements TokenStorageInterface, SingletonInterface
14
{
15 4
    public function __construct(
16
        private readonly ContainerInterface $container
17
    ) {
18 4
    }
19
20
    /**
21
     * Load token by id, must return null if token not found.
22
     *
23
     * @throws TokenStorageException
24
     */
25 1
    public function load(string $id): ?TokenInterface
26
    {
27 1
        return $this->getTokenStorage()->load($id);
28
    }
29
30
    /**
31
     * Create token based on the payload provided by actor provider.
32
     *
33
     * @throws TokenStorageException
34
     */
35 1
    public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface
36
    {
37 1
        return $this->getTokenStorage()->create($payload, $expiresAt);
38
    }
39
40
    /**
41
     * Delete token from the persistent storage.
42
     *
43
     * @throws TokenStorageException
44
     */
45 1
    public function delete(TokenInterface $token): void
46
    {
47 1
        $this->getTokenStorage()->delete($token);
48
    }
49
50
    /**
51
     * @throws ScopeException
52
     */
53 4
    private function getTokenStorage(): TokenStorageInterface
54
    {
55
        try {
56 4
            return $this->container->get(TokenStorageInterface::class);
57
        } catch (NotFoundExceptionInterface $e) {
58
            throw new ScopeException('Unable to resolve token storage, invalid scope', $e->getCode(), $e);
59
        }
60
    }
61
}
62