1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\DataFixtures; |
4
|
|
|
|
5
|
|
|
use App\Entity\ConfirmationToken; |
6
|
|
|
use App\Entity\User; |
7
|
|
|
use App\Entity\UserProfile; |
8
|
|
|
use App\Entity\UserProfileContacts; |
9
|
|
|
use Doctrine\Bundle\FixturesBundle\Fixture; |
10
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
11
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
12
|
|
|
|
13
|
|
|
class UsersFixtures extends Fixture |
14
|
|
|
{ |
15
|
|
|
const TESTER_EMAIL = '[email protected]'; |
16
|
|
|
const TESTER_USERNAME = 'tester_fixture'; |
17
|
|
|
const TESTER_PASSWORD = '123456'; |
18
|
|
|
const TESTER_API_TOKEN = 'tester_api_token'; |
19
|
|
|
const TESTER_EMAIL_CONFIRMATION_TOKEN = '11kYJ3ut7aOISPQN0RSqceYDasNnb690'; |
20
|
|
|
|
21
|
|
|
const ADMIN_EMAIL = '[email protected]'; |
22
|
|
|
const ADMIN_USERNAME = 'admin_fixture'; |
23
|
|
|
const ADMIN_PASSWORD = '12345678'; |
24
|
|
|
const ADMIN_API_TOKEN = 'admin_api_token'; |
25
|
|
|
|
26
|
|
|
public function load(ObjectManager $manager): void |
27
|
|
|
{ |
28
|
|
|
$user = $this->createUser(); |
29
|
|
|
$admin = $this->createAdmin(); |
30
|
|
|
|
31
|
|
|
$manager->persist($user); |
32
|
|
|
$manager->persist($admin); |
33
|
|
|
$manager->flush(); |
34
|
|
|
|
35
|
|
|
// Tester |
36
|
|
|
$this->createTestApiToken($user, self::TESTER_API_TOKEN, $manager); |
37
|
|
|
$this->createEmailConfirmationToken($user, self::TESTER_EMAIL_CONFIRMATION_TOKEN, $manager); |
38
|
|
|
// Admin |
39
|
|
|
$this->createTestApiToken($admin, self::ADMIN_API_TOKEN, $manager); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function createUser() |
43
|
|
|
{ |
44
|
|
|
$user = new User(); |
45
|
|
|
$user->username = self::TESTER_USERNAME; |
46
|
|
|
$user->email = self::TESTER_EMAIL; |
47
|
|
|
$user->setPlainPassword(self::TESTER_PASSWORD); |
48
|
|
|
|
49
|
|
|
$profile = $user->getProfile(); |
50
|
|
|
$profile->first_name = 'First'; |
51
|
|
|
$profile->last_name = 'Last'; |
52
|
|
|
|
53
|
|
|
for ($i = 3; $i--> 0;) { |
54
|
|
|
$profile->addContacts("TestProvider #{$i}", "https://test.com/{$i}/info"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $user; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function createAdmin() |
61
|
|
|
{ |
62
|
|
|
$user = new User(); |
63
|
|
|
$user->username = self::ADMIN_USERNAME; |
64
|
|
|
$user->email = self::ADMIN_PASSWORD; |
65
|
|
|
$user->setPlainPassword(self::ADMIN_PASSWORD); |
66
|
|
|
$user->addRole(User::ROLE_ADMIN); |
67
|
|
|
|
68
|
|
|
$profile = $user->getProfile(); |
69
|
|
|
$profile->first_name = 'Admin'; |
70
|
|
|
$profile->last_name = 'Admin'; |
71
|
|
|
|
72
|
|
|
for ($i = 3; $i--> 0;) { |
73
|
|
|
$profile->addContacts("TestProvider #{$i}", "https://test.com/{$i}/info"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $user; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function createTestApiToken(User $user, string $token, ObjectManager $manager): void |
80
|
|
|
{ |
81
|
|
|
$manager->getConnection()->exec("INSERT INTO users_api_tokens (id, user_id, token) VALUES (NEXTVAL('users_api_tokens_id_seq'), {$user->getId()}, '{$token}');"); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function createEmailConfirmationToken(User $user, string $token, ObjectManager $manager): void |
85
|
|
|
{ |
86
|
|
|
$type = ConfirmationToken::TYPE_CONFIRM_EMAIl; |
87
|
|
|
$manager->getConnection()->exec("INSERT INTO users_confirmation_tokens (id, user_id, token, type) VALUES (NEXTVAL('users_confirmation_tokens_id_seq'), {$user->getId()}, '{$token}', '{$type}');"); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
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: