Passed
Push — master ( 401a66...0a87d5 )
by Julito
12:18
created

Usergroup::addUrlRelUsergroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
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 ApiPlatform\Core\Annotation\ApiResource;
10
use Doctrine\Common\Collections\ArrayCollection;
11
use Doctrine\Common\Collections\Collection;
12
use Doctrine\ORM\Mapping as ORM;
13
use Gedmo\Timestampable\Traits\TimestampableEntity;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
/**
17
 * Classes and social groups.
18
 *
19
 * @ApiResource(
20
 *     attributes={"security"="is_granted('ROLE_ADMIN')"},
21
 *     normalizationContext={"groups"={"usergroup:read"}}
22
 * )
23
 *
24
 * @ORM\Table(name="usergroup")
25
 * @ORM\Entity
26
 */
27
class Usergroup extends AbstractResource implements ResourceInterface, ResourceIllustrationInterface, ResourceWithAccessUrlInterface
28
{
29
    use TimestampableEntity;
30
31
    /**
32
     * @ORM\Column(name="id", type="integer", nullable=false)
33
     * @ORM\Id
34
     * @ORM\GeneratedValue
35
     */
36
    protected int $id;
37
38
    /**
39
     * @Assert\NotBlank()
40
     *
41
     * @ORM\Column(name="name", type="string", length=255)
42
     */
43
    protected string $name;
44
45
    /**
46
     * @ORM\Column(name="description", type="text", nullable=true)
47
     */
48
    protected ?string $description = null;
49
50
    /**
51
     * @ORM\Column(name="group_type", type="integer", nullable=false)
52
     */
53
    protected int $groupType;
54
55
    /**
56
     * @ORM\Column(name="picture", type="string", length=255, nullable=true)
57
     */
58
    protected ?string $picture = null;
59
60
    /**
61
     * @ORM\Column(name="url", type="string", length=255, nullable=true)
62
     */
63
    protected ?string $url = null;
64
65
    /**
66
     * @ORM\Column(name="visibility", type="string", length=255, nullable=false)
67
     */
68
    protected string $visibility;
69
70
    /**
71
     * @ORM\Column(name="author_id", type="integer", nullable=true)
72
     */
73
    protected ?string $authorId = null;
74
75
    /**
76
     * @ORM\Column(name="allow_members_leave_group", type="integer")
77
     */
78
    protected int $allowMembersToLeaveGroup;
79
80
    /**
81
     * @var Collection|UsergroupRelUser[]
82
     * @ORM\OneToMany(targetEntity="UsergroupRelUser", mappedBy="usergroup", cascade={"persist"})
83
     */
84
    protected Collection $users;
85
86
    /**
87
     * @var Collection|UsergroupRelCourse[]
88
     * @ORM\OneToMany(targetEntity="UsergroupRelCourse", mappedBy="usergroup", cascade={"persist"})
89
     */
90
    protected Collection $courses;
91
92
    /**
93
     * @var Collection|UsergroupRelSession[]
94
     * @ORM\OneToMany(targetEntity="UsergroupRelSession", mappedBy="usergroup", cascade={"persist"})
95
     */
96
    protected Collection $sessions;
97
98
    /**
99
     * @var Collection|UsergroupRelQuestion[]
100
     * @ORM\OneToMany(targetEntity="UsergroupRelQuestion", mappedBy="usergroup", cascade={"persist"})
101
     */
102
    protected Collection $questions;
103
104
    /**
105
     * @var AccessUrlRelUserGroup[]|Collection
106
     *
107
     * @ORM\OneToMany(
108
     *     targetEntity="Chamilo\CoreBundle\Entity\AccessUrlRelUserGroup",
109
     *     mappedBy="userGroup", cascade={"persist", "remove"}, orphanRemoval=true
110
     * )
111
     */
112
    protected Collection $urls;
113
114
    public function __construct()
115
    {
116
        $this->users = new ArrayCollection();
117
        $this->urls = new ArrayCollection();
118
        $this->courses = new ArrayCollection();
119
        $this->sessions = new ArrayCollection();
120
        $this->questions = new ArrayCollection();
121
    }
122
123
    public function __toString(): string
124
    {
125
        return $this->getName();
126
    }
127
128
    public function getUsers()
129
    {
130
        return $this->users;
131
    }
132
133
    public function getUrls()
134
    {
135
        return $this->urls;
136
    }
137
138
    public function addAccessUrl(AccessUrl $url): self
139
    {
140
        $urlRelUsergroup = new AccessUrlRelUserGroup();
141
        $urlRelUsergroup->setUserGroup($this);
142
        $urlRelUsergroup->setUrl($url);
143
        $this->addUrlRelUsergroup($urlRelUsergroup);
144
145
        return $this;
146
    }
147
148
    public function addUrlRelUsergroup(AccessUrlRelUserGroup $urlRelUsergroup): self
149
    {
150
        $urlRelUsergroup->setUserGroup($this);
151
        $this->urls[] = $urlRelUsergroup;
152
153
        return $this;
154
    }
155
156
    public function setUsers($users): void
157
    {
158
        $this->users = new ArrayCollection();
159
160
        foreach ($users as $user) {
161
            $this->addUsers($user);
162
        }
163
    }
164
165
    public function addUsers(UsergroupRelUser $user): void
166
    {
167
        $user->setUsergroup($this);
168
        $this->users[] = $user;
169
    }
170
171
    public function removeUsers(UsergroupRelUser $user): void
172
    {
173
        foreach ($this->users as $key => $value) {
174
            if ($value->getId() === $user->getId()) {
175
                unset($this->users[$key]);
176
            }
177
        }
178
    }
179
180
    /**
181
     * Get id.
182
     *
183
     * @return int
184
     */
185
    public function getId()
186
    {
187
        return $this->id;
188
    }
189
190
    public function setName(string $name): self
191
    {
192
        $this->name = $name;
193
194
        return $this;
195
    }
196
197
    public function getName(): string
198
    {
199
        return $this->name;
200
    }
201
202
    public function setDescription(string $description): self
203
    {
204
        $this->description = $description;
205
206
        return $this;
207
    }
208
209
    public function getDescription(): ?string
210
    {
211
        return $this->description;
212
    }
213
214
    /**
215
     * @return int
216
     */
217
    public function getGroupType()
218
    {
219
        return $this->groupType;
220
    }
221
222
    public function setGroupType(int $groupType): self
223
    {
224
        $this->groupType = $groupType;
225
226
        return $this;
227
    }
228
229
    public function getVisibility(): string
230
    {
231
        return $this->visibility;
232
    }
233
234
    public function setVisibility(string $visibility): self
235
    {
236
        $this->visibility = $visibility;
237
238
        return $this;
239
    }
240
241
    public function getUrl(): ?string
242
    {
243
        return $this->url;
244
    }
245
246
    public function setUrl(?string $url): self
247
    {
248
        $this->url = $url;
249
250
        return $this;
251
    }
252
253
    public function getAuthorId(): string
254
    {
255
        return $this->authorId;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->authorId could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
256
    }
257
258
    public function setAuthorId(string $authorId): self
259
    {
260
        $this->authorId = $authorId;
0 ignored issues
show
Documentation Bug introduced by
It seems like $authorId of type string is incompatible with the declared type integer|null of property $authorId.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
261
262
        return $this;
263
    }
264
265
    public function getAllowMembersToLeaveGroup(): int
266
    {
267
        return $this->allowMembersToLeaveGroup;
268
    }
269
270
    public function setAllowMembersToLeaveGroup(int $allowMembersToLeaveGroup): self
271
    {
272
        $this->allowMembersToLeaveGroup = $allowMembersToLeaveGroup;
273
274
        return $this;
275
    }
276
277
    /**
278
     * @return UsergroupRelCourse[]|Collection
279
     */
280
    public function getCourses()
281
    {
282
        return $this->courses;
283
    }
284
285
    public function setCourses(Collection $courses): self
286
    {
287
        $this->courses = $courses;
288
289
        return $this;
290
    }
291
292
    /**
293
     * @return UsergroupRelSession[]|Collection
294
     */
295
    public function getSessions()
296
    {
297
        return $this->sessions;
298
    }
299
300
    public function setSessions(Collection $sessions): self
301
    {
302
        $this->sessions = $sessions;
303
304
        return $this;
305
    }
306
307
    /**
308
     * @return UsergroupRelQuestion[]|Collection
309
     */
310
    public function getQuestions()
311
    {
312
        return $this->questions;
313
    }
314
315
    public function setQuestions(Collection $questions)
316
    {
317
        $this->questions = $questions;
318
319
        return $this;
320
    }
321
322
    public function getPicture(): ?string
323
    {
324
        return $this->picture;
325
    }
326
327
    public function getDefaultIllustration(int $size): string
328
    {
329
        $size = empty($size) ? 32 : $size;
330
331
        return sprintf('/img/icons/%s/group_na.png', $size);
332
    }
333
334
    public function getResourceIdentifier(): int
335
    {
336
        return $this->getId();
337
    }
338
339
    public function getResourceName(): string
340
    {
341
        return $this->getName();
342
    }
343
344
    public function setResourceName(string $name): self
345
    {
346
        return $this->setName($name);
347
    }
348
}
349