Passed
Push — master ( eb5904...054905 )
by Angel Fernando Quiroz
08:23
created

CLpCategory::getResourceIdentifier()   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\CourseBundle\Entity;
8
9
use Chamilo\CoreBundle\Entity\AbstractResource;
10
use Chamilo\CoreBundle\Entity\ResourceInterface;
11
use Chamilo\CoreBundle\Entity\User;
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\Sortable\Entity\Repository\SortableRepository;
17
use Stringable;
18
use Symfony\Component\Validator\Constraints as Assert;
19
20
/**
21
 * Learning paths categories.
22
 */
23
#[ORM\Table(name: 'c_lp_category')]
24
#[ORM\Entity(repositoryClass: SortableRepository::class)]
25
class CLpCategory extends AbstractResource implements ResourceInterface, Stringable
26
{
27
    #[ORM\Column(name: 'iid', type: 'integer')]
28
    #[ORM\Id]
29
    #[ORM\GeneratedValue]
30
    protected ?int $iid = null;
31
32
    #[Assert\NotBlank]
33
    #[ORM\Column(name: 'title', type: 'text')]
34
    protected string $title;
35
36
    /**
37
     * @var Collection<int, CLpCategoryRelUser>
38
     */
39
    #[ORM\OneToMany(mappedBy: 'category', targetEntity: CLpCategoryRelUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
40
    protected Collection $users;
41
42
    /**
43
     * @var Collection<int, CLp>
44
     */
45
    #[ORM\OneToMany(mappedBy: 'category', targetEntity: CLp::class, cascade: ['detach'])]
46
    protected Collection $lps;
47
48
    public function __construct()
49
    {
50
        $this->users = new ArrayCollection();
51
        $this->lps = new ArrayCollection();
52
    }
53
54
    public function __toString(): string
55
    {
56
        return $this->getTitle();
57
    }
58
59
    public function getIid(): ?int
60
    {
61
        return $this->iid;
62
    }
63
64
    public function setTitle(string $title): self
65
    {
66
        $this->title = $title;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Get category name.
73
     */
74
    public function getTitle(): string
75
    {
76
        return $this->title;
77
    }
78
79
    /**
80
     * @return Collection<int, CLp>
81
     */
82
    public function getLps(): Collection
83
    {
84
        return $this->lps;
85
    }
86
87
    /**
88
     * @return Collection<int, CLpCategoryRelUser>
89
     */
90
    public function getUsers(): Collection
91
    {
92
        return $this->users;
93
    }
94
95
    public function setUsers(Collection $users): void
96
    {
97
        $this->users = new ArrayCollection();
98
        foreach ($users as $user) {
99
            $this->addUser($user);
100
        }
101
    }
102
103
    public function addUser(CLpCategoryRelUser $categoryUser): void
104
    {
105
        $categoryUser->setCategory($this);
106
107
        if (!$this->hasUser($categoryUser)) {
108
            $this->users->add($categoryUser);
109
        }
110
    }
111
112
    public function hasUser(CLpCategoryRelUser $categoryUser): bool
113
    {
114
        if (0 !== $this->getUsers()->count()) {
115
            $criteria = Criteria::create()->where(
116
                Criteria::expr()->eq('user', $categoryUser->getUser())
117
            )->andWhere(
118
                Criteria::expr()->eq('category', $categoryUser->getCategory())
119
            );
120
121
            $relation = $this->getUsers()->matching($criteria);
122
123
            return $relation->count() > 0;
124
        }
125
126
        return false;
127
    }
128
129
    public function hasUserAdded(User $user): bool
130
    {
131
        if (0 !== $this->getUsers()->count()) {
132
            $categoryUser = new CLpCategoryRelUser();
133
            $categoryUser->setCategory($this);
134
            $categoryUser->setUser($user);
135
136
            return $this->hasUser($categoryUser);
137
        }
138
139
        return false;
140
    }
141
142
    public function removeUsers(CLpCategoryRelUser $user): self
143
    {
144
        $this->users->removeElement($user);
145
146
        return $this;
147
    }
148
149
    /**
150
     * Resource identifier.
151
     */
152
    public function getResourceIdentifier(): int
153
    {
154
        return $this->getIid();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getIid() 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...
155
    }
156
157
    public function getResourceName(): string
158
    {
159
        return $this->getTitle();
160
    }
161
162
    public function setResourceName(string $name): self
163
    {
164
        return $this->setTitle($name);
165
    }
166
}
167