AlmaUserProvider::supportsClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 1
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace App\Security\User;
4
5
use App\Service\AlmaApi;
6
use App\Service\AlmaUserData;
7
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
8
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
use Symfony\Component\Security\Core\User\UserProviderInterface;
11
use Psr\Log\LoggerInterface;
12
13
class AlmaUserProvider implements UserProviderInterface
14
{
15
    private $api;
16
    private $userData;
17
    private $logger;
18
19
    public function __construct(AlmaApi $api, AlmaUserData $userData, LoggerInterface $logger)
20
    {
21
        $this->api = $api;
22
        $this->userData = $userData;
23
        $this->logger = $logger;
24
    }
25
26
    public function loadUserByUsername($username)
27
    {
28
        try {
29
            $response = $this->api->getUserById($username);
30
            return new AlmaUser($username, array('ROLE_USER'), $this->userData->getFullNameAsString($response));
31
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
32
            $this->logger->error($e->getCode() . $e->getMessage());
33
        }
34
35
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
36
    }
37
38
    public function refreshUser(UserInterface $user)
39
    {
40
        if (!$user instanceof AlmaUser) {
41
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
42
        }
43
        return $user;
44
    }
45
46
    public function supportsClass($class)
47
    {
48
        return AlmaUser::class === $class;
49
    }
50
}
51