InMemoryApiUserProvider::getUsernameByApiKey()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the OsLabSecurityApiBundle package.
5
 *
6
 * (c) OsLab <https://github.com/OsLab>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace OsLab\SecurityApiBundle\Security\User;
13
14
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
15
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
16
use Symfony\Component\Security\Core\User\User;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
use Symfony\Component\Security\Core\User\UserProviderInterface;
19
20
/**
21
 * Provider in memory strategy.
22
 *
23
 * @author Michael COULLERET <[email protected]>
24
 * @author Florent DESPIERRES <[email protected]>
25
 */
26
class InMemoryApiUserProvider implements UserProviderInterface
27
{
28
    /**
29
     * @var array
30
     */
31
    private $users;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param array $users The list of given users in security
37
     */
38 30
    public function __construct(array $users = [])
39
    {
40 30
        $this->users = $users;
41 30
    }
42
43
    /**
44
     * Returns a username for a given api key.
45
     *
46
     * @param string $apiKey the api key
47
     *
48
     * @return string|null
49
     */
50 9
    public function getUsernameByApiKey($apiKey)
51
    {
52 9
        foreach ($this->users as $user) {
53 6
            if ($apiKey === $user->getPassword()) {
54 4
                return $user->getUsername();
55
            }
56
        }
57
58 6
        return null;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 6
    public function loadUserByUsername($username)
65
    {
66 6
        $user = $this->getUser($username);
67
68 3
        return new User($username, null, $user->getRoles());
69
    }
70
71
    /**
72
     * Adds a new User to the provider.
73
     *
74
     * @param UserInterface $user A UserInterface instance
75
     *
76
     * @throws \LogicException
77
     */
78
    public function createUser(UserInterface $user)
79 6
    {
80
        if (isset($this->users[strtolower($user->getUsername())])) {
81 6
            throw new \LogicException('Another user with the same username already exists.');
82 3
        }
83
84
        $this->users[strtolower($user->getUsername())] = $user;
85 3
    }
86 3
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function refreshUser(UserInterface $user)
91 3
    {
92
        throw new UnsupportedUserException();
93 3
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function supportsClass($class)
99 3
    {
100
        return User::class === $class;
101 3
    }
102
103
    /**
104
     * Returns the user by given username.
105
     *
106
     * @param string $username the username
107
     *
108
     * @return User
109
     */
110
    private function getUser($username)
111
    {
112 6
        if (!isset($this->users[strtolower($username)])) {
113
            $exception = new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
114 6
            $exception->setUsername($username);
115 3
116 3
            throw $exception;
117
        }
118 3
119
        return $this->users[strtolower($username)];
120
    }
121
}
122