UserFixtures::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\DataFixtures;
4
5
use App\Entity\User;
6
use Doctrine\Bundle\FixturesBundle\Fixture;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\Persistence\ObjectManager;
9
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
10
11
class UserFixtures extends Fixture
12
{
13
    public const USER_ADMIN_REFERENCE = 'user_admin';
14
    public const USER_HHV_REFERENCE = 'user_hhv';
15
    public const USER_EXPORTER_REFERENCE = 'user_exporter';
16
    public const USER_READONLY_REFERENCE = 'user_readonly';
17
18
    protected $em;
19
    protected $passwordEncoder;
20
21
    public function __construct(EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordEncoder)
22
    {
23
        $this->em = $entityManager;
24
        $this->passwordEncoder = $passwordEncoder;
25
    }
26
27
    public function load(ObjectManager $manager)
28
    {
29
        //Reset autoincrement
30
        $this->em->getConnection()
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::exec() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

30
        /** @scrutinizer ignore-deprecated */ $this->em->getConnection()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
31
            ->exec('ALTER TABLE `user` AUTO_INCREMENT = 1;');
32
        //ALTER TABLE does an implicit commit and PHP 8 throws if commit is called later internally without active transactions
33
        $this->em->getConnection()->beginTransaction();
34
35
        $user = new User();
36
        $user->setUsername('admin');
37
        //We use plaintext encoder so we can just set the PW here
38
        $user->setPassword($this->passwordEncoder->hashPassword($user, '1234'));
39
        $user->setFirstName('Admin');
40
        $user->setLastName('User');
41
        $user->setRoles(['ROLE_ADMIN',
42
            'ROLE_EDIT_USER',
43
            'ROLE_EDIT_ORGANISATIONS',
44
            'ROLE_SHOW_PAYMENT_ORDERS',
45
            'ROLE_EDIT_PAYMENT_ORDERS',
46
            'ROLE_PO_FACTUALLY',
47
            'ROLE_PO_MATHEMATICALLY',
48
            'ROLE_EDIT_BANK_ACCOUNTS',
49
            'ROLE_VIEW_AUDITS',
50
            'ROLE_MANUAL_CONFIRMATION',
51
        ]);
52
        $this->addReference(self::USER_ADMIN_REFERENCE, $user);
53
        $manager->persist($user);
54
55
        $user = new User();
56
        $user->setUsername('hhv');
57
        //We use plaintext encoder so we can just set the PW here
58
        $user->setPassword($this->passwordEncoder->hashPassword($user, '1234'));
59
        $user->setRoles(['ROLE_ADMIN',
60
            'ROLE_EDIT_ORGANISATIONS',
61
            'ROLE_SHOW_PAYMENT_ORDERS',
62
            'ROLE_EDIT_PAYMENT_ORDERS',
63
            'ROLE_PO_FACTUALLY',
64
            'ROLE_EDIT_BANK_ACCOUNTS',
65
            'ROLE_VIEW_AUDITS',
66
        ]);
67
        $this->addReference(self::USER_HHV_REFERENCE, $user);
68
        $manager->persist($user);
69
70
        $user = new User();
71
        $user->setUsername('exporter');
72
        //We use plaintext encoder so we can just set the PW here
73
        $user->setPassword($this->passwordEncoder->hashPassword($user, '1234'));
74
        $user->setRoles(['ROLE_ADMIN',
75
            'ROLE_SHOW_PAYMENT_ORDERS',
76
            'ROLE_EDIT_PAYMENT_ORDERS',
77
            'ROLE_PO_MATHEMATICALLY',
78
        ]);
79
        $this->addReference(self::USER_EXPORTER_REFERENCE, $user);
80
        $manager->persist($user);
81
82
        $user = new User();
83
        $user->setUsername('readonly');
84
        //We use plaintext encoder so we can just set the PW here
85
        $user->setPassword($this->passwordEncoder->hashPassword($user, '1234'));
86
        $user->setRoles(['ROLE_ADMIN',
87
            'ROLE_SHOW_PAYMENT_ORDERS',
88
        ]);
89
        $this->addReference(self::USER_READONLY_REFERENCE, $user);
90
        $manager->persist($user);
91
92
        $manager->flush();
93
    }
94
}
95