Completed
Pull Request — master (#4)
by ANTHONIUS
04:04
created

Group::setRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doyo\UserBundle\Model;
4
5
/**
6
 * Base group
7
 * @author Anthonius Munthi <[email protected]>
8
 */
9
abstract class Group implements GroupInterface
10
{
11
    /**
12
     * @var mixed
13
     */
14
    protected $id;
15
16
    /**
17
     * @var string
18
     */
19
    protected $name;
20
21
    /**
22
     * @var array
23
     */
24
    protected $roles;
25
26
    /**
27
     * Group constructor.
28
     *
29
     * @param string $name
30
     * @param array  $roles
31
     */
32 13
    public function __construct($name, $roles = array())
33
    {
34 13
        $this->name = $name;
35 13
        $this->roles = $roles;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 5
    public function addRole($role)
42
    {
43 5
        if (!$this->hasRole($role)) {
44 5
            $this->roles[] = strtoupper($role);
45
        }
46
47 5
        return $this;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 6
    public function getId()
54
    {
55 6
        return $this->id;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 6
    public function getName()
62
    {
63 6
        return $this->name;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 5
    public function hasRole($role)
70
    {
71 5
        return in_array(strtoupper($role), $this->roles, true);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 6
    public function getRoles()
78
    {
79 6
        return $this->roles;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    public function removeRole($role)
86
    {
87 2
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
88 2
            unset($this->roles[$key]);
89 2
            $this->roles = array_values($this->roles);
90
        }
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 5
    public function setName($name)
99
    {
100 5
        $this->name = $name;
101
102 5
        return $this;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function setRoles(array $roles)
109
    {
110
        $this->roles = $roles;
111
112
        return $this;
113
    }
114
}
115