Passed
Push — master ( df9557...e5317d )
by Jan
04:48 queued 11s
created

UserFixtures::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 45
nc 1
nop 1
dl 0
loc 61
rs 9.2
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Security\Core\Encoder\UserPasswordEncoderInterface;
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, UserPasswordEncoderInterface $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()
31
            ->exec('ALTER TABLE `user` AUTO_INCREMENT = 1;');
32
33
        $user = new User();
34
        $user->setUsername('admin');
35
        //We use plaintext encoder so we can just set the PW here
36
        $user->setPassword($this->passwordEncoder->encodePassword($user, '1234'));
37
        $user->setFirstName('Admin');
38
        $user->setLastName('User');
39
        $user->setRoles(['ROLE_ADMIN',
40
            'ROLE_EDIT_USER',
41
            'ROLE_EDIT_ORGANISATIONS',
42
            'ROLE_SHOW_PAYMENT_ORDERS',
43
            'ROLE_EDIT_PAYMENT_ORDERS',
44
            'ROLE_PO_FACTUALLY',
45
            'ROLE_PO_MATHEMATICALLY',
46
            'ROLE_EDIT_BANK_ACCOUNTS',
47
        ]);
48
        $this->addReference(self::USER_ADMIN_REFERENCE, $user);
49
        $manager->persist($user);
50
51
        $user = new User();
52
        $user->setUsername('hhv');
53
        //We use plaintext encoder so we can just set the PW here
54
        $user->setPassword($this->passwordEncoder->encodePassword($user, '1234'));
55
        $user->setRoles(['ROLE_ADMIN',
56
            'ROLE_EDIT_ORGANISATIONS',
57
            'ROLE_SHOW_PAYMENT_ORDERS',
58
            'ROLE_EDIT_PAYMENT_ORDERS',
59
            'ROLE_PO_FACTUALLY',
60
            'ROLE_EDIT_BANK_ACCOUNTS',
61
        ]);
62
        $this->addReference(self::USER_HHV_REFERENCE, $user);
63
        $manager->persist($user);
64
65
        $user = new User();
66
        $user->setUsername('exporter');
67
        //We use plaintext encoder so we can just set the PW here
68
        $user->setPassword($this->passwordEncoder->encodePassword($user, '1234'));
69
        $user->setRoles(['ROLE_ADMIN',
70
            'ROLE_SHOW_PAYMENT_ORDERS',
71
            'ROLE_EDIT_PAYMENT_ORDERS',
72
            'ROLE_PO_MATHEMATICALLY',
73
        ]);
74
        $this->addReference(self::USER_EXPORTER_REFERENCE, $user);
75
        $manager->persist($user);
76
77
        $user = new User();
78
        $user->setUsername('readonly');
79
        //We use plaintext encoder so we can just set the PW here
80
        $user->setPassword($this->passwordEncoder->encodePassword($user, '1234'));
81
        $user->setRoles(['ROLE_ADMIN',
82
            'ROLE_SHOW_PAYMENT_ORDERS',
83
        ]);
84
        $this->addReference(self::USER_READONLY_REFERENCE, $user);
85
        $manager->persist($user);
86
87
        $manager->flush();
88
    }
89
}
90