Passed
Push — master ( b4ac3b...3cca7b )
by Tarmo
02:47
created

LoadUserData::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/DataFixtures/ORM/LoadUserData.php
5
 *
6
 * @author  TLe, Tarmo Leppänen <[email protected]>
7
 */
8
namespace App\DataFixtures\ORM;
9
10
use App\Entity\User;
11
use App\Entity\UserGroup;
12
use App\Security\RolesService;
13
use App\Security\RolesServiceInterface;
14
use Doctrine\Common\DataFixtures\AbstractFixture;
15
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
/**
21
 * Class LoadUserData
22
 *
23
 * @package App\DataFixtures\ORM
24
 * @author  TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
27
{
28
    /**
29
     * @var ContainerInterface
30
     */
31
    private $container;
32
33
    /**
34
     * @var ObjectManager
35
     */
36
    private $manager;
37
38
    /**
39
     * @var RolesServiceInterface
40
     */
41
    private $roles;
42
43
    /**
44
     * @param ContainerInterface|null $container
45
     */
46
    public function setContainer(ContainerInterface $container = null): void
47
    {
48
        $this->container = $container;
49
    }
50
51
    /**
52
     * Load data fixtures with the passed EntityManager
53
     *
54
     * @param ObjectManager $manager
55
     *
56
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
57
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
58
     */
59
    public function load(ObjectManager $manager): void
60
    {
61
        $this->manager = $manager;
62
        $this->roles = $this->container->get(RolesService::class);
63
64
        // Create entities
65
        \array_map([$this, 'createUser'], $this->roles->getRoles());
66
67
        $this->createUser();
68
69
        // Flush database changes
70
        $this->manager->flush();
71
    }
72
73
    /**
74
     * Get the order of this fixture
75
     *
76
     * @return integer
77
     */
78
    public function getOrder(): int
79
    {
80
        return 3;
81
    }
82
83
    /**
84
     * @param string $role
85
     */
86
    private function createUser(string $role = null): void
87
    {
88
        $suffix = $role === null ? '' : '-' . $this->roles->getShort($role);
89
90
        // Create new entity
91
        $entity = new User();
92
        $entity->setUsername('john' . $suffix);
93
        $entity->setFirstname('John');
94
        $entity->setSurname('Doe');
95
        $entity->setEmail('john.doe' . $suffix . '@test.com');
96
        $entity->setPlainPassword('password' . $suffix);
97
98
        if ($role !== null) {
99
            /** @var UserGroup $userGroup */
100
            $userGroup = $this->getReference('UserGroup-' . $this->roles->getShort($role));
101
102
            $entity->addUserGroup($userGroup);
103
        }
104
105
        // Persist entity
106
        $this->manager->persist($entity);
107
108
        // Create reference for later usage
109
        $this->addReference('User-' . $entity->getUsername(), $entity);
110
    }
111
}
112