Passed
Push — master ( b3f573...f22e85 )
by Yannick
06:55 queued 13s
created

Group::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use Doctrine\Common\Collections\ArrayCollection;
10
use Doctrine\Common\Collections\Collection;
11
use Doctrine\ORM\Mapping as ORM;
12
use Stringable;
13
use Symfony\Component\Validator\Constraints as Assert;
14
15
/**
16
 * User platform roles.
17
 */
18
#[ORM\Table(name: 'fos_group')]
19
#[ORM\Entity]
20
class Group implements Stringable
21
{
22
    #[ORM\Column(name: 'id', type: 'integer')]
23
    #[ORM\Id]
24
    #[ORM\GeneratedValue(strategy: 'AUTO')]
25
    protected ?int $id = null;
26
27
    #[Assert\NotBlank]
28
    #[ORM\Column(name: 'code', type: 'string', length: 40, nullable: false, unique: true)]
29
    protected string $code;
30
31
    /**
32
     * @var User[]|Collection
33
     */
34
    #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'groups')]
35
    protected Collection $users;
36
37
    public function __construct(
38
        #[Assert\NotBlank]
39
        #[ORM\Column(name: 'title', type: 'string', length: 255, unique: true, nullable: false)]
40
        protected string $title,
41
        #[ORM\Column(name: 'roles', type: 'array')]
42
        protected array $roles = [
43
        ]
44
    ) {
45
        $this->users = new ArrayCollection();
46
    }
47
48
    public function __toString(): string
49
    {
50
        return $this->getTitle() ?: '';
51
    }
52
53
    public function addRole(string $role): self
54
    {
55
        if (!$this->hasRole($role)) {
56
            $this->roles[] = strtoupper($role);
57
        }
58
59
        return $this;
60
    }
61
62
    public function hasRole(string $role): bool
63
    {
64
        return \in_array(strtoupper($role), $this->roles, true);
65
    }
66
67
    public function getRoles(): array
68
    {
69
        return $this->roles;
70
    }
71
72
    public function removeRole(string $role): self
73
    {
74
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
75
            unset($this->roles[$key]);
76
            $this->roles = array_values($this->roles);
77
        }
78
79
        return $this;
80
    }
81
82
    public function getTitle(): string
83
    {
84
        return $this->title;
85
    }
86
87
    public function setTitle(string $title): self
88
    {
89
        $this->title = $title;
90
91
        return $this;
92
    }
93
94
    public function setRoles(array $roles): self
95
    {
96
        $this->roles = $roles;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Get id.
103
     *
104
     * @return int
105
     */
106
    public function getId()
107
    {
108
        return $this->id;
109
    }
110
111
    /**
112
     * @return User[]|Collection
113
     */
114
    public function getUsers(): array|Collection
115
    {
116
        return $this->users;
117
    }
118
119
    public function getCode(): string
120
    {
121
        return $this->code;
122
    }
123
124
    public function setCode(string $code): self
125
    {
126
        $this->code = $code;
127
128
        return $this;
129
    }
130
}
131