Passed
Push — master ( b95980...a81919 )
by Julito
08:46
created

CLpCategory::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 1
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\Mapping\Annotation as Gedmo;
17
use Symfony\Component\Validator\Constraints as Assert;
18
19
/**
20
 * CLpCategory.
21
 *
22
 * @ORM\Table(
23
 *     name="c_lp_category",
24
 * )
25
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
26
 */
27
class CLpCategory extends AbstractResource implements ResourceInterface
28
{
29
    /**
30
     * @ORM\Column(name="iid", type="integer")
31
     * @ORM\Id
32
     * @ORM\GeneratedValue
33
     */
34
    protected ?int $iid = null;
35
36
    /**
37
     * @Assert\NotBlank()
38
     * @ORM\Column(name="name", type="text")
39
     */
40
    protected string $name;
41
42
    /**
43
     * @Gedmo\SortablePosition
44
     * @ORM\Column(name="position", type="integer")
45
     */
46
    protected int $position;
47
48
    /**
49
     * @var Collection|CLpCategoryUser
50
     *
51
     * @ORM\OneToMany(
52
     *     targetEntity="Chamilo\CourseBundle\Entity\CLpCategoryUser",
53
     *     mappedBy="category",
54
     *     cascade={"persist", "remove"},
55
     *     orphanRemoval=true
56
     * )
57
     */
58
    protected $users;
59
60
    /**
61
     * @var Collection|CLp[]
62
     *
63
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CLp", mappedBy="category", cascade={"detach"})
64
     */
65
    protected $lps;
66
67
    public function __construct()
68
    {
69
        $this->users = new ArrayCollection();
70
        $this->lps = new ArrayCollection();
71
    }
72
73
    public function __toString(): string
74
    {
75
        return $this->getName();
76
    }
77
78
    public function getIid(): ?int
79
    {
80
        return $this->iid;
81
    }
82
83
    public function setName(string $name): self
84
    {
85
        $this->name = $name;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Get category name.
92
     */
93
    public function getName(): string
94
    {
95
        return $this->name;
96
    }
97
98
    public function setPosition(int $position): self
99
    {
100
        $this->position = $position;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    public function getPosition()
109
    {
110
        return $this->position;
111
    }
112
113
    /**
114
     * @return Collection|CLp[]
115
     */
116
    public function getLps()
117
    {
118
        return $this->lps;
119
    }
120
121
    /**
122
     * @return Collection
123
     */
124
    public function getUsers()
125
    {
126
        return $this->users;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->users also could return the type Chamilo\CourseBundle\Entity\CLpCategoryUser which is incompatible with the documented return type Doctrine\Common\Collections\Collection.
Loading history...
127
    }
128
129
    /**
130
     * @param Collection $users
131
     */
132
    public function setUsers($users): void
133
    {
134
        $this->users = new ArrayCollection();
135
        foreach ($users as $user) {
136
            $this->addUser($user);
137
        }
138
    }
139
140
    public function addUser(CLpCategoryUser $categoryUser): void
141
    {
142
        $categoryUser->setCategory($this);
143
144
        if (!$this->hasUser($categoryUser)) {
145
            $this->users->add($categoryUser);
0 ignored issues
show
Bug introduced by
The method add() does not exist on Chamilo\CourseBundle\Entity\CLpCategoryUser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
            $this->users->/** @scrutinizer ignore-call */ 
146
                          add($categoryUser);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
        }
147
    }
148
149
    /**
150
     * @return bool
151
     */
152
    public function hasUser(CLpCategoryUser $categoryUser)
153
    {
154
        if (0 !== $this->getUsers()->count()) {
155
            $criteria = Criteria::create()->where(
156
                Criteria::expr()->eq('user', $categoryUser->getUser())
157
            )->andWhere(
158
                Criteria::expr()->eq('category', $categoryUser->getCategory())
159
            );
160
161
            $relation = $this->getUsers()->matching($criteria);
162
163
            return $relation->count() > 0;
164
        }
165
166
        return false;
167
    }
168
169
    /**
170
     * @param User $user
171
     *
172
     * @return bool
173
     */
174
    public function hasUserAdded($user)
175
    {
176
        if (0 !== $this->getUsers()->count()) {
177
            $categoryUser = new CLpCategoryUser();
178
            $categoryUser->setCategory($this);
179
            $categoryUser->setUser($user);
180
181
            return $this->hasUser($categoryUser);
182
        }
183
184
        return false;
185
    }
186
187
    /**
188
     * @return $this
189
     */
190
    public function removeUsers(CLpCategoryUser $user)
191
    {
192
        $this->users->removeElement($user);
0 ignored issues
show
Bug introduced by
The method removeElement() does not exist on Chamilo\CourseBundle\Entity\CLpCategoryUser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

192
        $this->users->/** @scrutinizer ignore-call */ 
193
                      removeElement($user);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
193
194
        return $this;
195
    }
196
197
    /**
198
     * Resource identifier.
199
     */
200
    public function getResourceIdentifier(): int
201
    {
202
        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...
203
    }
204
205
    public function getResourceName(): string
206
    {
207
        return $this->getName();
208
    }
209
210
    public function setResourceName(string $name): self
211
    {
212
        return $this->setName($name);
213
    }
214
}
215