]> Raphaël G. Git Repositories - userbundle/blob - Fixture/UserFixture.php
Add user bundle templates
[userbundle] / Fixture / UserFixture.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys UserBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\UserBundle\Fixture;
13
14 use Doctrine\Bundle\FixturesBundle\Fixture;
15 use Doctrine\Persistence\ObjectManager;
16
17 use Rapsys\UserBundle\RapsysUserBundle;
18
19 use Symfony\Component\DependencyInjection\ContainerInterface;
20 use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
21
22 /**
23 * {@inheritdoc}
24 */
25 class UserFixture extends Fixture {
26 /**
27 * Config array
28 */
29 protected array $config;
30
31 /**
32 * Air fixtures constructor
33 */
34 public function __construct(protected ContainerInterface $container, protected UserPasswordHasherInterface $hasher) {
35 //Retrieve config
36 $this->config = $container->getParameter(RapsysUserBundle::getAlias());
37 }
38
39 /**
40 * {@inheritDoc}
41 */
42 public function load(ObjectManager $manager) {
43 //Civility tree
44 $civilityTree = [
45 'Mister',
46 'Madam',
47 'Miss'
48 ];
49
50 //Create titles
51 $civilitys = [];
52 foreach($civilityTree as $civilityData) {
53 $civility = new $this->config['class']['civility']($civilityData);
54 $manager->persist($civility);
55 $civilitys[$civilityData] = $civility;
56 unset($civility);
57 }
58
59 //Group tree
60 //XXX: ROLE_XXX is required by
61 $groupTree = [
62 'Guest',
63 'User',
64 'Admin'
65 ];
66
67 //Create groups
68 $groups = [];
69 foreach($groupTree as $groupData) {
70 $group = new $this->config['class']['group']($groupData);
71 $manager->persist($group);
72 $groups[$groupData] = $group;
73 unset($group);
74 }
75
76 //Flush to get the ids
77 $manager->flush();
78
79 //User tree
80 $userTree = [
81 [
82 'civility' => 'Mister',
83 'group' => 'Admin',
84 'mail' => 'admin@example.com',
85 'forename' => 'Forename',
86 'surname' => 'Surname',
87 'password' => 'test',
88 'active' => true
89 ]
90 ];
91
92 //Create users
93 $users = [];
94 foreach($userTree as $userData) {
95 $user = new $this->config['class']['user']($userData['mail'], $userData['password'], $civilitys[$userData['civility']], $userData['forename'], $userData['surname'], $userData['active']);
96 #TODO: check that password is hashed correctly !!!
97 $user->setPassword($this->hasher->hashPassword($user, $userData['password']));
98 $user->addGroup($groups[$userData['group']]);
99 $manager->persist($user);
100 $users[] = $user;
101 unset($user);
102 }
103
104 //Flush to get the ids
105 $manager->flush();
106 }
107 }