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

LoadRoleData::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/DataFixtures/ORM/LoadRoleData.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\Security\RolesService;
12
use App\Security\RolesServiceInterface;
13
use Doctrine\Common\DataFixtures\AbstractFixture;
14
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
/**
20
 * Class LoadRoleData
21
 *
22
 * @package App\DataFixtures\ORM
23
 * @author  TLe, Tarmo Leppänen <[email protected]>
24
 */
25
class LoadRoleData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
26
{
27
    /**
28
     * @var ContainerInterface
29
     */
30
    private $container;
31
32
    /**
33
     * @var ObjectManager
34
     */
35
    private $manager;
36
37
    /**
38
     * @var RolesServiceInterface
39
     */
40
    private $roles;
41
42
    /**
43
     * @param ContainerInterface|null $container
44
     */
45
    public function setContainer(ContainerInterface $container = null): void
46
    {
47
        $this->container = $container;
48
    }
49
50
    /**
51
     * Load data fixtures with the passed EntityManager
52
     *
53
     * @param ObjectManager $manager
54
     *
55
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
56
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
57
     */
58
    public function load(ObjectManager $manager): void
59
    {
60
        $this->manager = $manager;
61
        $this->roles = $this->container->get(RolesService::class);
62
63
        // Create entities
64
        \array_map([$this, 'createRole'], $this->roles->getRoles());
65
66
        // Flush database changes
67
        $this->manager->flush();
68
    }
69
70
    /**
71
     * Get the order of this fixture
72
     *
73
     * @return integer
74
     */
75
    public function getOrder(): int
76
    {
77
        return 1;
78
    }
79
80
    /**
81
     * Method to create, persist and flush Role entity to database.
82
     *
83
     * @param string $role
84
     */
85
    private function createRole(string $role): void
86
    {
87
        // Create new Role entity
88
        $entity = new Role($role);
89
90
        // Persist entity
91
        $this->manager->persist($entity);
92
93
        // Create reference for later usage
94
        $this->addReference('Role-' . $this->roles->getShort($role), $entity);
95
    }
96
}
97