UserFixtures::load()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 70
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 70
rs 9.216
c 0
b 0
f 0
cc 3
nc 3
nop 1

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\Common\Persistence\ObjectManager;
8
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
9
use Faker\Factory;
10
11
12
class UserFixtures extends Fixture
13
{
14
    /**
15
     * @var UserPasswordEncoderInterface
16
     */
17
    private $passwordEncoder;
18
19
    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
20
    {
21
        $this->passwordEncoder = $passwordEncoder;
22
    }
23
24
    public function load(ObjectManager $manager)
25
    {
26
        $faker = Factory::create();
27
28
        $user = new User();
29
        $user->setEmail('[email protected]')
30
            ->setUserName('admin')
31
            ->setPassword($this->passwordEncoder->encodePassword(
32
                $user,
33
                'admin'
34
            ))
35
            ->setRoles(['ROLE_ADMIN'])
36
            ->setImage($faker->image('public/uploads/user_images',400,300, null, false) )
37
            ->setVerified(true)
38
            ->setVerifiedHash(bin2hex(random_bytes(16)));
39
40
        // $product = new Product();
41
        $manager->persist($user);
42
43
        $user = new User();
44
        $user->setEmail('[email protected]')
45
            ->setUserName('user')
46
            ->setPassword($this->passwordEncoder->encodePassword(
47
                $user,
48
                'user'
49
            ))
50
            ->setImage($faker->image('public/uploads/user_images',400,300, null, false) )
51
            ->setVerified(true)
52
            ->setVerifiedHash(bin2hex(random_bytes(16)));
53
54
        // $product = new Product();
55
        $manager->persist($user);
56
57
        $user = new User();
58
        $user->setEmail('[email protected]')
59
            ->setUserName('usertest')
60
            ->setPassword($this->passwordEncoder->encodePassword(
61
                $user,
62
                'user'
63
            ))
64
            ->setVerified(false)
65
            ->setVerifiedHash(bin2hex(random_bytes(16)));
66
67
        $manager->persist($user);
68
69
70
71
        //Adding extra users
72
73
        for($i=0; $i<10; $i++){
74
            $user = new User();
75
            $user->setEmail('user'.$i.'@localhost.com')
76
                ->setUserName('user'.$i)
77
                ->setPassword($this->passwordEncoder->encodePassword(
78
                    $user,
79
                    'user'
80
                ))
81
82
                ->setVerified(true)
83
                ->setVerifiedHash(bin2hex(random_bytes(16)));
84
85
            //Not all users will have an image
86
            if(rand(0,2)>=1){
87
                $user->setImage($faker->image('public/uploads/user_images',400,300, null, false) );
88
            }
89
            // $product = new Product();
90
            $manager->persist($user);
91
        }
92
93
        $manager->flush();
94
95
    }
96
}
97