Passed
Pull Request — master (#5614)
by Angel Fernando Quiroz
08:27
created

AccessUrl::getLimitDiskSpace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Metadata\ApiResource;
10
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
11
use DateTime;
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Doctrine\Common\Collections\Collection;
14
use Doctrine\Common\Collections\Criteria;
15
use Doctrine\ORM\Mapping as ORM;
16
use Gedmo\Mapping\Annotation as Gedmo;
17
use Stringable;
18
use Symfony\Component\Serializer\Annotation\Groups;
19
use Symfony\Component\Validator\Constraints as Assert;
20
21
#[ApiResource(
22
    normalizationContext: [
23
        'groups' => ['access_url:read'],
24
        'swagger_definition_name' => 'Read',
25
    ],
26
    denormalizationContext: [
27
        'groups' => ['access_url:write', 'course_category:write'],
28
    ],
29
    security: "is_granted('ROLE_ADMIN')"
30
)]
31
#[ORM\Table(name: 'access_url')]
32
#[Gedmo\Tree(type: 'nested')]
33
#[ORM\Entity(repositoryClass: AccessUrlRepository::class)]
34
class AccessUrl extends AbstractResource implements ResourceInterface, Stringable
35
{
36
    public const DEFAULT_ACCESS_URL = 'http://localhost/';
37
    #[ORM\Column(name: 'id', type: 'integer')]
38
    #[ORM\Id]
39
    #[ORM\GeneratedValue]
40
    #[Groups(['access_url:read', 'access_url:write'])]
41
    protected ?int $id = null;
42
43
    /**
44
     * @var Collection<int, AccessUrlRelCourse>
45
     */
46
    #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelCourse::class, cascade: ['persist'], orphanRemoval: true)]
47
    protected Collection $courses;
48
49
    /**
50
     * @var Collection<int, AccessUrlRelSession>
51
     */
52
    #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelSession::class, cascade: ['persist'], orphanRemoval: true)]
53
    protected Collection $sessions;
54
55
    /**
56
     * @var Collection<int, AccessUrlRelUser>
57
     */
58
    #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelUser::class, cascade: ['persist'], orphanRemoval: true)]
59
    protected Collection $users;
60
61
    /**
62
     * @var Collection<int, SettingsCurrent>
63
     */
64
    #[ORM\OneToMany(mappedBy: 'url', targetEntity: SettingsCurrent::class, cascade: ['persist'], orphanRemoval: true)]
65
    protected Collection $settings;
66
67
    /**
68
     * @var Collection<int, SessionCategory>
69
     */
70
    #[ORM\OneToMany(mappedBy: 'url', targetEntity: SessionCategory::class, cascade: ['persist'], orphanRemoval: true)]
71
    protected Collection $sessionCategories;
72
73
    /**
74
     * @var Collection<int, AccessUrlRelCourseCategory>
75
     */
76
    #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelCourseCategory::class, cascade: ['persist'], orphanRemoval: true)]
77
    protected Collection $courseCategory;
78
79
    #[ORM\JoinColumn(onDelete: 'CASCADE')]
80
    #[Gedmo\TreeParent]
81
    #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
82
    protected ?AccessUrl $parent = null;
83
84
    /**
85
     * @var Collection<int, AccessUrl>
86
     */
87
    #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
88
    #[ORM\OrderBy(['id' => 'ASC'])]
89
    protected Collection $children;
90
91
    #[Gedmo\TreeLeft]
92
    #[ORM\Column(name: 'lft', type: 'integer')]
93
    protected int $lft;
94
95
    #[Gedmo\TreeLevel]
96
    #[ORM\Column(name: 'lvl', type: 'integer')]
97
    protected int $lvl;
98
99
    #[Gedmo\TreeRight]
100
    #[ORM\Column(name: 'rgt', type: 'integer')]
101
    protected int $rgt;
102
103
    #[Gedmo\TreeRoot]
104
    #[ORM\ManyToOne(targetEntity: self::class)]
105
    #[ORM\JoinColumn(name: 'tree_root', onDelete: 'CASCADE')]
106
    protected ?AccessUrl $root = null;
107
108
    #[Assert\NotBlank]
109
    #[Groups(['access_url:read', 'access_url:write'])]
110
    #[ORM\Column(name: 'url', type: 'string', length: 255)]
111
    protected string $url;
112
113
    #[ORM\Column(name: 'description', type: 'text')]
114
    protected ?string $description = null;
115
116
    #[ORM\Column(name: 'active', type: 'integer')]
117
    protected int $active;
118
119
    #[ORM\Column(name: 'created_by', type: 'integer')]
120
    protected int $createdBy;
121
122
    #[ORM\Column(name: 'tms', type: 'datetime', nullable: true)]
123
    protected ?DateTime $tms;
124
125
    #[ORM\Column(name: 'url_type', type: 'boolean', nullable: true)]
126
    protected ?bool $urlType = null;
127
128
    #[ORM\Column(name: 'limit_courses', type: 'integer', nullable: true)]
129
    protected ?int $limitCourses = null;
130
131
    #[ORM\Column(name: 'limit_active_courses', type: 'integer', nullable: true)]
132
    protected ?int $limitActiveCourses = null;
133
134
    #[ORM\Column(name: 'limit_sessions', type: 'integer', nullable: true)]
135
    protected ?int $limitSessions = null;
136
137
    #[ORM\Column(name: 'limit_users', type: 'integer', nullable: true)]
138
    protected ?int $limitUsers = null;
139
140
    #[ORM\Column(name: 'limit_teachers', type: 'integer', nullable: true)]
141
    protected ?int $limitTeachers = null;
142
143
    #[ORM\Column(name: 'limit_disk_space', type: 'integer', nullable: true)]
144
    protected ?int $limitDiskSpace = null;
145
146
    #[Assert\Email]
147
    #[ORM\Column(name: 'email', type: 'string', length: 255, nullable: true)]
148
    protected ?string $email = null;
149
150
    /**
151
     * @var Collection<int, AccessUrlRelColorTheme>
152
     */
153
    #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelColorTheme::class, cascade: ['persist'], orphanRemoval: true)]
154
    private Collection $colorThemes;
155
156
    public function __construct()
157
    {
158
        $this->description = '';
159
        $this->tms = new DateTime();
160
        $this->createdBy = 1;
161
        $this->courses = new ArrayCollection();
162
        $this->sessions = new ArrayCollection();
163
        $this->users = new ArrayCollection();
164
        $this->settings = new ArrayCollection();
165
        $this->sessionCategories = new ArrayCollection();
166
        $this->courseCategory = new ArrayCollection();
167
        $this->children = new ArrayCollection();
168
        $this->colorThemes = new ArrayCollection();
169
    }
170
171
    public function __toString(): string
172
    {
173
        return $this->getUrl();
174
    }
175
176
    public function getUrl(): string
177
    {
178
        return $this->url;
179
    }
180
181
    public function setUrl(string $url): self
182
    {
183
        $this->url = $url;
184
185
        return $this;
186
    }
187
188
    public function getDescription(): ?string
189
    {
190
        return $this->description;
191
    }
192
193
    public function setDescription(string $description): self
194
    {
195
        $this->description = $description;
196
197
        return $this;
198
    }
199
200
    public function getActive(): int
201
    {
202
        return $this->active;
203
    }
204
205
    public function setActive(int $active): self
206
    {
207
        $this->active = $active;
208
209
        return $this;
210
    }
211
212
    public function getCreatedBy(): int
213
    {
214
        return $this->createdBy;
215
    }
216
217
    public function setCreatedBy(int $createdBy): self
218
    {
219
        $this->createdBy = $createdBy;
220
221
        return $this;
222
    }
223
224
    public function getTms(): ?DateTime
225
    {
226
        return $this->tms;
227
    }
228
229
    public function setTms(DateTime $tms): self
230
    {
231
        $this->tms = $tms;
232
233
        return $this;
234
    }
235
236
    public function getUrlType(): ?bool
237
    {
238
        return $this->urlType;
239
    }
240
241
    public function setUrlType(bool $urlType): self
242
    {
243
        $this->urlType = $urlType;
244
245
        return $this;
246
    }
247
248
    public function getLimitActiveCourses(): ?int
249
    {
250
        return $this->limitActiveCourses;
251
    }
252
253
    public function setLimitActiveCourses(int $limitActiveCourses): self
254
    {
255
        $this->limitActiveCourses = $limitActiveCourses;
256
257
        return $this;
258
    }
259
260
    public function getLimitSessions(): ?int
261
    {
262
        return $this->limitSessions;
263
    }
264
265
    public function setLimitSessions(int $limitSessions): self
266
    {
267
        $this->limitSessions = $limitSessions;
268
269
        return $this;
270
    }
271
272
    public function getLimitUsers(): ?int
273
    {
274
        return $this->limitUsers;
275
    }
276
277
    public function setLimitUsers(int $limitUsers): self
278
    {
279
        $this->limitUsers = $limitUsers;
280
281
        return $this;
282
    }
283
284
    public function getLimitTeachers(): ?int
285
    {
286
        return $this->limitTeachers;
287
    }
288
289
    public function setLimitTeachers(int $limitTeachers): self
290
    {
291
        $this->limitTeachers = $limitTeachers;
292
293
        return $this;
294
    }
295
296
    public function getLimitDiskSpace(): ?int
297
    {
298
        return $this->limitDiskSpace;
299
    }
300
301
    public function setLimitDiskSpace(int $limitDiskSpace): self
302
    {
303
        $this->limitDiskSpace = $limitDiskSpace;
304
305
        return $this;
306
    }
307
308
    public function getEmail(): ?string
309
    {
310
        return $this->email;
311
    }
312
313
    public function setEmail(string $email): self
314
    {
315
        $this->email = $email;
316
317
        return $this;
318
    }
319
320
    /**
321
     * @return Collection<int, SettingsCurrent>
322
     */
323
    public function getSettings(): Collection
324
    {
325
        return $this->settings;
326
    }
327
328
    /**
329
     * @param Collection<int, SettingsCurrent> $settings
330
     */
331
    public function setSettings(Collection $settings): self
332
    {
333
        $this->settings = $settings;
334
335
        return $this;
336
    }
337
338
    public function getLimitCourses(): ?int
339
    {
340
        return $this->limitCourses;
341
    }
342
343
    public function setLimitCourses(int $limitCourses): self
344
    {
345
        $this->limitCourses = $limitCourses;
346
347
        return $this;
348
    }
349
350
    /**
351
     * @return Collection<int, AccessUrlRelCourse>
352
     */
353
    public function getCourses(): Collection
354
    {
355
        return $this->courses;
356
    }
357
358
    public function setCourses(Collection $courses): self
359
    {
360
        $this->courses = $courses;
361
362
        return $this;
363
    }
364
365
    public function addCourse(Course $course): self
366
    {
367
        if (!$this->hasCourse($course)) {
368
            $urlRelCourse = (new AccessUrlRelCourse())->setUrl($this);
369
            $course->addUrls($urlRelCourse);
370
371
            $this->courses->add($urlRelCourse);
372
        }
373
374
        return $this;
375
    }
376
377
    public function hasCourse(Course $course): bool
378
    {
379
        if ($this->courses->count() > 0) {
380
            $criteria = Criteria::create()->where(Criteria::expr()->eq('course', $course));
381
            $found = $this->courses->matching($criteria);
382
383
            return $found->count() > 0;
384
        }
385
386
        return false;
387
    }
388
389
    /**
390
     * @return Collection<int, SessionCategory>
391
     */
392
    public function getSessionCategories(): Collection
393
    {
394
        return $this->sessionCategories;
395
    }
396
397
    public function setSessionCategories(Collection $sessionCategories): self
398
    {
399
        $this->sessionCategories = $sessionCategories;
400
401
        return $this;
402
    }
403
404
    /**
405
     * @return Collection<int, AccessUrlRelSession>
406
     */
407
    public function getSessions(): Collection
408
    {
409
        return $this->sessions;
410
    }
411
412
    /**
413
     * @return Collection<int, AccessUrl>
414
     */
415
    public function getChildren(): Collection
416
    {
417
        return $this->children;
418
    }
419
420
    /**
421
     * @return Collection<int, AccessUrlRelUser>
422
     */
423
    public function getUsers(): Collection
424
    {
425
        return $this->users;
426
    }
427
428
    public function addUser(User $user): self
429
    {
430
        if (!$this->hasUser($user)) {
431
            $accessUrlRelUser = (new AccessUrlRelUser())->setUser($user)->setUrl($this);
432
            $this->users->add($accessUrlRelUser);
433
        }
434
435
        return $this;
436
    }
437
438
    public function hasUser(User $user): bool
439
    {
440
        if (0 !== $this->users->count()) {
441
            $criteria = Criteria::create()->where(Criteria::expr()->eq('user', $user));
442
            $relation = $this->users->matching($criteria);
443
444
            return $relation->count() > 0;
445
        }
446
447
        return false;
448
    }
449
450
    /**
451
     * @return Collection<int, AccessUrlRelCourseCategory>
452
     */
453
    public function getCourseCategory(): Collection
454
    {
455
        return $this->courseCategory;
456
    }
457
458
    public function getLft(): int
459
    {
460
        return $this->lft;
461
    }
462
463
    public function getLvl(): int
464
    {
465
        return $this->lvl;
466
    }
467
468
    public function getRgt(): int
469
    {
470
        return $this->rgt;
471
    }
472
473
    public function getRoot(): ?self
474
    {
475
        return $this->root;
476
    }
477
478
    public function getResourceIdentifier(): int
479
    {
480
        return $this->getId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getId() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
481
    }
482
483
    public function getId(): ?int
484
    {
485
        return $this->id;
486
    }
487
488
    public function getResourceName(): string
489
    {
490
        $url = $this->getUrl();
491
        $url = parse_url($url);
492
493
        return $url['host'];
494
    }
495
496
    public function setResourceName(string $name): self
497
    {
498
        return $this->setUrl($name);
499
    }
500
501
    public function getActiveColorTheme(): ?AccessUrlRelColorTheme
502
    {
503
        $criteria = Criteria::create();
504
        $criteria->where(
505
            Criteria::expr()->eq('active', true)
506
        );
507
508
        return $this->colorThemes->matching($criteria)->first() ?: null;
509
    }
510
511
    /**
512
     * @return Collection<int, AccessUrlRelColorTheme>
513
     */
514
    public function getColorThemes(): Collection
515
    {
516
        return $this->colorThemes;
517
    }
518
519
    public function addColorTheme(AccessUrlRelColorTheme $colorTheme): static
520
    {
521
        if (!$this->colorThemes->contains($colorTheme)) {
522
            $this->colorThemes->add($colorTheme);
523
            $colorTheme->setUrl($this);
524
        }
525
526
        return $this;
527
    }
528
529
    public function removeColorTheme(AccessUrlRelColorTheme $colorTheme): static
530
    {
531
        if ($this->colorThemes->removeElement($colorTheme)) {
532
            // set the owning side to null (unless already changed)
533
            if ($colorTheme->getUrl() === $this) {
534
                $colorTheme->setUrl(null);
535
            }
536
        }
537
538
        return $this;
539
    }
540
541
    public function getColorThemeByTheme(ColorTheme $theme): ?AccessUrlRelColorTheme
542
    {
543
        $criteria = Criteria::create();
544
        $criteria->where(
545
            Criteria::expr()->eq('colorTheme', $theme)
546
        );
547
548
        return $this->colorThemes->matching($criteria)->first() ?:  null;
549
    }
550
}
551