LoadRoleData::getOrder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of Webcook security bundle.
5
 *
6
 * See LICENSE file in the root of the bundle. Webcook 
7
 */
8
9
namespace Webcook\Cms\SecurityBundle\DataFixtures\ORM;
10
11
use Doctrine\Common\DataFixtures\FixtureInterface;
12
use Doctrine\Common\Persistence\ObjectManager;
13
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
14
use Webcook\Cms\SecurityBundle\Entity\Role;
15
use Webcook\Cms\SecurityBundle\Entity\RoleResource;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
/**
20
 * Role fixtures for tests.
21
 */
22
class LoadRoleData implements FixtureInterface, ContainerAwareInterface, OrderedFixtureInterface
23
{
24
    /**
25
     * System container.
26
     *
27
     * @var ContainerInterface
28
     */
29
    private $container;
30
31
    /**
32
     * Entity manager.
33
     *
34
     * @var ObjectManager
35
     */
36
    private $manager;
37
38
    /**
39
     * Set container.
40
     *
41
     * {@inheritDoc}
42
     * @param ContainerInterface $container [description]
43
     */
44 49
    public function setContainer(ContainerInterface $container = null)
45
    {
46 49
        $this->container = $container;
47 49
    }
48
49
    /**
50
     * Load fixtures into db.
51
     *
52
     * @param ObjectManager $manager
53
     *                               {@inheritDoc}
54
     */
55 49
    public function load(ObjectManager $manager)
56
    {
57 49
        $this->manager = $manager;
58
59 49
        $role = new Role();
60 49
        $role->setName('Administrator');
61 49
        $role->setRole('ROLE_ADMIN');
62
63 49
        $this->manager->persist($role);
64
65 49
        $this->addResources($role);
66
67 49
        $role = new Role();
68 49
        $role->setName('Editor');
69 49
        $role->setRole('ROLE_EDITOR');
70
71 49
        $this->manager->persist($role);
72
73 49
        $this->addResources($role, false);
74
75 49
        $this->manager->flush();
76 49
    }
77
78
    /**
79
     * Add resources into role object.
80
     *
81
     * @param Role    $role  [description]
82
     * @param boolean $admin [description]
83
     */
84 49
    private function addResources(Role $role, $admin = true)
85
    {
86 49
        $resources = $this->manager->getRepository('Webcook\Cms\SecurityBundle\Entity\Resource')->findAll();
87
88 49
        foreach ($resources as $resource) {
89 49
            $roleResource = new RoleResource();
90 49
            $roleResource->setRole($role);
91 49
            $roleResource->setResource($resource);
92 49
            $roleResource->setEdit($admin);
93 49
            $roleResource->setInsert($admin);
94 49
            $roleResource->setDelete($admin);
95
96 49
            $this->manager->persist($roleResource);
97
        }
98 49
    }
99
100
    /**
101
     * Get fixture order.
102
     *
103
     * {@inheritdoc}
104
     *
105
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
106
     */
107 49
    public function getOrder()
108
    {
109 49
        return 1;
110
    }
111
}
112