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

TokenStorageScope   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 8
c 1
b 0
f 0
dl 0
loc 46
ccs 10
cts 12
cp 0.8333
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenStorage() 0 6 2
A create() 0 3 1
A load() 0 3 1
A delete() 0 3 1
A __construct() 0 3 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