Group::removeRole()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace ProjetNormandie\UserBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
7
#[ORM\Table(name:'pnu_group')]
8
#[ORM\Entity]
9
class Group
10
{
11
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
12
    protected ?int $id = null;
13
14
    #[ORM\Column(length: 100, unique: true, nullable: false)]
15
    protected string $name = '';
16
17
    #[ORM\Column(type: 'array')]
18
    protected array $roles = [];
19
20
    /**
21
     * @return string
22
     */
23
    public function __toString()
24
    {
25
        return sprintf('%s [%d]', $this->getName(), $this->getId());
26
    }
27
28
    /**
29
     * @param       $name
30
     * @param array $roles
31
     */
32
    public function __construct($name, array $roles = [])
33
    {
34
        $this->name = $name;
35
        $this->roles = $roles;
36
    }
37
38
    /**
39
     * @return int|null
40
     */
41
    public function getId(): ?int
42
    {
43
        return $this->id;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getName(): string
50
    {
51
        return $this->name;
52
    }
53
54
    /**
55
     * @param $role
56
     * @return bool
57
     */
58
    public function hasRole($role): bool
59
    {
60
        return in_array(strtoupper($role), $this->roles, true);
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function getRoles(): array
67
    {
68
        return $this->roles;
69
    }
70
71
    /**
72
     * @param $role
73
     * @return $this
74
     */
75
    public function removeRole($role): self
76
    {
77
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
78
            unset($this->roles[$key]);
79
            $this->roles = array_values($this->roles);
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param $name
87
     * @return $this
88
     */
89
    public function setName($name): self
90
    {
91
        $this->name = $name;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param array $roles
98
     * @return $this
99
     */
100
    public function setRoles(array $roles): self
101
    {
102
        $this->roles = $roles;
103
104
        return $this;
105
    }
106
}
107