StorageUserProvider::loadUserByApiKey()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Security\ApiKey;
6
7
use Damax\Bundle\ApiAuthBundle\Key\Storage\KeyNotFound;
8
use Damax\Bundle\ApiAuthBundle\Key\Storage\Reader as Storage;
9
use Damax\Bundle\ApiAuthBundle\Security\ApiUser;
10
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
final class StorageUserProvider implements ApiKeyUserProvider
14
{
15
    private $storage;
16
17
    public function __construct(Storage $storage)
18
    {
19
        $this->storage = $storage;
20
    }
21
22
    public function supportsClass($class): bool
23
    {
24
        return ApiUser::class === $class;
25
    }
26
27
    public function loadUserByUsername($username): UserInterface
28
    {
29
        return new ApiUser($username);
30
    }
31
32
    public function loadUserByApiKey(string $apiKey): UserInterface
33
    {
34
        try {
35
            $key = $this->storage->get($apiKey);
36
        } catch (KeyNotFound $e) {
37
            throw new InvalidApiKey();
38
        }
39
40
        if ($key->expired()) {
41
            throw new InvalidApiKey();
42
        }
43
44
        return $this->loadUserByUsername($key->identity());
45
    }
46
47
    /**
48
     * @throws UnsupportedUserException
49
     */
50
    public function refreshUser(UserInterface $user): UserInterface
51
    {
52
        throw new UnsupportedUserException(sprintf('Provider "%s" must be configured as stateless.', __CLASS__));
53
    }
54
}
55