1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Security; |
4
|
|
|
|
5
|
|
|
use App\Repository\UserRepository; |
6
|
|
|
use App\Entity\User; |
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
|
|
|
|
12
|
|
|
class UserProvider implements UserProviderInterface |
13
|
|
|
{ |
14
|
|
|
protected $userRepository; |
15
|
|
|
|
16
|
|
|
public function __construct(UserRepository $userRepository) |
17
|
|
|
{ |
18
|
|
|
$this->userRepository = $userRepository; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function loadUserByUsername($username) |
25
|
|
|
{ |
26
|
|
|
$user = $this->findUser($username); |
27
|
|
|
|
28
|
|
|
if (!$user) { |
29
|
|
|
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $user; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function refreshUser(UserInterface $user) |
39
|
|
|
{ |
40
|
|
|
if (!$this->supportsClass(get_class($user))) { |
41
|
|
|
throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".', User::class, get_class($user))); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var $user User |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
if (null === $reloadedUser = $this->userRepository->findBy(['id' => $user->getId()])) { |
49
|
|
|
throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId())); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $reloadedUser; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function supportsClass($class) |
59
|
|
|
{ |
60
|
|
|
$userClass = User::class; |
61
|
|
|
return $userClass === $class || is_subclass_of($class, $userClass); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Finds a user by username. |
66
|
|
|
* |
67
|
|
|
* This method is meant to be an extension point for child classes. |
68
|
|
|
* |
69
|
|
|
* @param string $username |
70
|
|
|
* |
71
|
|
|
* @return User|null |
72
|
|
|
*/ |
73
|
|
|
protected function findUser($username) |
74
|
|
|
{ |
75
|
|
|
return $this->userRepository->findUserByUsername($username); |
76
|
|
|
} |
77
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.