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

LoadUserGroupData::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/DataFixtures/ORM/LoadUserGroupData.php
5
 *
6
 * @author  TLe, Tarmo Leppänen <[email protected]>
7
 */
8
namespace App\DataFixtures\ORM;
9
10
use App\Entity\Role;
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 LoadUserGroupData
22
 *
23
 * @package App\DataFixtures\ORM
24
 * @author  TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class LoadUserGroupData 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, 'createUserGroup'], $this->roles->getRoles());
66
67
        // Flush database changes
68
        $this->manager->flush();
69
    }
70
71
    /**
72
     * Get the order of this fixture
73
     *
74
     * @return integer
75
     */
76
    public function getOrder(): int
77
    {
78
        return 2;
79
    }
80
81
    /**
82
     * @param string $role
83
     */
84
    private function createUserGroup(string $role): void
85
    {
86
        /** @var Role $roleReference */
87
        $roleReference = $this->getReference('Role-' . $this->roles->getShort($role));
88
89
        // Create new entity
90
        $entity = new UserGroup();
91
        $entity->setRole($roleReference);
92
        $entity->setName($this->roles->getRoleLabel($role));
93
94
        // Persist entity
95
        $this->manager->persist($entity);
96
97
        // Create reference for later usage
98
        $this->addReference('UserGroup-' . $this->roles->getShort($role), $entity);
99
    }
100
}
101