HierarchicalRole   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 1
dl 0
loc 123
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getId() 0 4 1
A setName() 0 4 1
A getName() 0 4 1
A addChild() 0 4 1
A addPermission() 0 4 1
A hasPermission() 0 7 1
A getChildren() 0 4 1
A hasChildren() 0 4 1
1
<?php
2
3
namespace JhUser\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Rbac\Role\HierarchicalRoleInterface;
8
use ZfcRbac\Permission\PermissionInterface;
9
10
/**
11
 * @ORM\Entity
12
 * @ORM\Table(name="role")
13
 */
14
class HierarchicalRole implements HierarchicalRoleInterface
15
{
16
    /**
17
     * @var int|null
18
     *
19
     * @ORM\Id
20
     * @ORM\Column(type="integer")
21
     * @ORM\GeneratedValue(strategy="AUTO")
22
     */
23
    protected $id;
24
25
    /**
26
     * @var string|null
27
     *
28
     * @ORM\Column(type="string", length=48, unique=true)
29
     */
30
    protected $name;
31
32
    /**
33
     * @var HierarchicalRoleInterface[]|\Doctrine\Common\Collections\Collection
34
     *
35
     * @ORM\ManyToMany(targetEntity="JhUser\Entity\HierarchicalRole")
36
     * @ORM\JoinTable(name="role_role",
37
     *      joinColumns={@ORM\JoinColumn(name="src_role_id", referencedColumnName="id")},
38
     *      inverseJoinColumns={@ORM\JoinColumn(name="target_role_id", referencedColumnName="id")}
39
     * )
40
     */
41
    protected $children = [];
42
43
    /**
44
     * @var PermissionInterface[]|\Doctrine\Common\Collections\Collection
45
     *
46
     * @ORM\ManyToMany(targetEntity="JhUser\Entity\Permission", indexBy="name", fetch="EAGER")
47
     * @ORM\JoinTable(name="role_permission",
48
     *      joinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")},
49
     *      inverseJoinColumns={@ORM\JoinColumn(name="permission_id", referencedColumnName="id")}
50
     * )
51
     */
52
    protected $permissions;
53
54
    /**
55
     * Init the Doctrine collection
56
     */
57
    public function __construct()
58
    {
59
        $this->children    = new ArrayCollection();
60
        $this->permissions = new ArrayCollection();
61
    }
62
63
    /**
64
     * Get the role identifier
65
     *
66
     * @return int
67
     */
68
    public function getId()
69
    {
70
        return $this->id;
71
    }
72
73
    /**
74
     * Set the role name
75
     *
76
     * @param  string $name
77
     * @return void
78
     */
79
    public function setName($name)
80
    {
81
        $this->name = (string) $name;
82
    }
83
84
    /**
85
     * Get the role name
86
     *
87
     * @return string
88
     */
89
    public function getName()
90
    {
91
        return $this->name;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function addChild(HierarchicalRoleInterface $child)
98
    {
99
        $this->children[] = $child;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function addPermission(Permission $permission)
106
    {
107
        $this->permissions[(string) $permission] = $permission;
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    public function hasPermission($permission)
114
    {
115
        // This can be a performance problem if your role has a lot of permissions. Please refer
116
        // to the cookbook to an elegant way to solve this issue
117
118
        return isset($this->permissions[(string) $permission]);
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function getChildren()
125
    {
126
        return $this->children;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function hasChildren()
133
    {
134
        return !$this->children->isEmpty();
135
    }
136
}
137