Passed
Push — 1.11.x ( fce305...ff0bd4 )
by Julito
10:48
created

CLpCategory::setId()   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
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Entity;
5
6
use Chamilo\UserBundle\Entity\User;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Criteria;
9
use Doctrine\ORM\Mapping as ORM;
10
use Gedmo\Mapping\Annotation as Gedmo;
11
12
/**
13
 * CLpCategory.
14
 *
15
 * @ORM\Table(
16
 *  name="c_lp_category",
17
 *  indexes={
18
 *      @ORM\Index(name="course", columns={"c_id"})
19
 *  }
20
 * )
21
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
22
 */
23
class CLpCategory
24
{
25
    /**
26
     * @var int
27
     *
28
     * @ORM\Column(name="iid", type="integer")
29
     * @ORM\Id
30
     * @ORM\GeneratedValue
31
     */
32
    protected $iid;
33
34
    /**
35
     * @Gedmo\SortableGroup
36
     * @ORM\Column(name="c_id", type="integer")
37
     */
38
    protected $cId;
39
40
    /**
41
     * @var string
42
     *
43
     * @ORM\Column(name="name")
44
     */
45
    protected $name;
46
47
    /**
48
     * @Gedmo\SortablePosition
49
     * @ORM\Column(name="position", type="integer")
50
     */
51
    protected $position;
52
53
    /**
54
     * @var CLpCategoryUser[]
55
     *
56
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CLpCategoryUser", mappedBy="category", cascade={"persist", "remove"}, orphanRemoval=true)
57
     */
58
    protected $users;
59
60
    /**
61
     * @var int
62
     */
63
    protected $sessionId;
64
65
    /**
66
     * CLpCategory constructor.
67
     */
68
    public function __construct()
69
    {
70
        $this->users = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type Chamilo\CourseBundle\Entity\CLpCategoryUser[] of property $users.

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...
71
        $this->sessionId = 0;
72
    }
73
74
    /**
75
     * Set cId.
76
     *
77
     * @param int $cId
78
     *
79
     * @return CLpCategory
80
     */
81
    public function setCId($cId)
82
    {
83
        $this->cId = $cId;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Get cId.
90
     *
91
     * @return int
92
     */
93
    public function getCId()
94
    {
95
        return $this->cId;
96
    }
97
98
    /**
99
     * Set id.
100
     *
101
     * @param int $id
102
     *
103
     * @return CLpCategory
104
     */
105
    public function setId($id)
106
    {
107
        $this->iid = $id;
108
109
        return $this;
110
    }
111
112
    /**
113
     * Get id.
114
     *
115
     * @return int
116
     */
117
    public function getId()
118
    {
119
        return $this->iid;
120
    }
121
122
    /**
123
     * @param $name
124
     *
125
     * @return $this
126
     */
127
    public function setName($name)
128
    {
129
        $this->name = $name;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Get category name.
136
     *
137
     * @return string
138
     */
139
    public function getName()
140
    {
141
        return $this->name;
142
    }
143
144
    /**
145
     * @param $position
146
     *
147
     * @return $this
148
     */
149
    public function setPosition($position)
150
    {
151
        $this->position = $position;
152
153
        return $this;
154
    }
155
156
    /**
157
     * @return int
158
     */
159
    public function getPosition()
160
    {
161
        return $this->position;
162
    }
163
164
    /**
165
     * @return int
166
     */
167
    public function getSessionId()
168
    {
169
        return $this->sessionId;
170
    }
171
172
    /**
173
     * @param int $sessionId
174
     *
175
     * @return CLpCategory
176
     */
177
    public function setSessionId($sessionId)
178
    {
179
        $this->sessionId = $sessionId;
180
181
        return $this;
182
    }
183
184
    /**
185
     * @return ArrayCollection|CLpCategoryUser[]
186
     */
187
    public function getUsers()
188
    {
189
        return $this->users;
190
    }
191
192
    /**
193
     * @param $users
194
     */
195
    public function setUsers($users)
196
    {
197
        $this->users = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type Chamilo\CourseBundle\Entity\CLpCategoryUser[] of property $users.

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...
198
199
        foreach ($users as $user) {
200
            $this->addUser($user);
201
        }
202
    }
203
204
    public function addUser(CLpCategoryUser $categoryUser)
205
    {
206
        $categoryUser->setCategory($this);
207
208
        if (!$this->hasUser($categoryUser)) {
209
            $this->users->add($categoryUser);
210
        }
211
    }
212
213
    /**
214
     * @return bool
215
     */
216
    public function hasUser(CLpCategoryUser $categoryUser)
217
    {
218
        if ($this->getUsers()->count()) {
219
            $criteria = Criteria::create()->where(
220
                Criteria::expr()->eq("user", $categoryUser->getUser())
221
            )->andWhere(
222
                Criteria::expr()->eq("category", $categoryUser->getCategory())
223
            );
224
225
            $relation = $this->getUsers()->matching($criteria);
226
227
            return $relation->count() > 0;
228
        }
229
230
        return false;
231
    }
232
233
    /**
234
     * @param User $user
235
     *
236
     * @return bool
237
     */
238
    public function hasUserAdded($user)
239
    {
240
        if ($this->getUsers()->count()) {
241
            $categoryUser = new CLpCategoryUser();
242
            $categoryUser->setCategory($this);
243
            $categoryUser->setUser($user);
244
245
            return $this->hasUser($categoryUser);
246
        }
247
248
        return false;
249
    }
250
251
    /**
252
     * @return $this
253
     */
254
    public function removeUsers(CLpCategoryUser $user)
255
    {
256
        $this->users->removeElement($user);
257
258
        return $this;
259
    }
260
}
261