Passed
Push — master ( e27912...81ef78 )
by David
18:33
created

MajimaUserProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A refreshUser() 0 9 2
A loadUserByUsername() 0 20 3
A supportsClass() 0 3 1
A __construct() 0 4 1
1
<?php
2
/**
3
 * Copyright (c) 2017
4
 *
5
 * @package   Majima
6
 * @author    David Neustadt <[email protected]>
7
 * @copyright 2017 David Neustadt
8
 * @license   MIT
9
 */
10
11
namespace Majima\Security;
12
13
use Symfony\Component\DependencyInjection\Container;
14
use Symfony\Component\Security\Core\User\UserProviderInterface;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
17
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
18
19
/**
20
 * Class MajimaUserProvider
21
 * @package Majima\Security
22
 */
23
class MajimaUserProvider implements UserProviderInterface
24
{
25
    /**
26
     * @var Container
27
     */
28
    private $container;
29
30
    /**
31
     * @var \FluentPDO
32
     */
33
    private $dbal;
34
35
    /**
36
     * MajimaUserProvider constructor.
37
     * @param Container $container
38
     * @param \FluentPDO $dbal
39
     */
40
    public function __construct(Container $container, \FluentPDO $dbal)
41
    {
42
        $this->container = $container;
43
        $this->dbal = $dbal;
44
    }
45
46
    /**
47
     * @param string $username
48
     * @return MajimaUser
49
     * @throws \Exception
50
     */
51
    public function loadUserByUsername($username)
52
    {
53
        $user = $this->dbal
54
            ->from('users')
55
            ->where('name', $username)
56
            ->fetch();
57
58
        if ($user) {
59
            $roles = $this->dbal
60
                ->from('users_roles')
61
                ->select(NULL)
62
                ->select('role')
63
                ->where('userID', $user['id'])
64
                ->fetchPairs('role', 'role');
65
66
            return new MajimaUser($username, $user['password'], $user['salt'], is_array($roles) ? $roles : ['ROLE_USER']);
67
        }
68
69
        throw new UsernameNotFoundException(
70
            sprintf('Username "%s" does not exist.', $username)
71
        );
72
    }
73
74
    /**
75
     * @param UserInterface $user
76
     * @return MajimaUser
77
     */
78
    public function refreshUser(UserInterface $user)
79
    {
80
        if (!$user instanceof MajimaUser) {
81
            throw new UnsupportedUserException(
82
                sprintf('Instances of "%s" are not supported.', get_class($user))
83
            );
84
        }
85
86
        return $this->loadUserByUsername($user->getUsername());
87
    }
88
89
    /**
90
     * @param string $class
91
     * @return bool
92
     */
93
    public function supportsClass($class)
94
    {
95
        return MajimaUser::class === $class;
96
    }
97
}