LoadRoleData::load()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\TestRoleHierarchyBundle\DataFixtures\ORM;
13
14
use Doctrine\Common\DataFixtures\AbstractFixture;
15
use Doctrine\Common\DataFixtures\FixtureInterface;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
class LoadRoleData extends AbstractFixture implements FixtureInterface, ContainerAwareInterface
21
{
22
    /**
23
     * @var ContainerInterface
24
     */
25
    private $container;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function setContainer(ContainerInterface $container = null)
31
    {
32
        $this->container = $container;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function load(ObjectManager $manager)
39
    {
40
        $role_manager = $this->container->get('role_hierarchy.role_manager');
41
42
        foreach ($this->getRoles() as $role) {
43
            /*
44
             * @var \SpomkyLabs\TestRoleHierarchyBundle\Entity\Role
45
             */
46
            $entity = $role_manager->createRole();
47
            $parent = null === $role['parent'] ? null : $this->getReference('role-'.$role['parent']);
48
            $entity->setName($role['name']);
49
            $entity->setParent($parent);
50
51
            $role_manager->saveRole($entity);
52
            $this->addReference('role-'.$role['name'], $entity);
53
        }
54
    }
55
56
    protected function getRoles()
57
    {
58
        return [
59
            [
60
                'name'   => 'ROLE_SUPERADMIN',
61
                'parent' => null,
62
            ],
63
            [
64
                'name'   => 'ROLE_ADMIN',
65
                'parent' => 'ROLE_SUPERADMIN',
66
            ],
67
            [
68
                'name'   => 'ROLE_SUPERVISOR',
69
                'parent' => 'ROLE_ADMIN',
70
            ],
71
            [
72
                'name'   => 'ROLE_MANAGER',
73
                'parent' => 'ROLE_ADMIN',
74
            ],
75
            [
76
                'name'   => 'ROLE_OPERATOR',
77
                'parent' => 'ROLE_SUPERVISOR',
78
            ],
79
            [
80
                'name'   => 'ROLE_USER',
81
                'parent' => 'ROLE_MANAGER',
82
            ],
83
        ];
84
    }
85
}
86