Passed
Push — master ( a34e41...faa861 )
by Will
04:01
created

AlmaUserProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
18
    public function __construct(AlmaApi $api, AlmaUserData $userData, LoggerInterface $logger)
19
    {
20
        $this->api = $api;
21
        $this->userData = $userData;
22
        $this->logger = $logger;
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
    }
24
25
    public function loadUserByUsername($username)
26
    {
27
        try {
28
            $response = $this->api->findUserById($username);
29
            if ($this->userData->isValidUser($response)) {
30
                list($firstName, $lastName) = $this->userData->getFirstLastName($response);
31
32
                return new AlmaUser($username, array('ROLE_USER'), $firstName, $lastName);
33
            }
34
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
35
            $this->logger->error($e->getCode() . $e->getMessage());
36
        }
37
38
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
39
    }
40
41
    public function refreshUser(UserInterface $user)
42
    {
43
        if (!$user instanceof AlmaUser) {
44
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
45
        }
46
        return $user;
47
    }
48
49
    public function supportsClass($class)
50
    {
51
        return AlmaUser::class === $class;
52
    }
53
}
54