1 | <?php |
||
17 | class AuthenticatorService implements ServiceInterface |
||
18 | { |
||
19 | |||
20 | /** @var Controller */ |
||
21 | protected $controller; |
||
22 | |||
23 | /** @var DbService */ |
||
24 | protected $orm; |
||
25 | |||
26 | /** @var Config */ |
||
27 | protected $config; |
||
28 | |||
29 | /** @var string */ |
||
30 | protected $redirectAfterAuth; |
||
31 | |||
32 | /** |
||
33 | * Authenticator constructor. |
||
34 | * @param Controller $controller |
||
35 | * @param Config $config |
||
36 | */ |
||
37 | public function __construct(Controller $controller, Config $config) |
||
42 | |||
43 | /** |
||
44 | * @param UserEntity $user |
||
45 | * @return bool |
||
46 | * @codeCoverageIgnore |
||
47 | */ |
||
48 | public function loginUser(UserEntity $user) |
||
68 | |||
69 | /** |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function redirectToAuthentication() |
||
80 | |||
81 | /** |
||
82 | * @param string $uri |
||
83 | * @codeCoverageIgnore |
||
84 | */ |
||
85 | public function redirectAfterAuthentication(string $uri) |
||
89 | |||
90 | /** |
||
91 | * @param array $roles |
||
92 | * @return bool |
||
93 | */ |
||
94 | public function isAuthenticated(array $roles) |
||
113 | |||
114 | /** |
||
115 | * @param UserEntity $user |
||
116 | */ |
||
117 | public function saveUserInSession(UserEntity $user) |
||
121 | |||
122 | /** |
||
123 | * @return UserEntity |
||
124 | */ |
||
125 | public function getUserFromSession() |
||
133 | |||
134 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: