Passed
Push — master ( 62083f...ad8d4a )
by Julito
10:31
created

CourseCategory::addUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
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\ApiFilter;
10
use ApiPlatform\Core\Annotation\ApiResource;
11
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
12
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
13
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Doctrine\Common\Collections\Criteria;
17
use Doctrine\ORM\Mapping as ORM;
18
use Symfony\Component\Serializer\Annotation\Groups;
19
use Symfony\Component\Validator\Constraints as Assert;
20
21
/**
22
 * @ApiResource(
23
 *     attributes={"security"="is_granted('ROLE_ADMIN')"},
24
 *     normalizationContext={"groups"={"course_category:read", "course:read"}, "swagger_definition_name"="Read"},
25
 *     denormalizationContext={"groups"={"course_category:write", "course:write"}},
26
 * )
27
 * @ApiFilter(SearchFilter::class, properties={"name":"partial", "code":"partial"})
28
 * @ApiFilter(PropertyFilter::class)
29
 * @ApiFilter(OrderFilter::class, properties={"name", "code"})
30
 * @ORM\Table(
31
 *     name="course_category",
32
 *     uniqueConstraints={
33
 *         @ORM\UniqueConstraint(name="code", columns={"code"})
34
 *     },
35
 *     indexes={
36
 *         @ORM\Index(name="parent_id", columns={"parent_id"}),
37
 *         @ORM\Index(name="tree_pos", columns={"tree_pos"})
38
 *     }
39
 * )
40
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\CourseCategoryRepository")
41
 */
42
class CourseCategory
43
{
44
    /**
45
     * @Groups({"course_category:read", "course:read"})
46
     * @ORM\Column(name="id", type="integer")
47
     * @ORM\Id
48
     * @ORM\GeneratedValue()
49
     */
50
    protected ?int $id = null;
51
52
    /**
53
     * @ORM\OneToMany(targetEntity="CourseCategory", mappedBy="parent")
54
     *
55
     * @var Collection|CourseCategory[]
56
     */
57
    protected Collection $children;
58
59
    /**
60
     * @Assert\NotBlank()
61
     * @Groups({"course_category:read", "course_category:write", "course:read"})
62
     * @ORM\Column(name="name", type="text", nullable=false)
63
     */
64
    protected string $name;
65
66
    /**
67
     * @Assert\NotBlank()
68
     * @Groups({"course_category:read", "course_category:write", "course:read"})
69
     * @ORM\Column(name="code", type="string", length=40, nullable=false)
70
     */
71
    protected string $code;
72
73
    /**
74
     * @ORM\ManyToOne(targetEntity="CourseCategory", inversedBy="children")
75
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
76
     */
77
    protected ?CourseCategory $parent = null;
78
79
    /**
80
     * @ORM\Column(name="tree_pos", type="integer", nullable=true)
81
     */
82
    protected ?int $treePos = null;
83
84
    /**
85
     * @ORM\Column(name="children_count", type="smallint", nullable=true)
86
     */
87
    protected ?int $childrenCount;
88
89
    /**
90
     * @ORM\Column(name="auth_course_child", type="string", length=40, nullable=true)
91
     */
92
    protected ?string $authCourseChild = null;
93
94
    /**
95
     * @ORM\Column(name="auth_cat_child", type="string", length=40, nullable=true)
96
     */
97
    protected ?string $authCatChild = null;
98
99
    /**
100
     * @ORM\Column(name="image", type="string", length=255, nullable=true)
101
     */
102
    protected ?string $image = null;
103
104
    /**
105
     * @Groups({"course_category:read", "course_category:write"})
106
     * @ORM\Column(name="description", type="text", nullable=true)
107
     */
108
    protected ?string $description = null;
109
110
    /**
111
     * @ORM\OneToMany(
112
     *     targetEntity="Chamilo\CoreBundle\Entity\AccessUrlRelCourseCategory",
113
     *     mappedBy="courseCategory", cascade={"persist"}, orphanRemoval=true
114
     * )
115
     *
116
     * @var AccessUrlRelCourseCategory[]|Collection
117
     */
118
    protected Collection $urls;
119
120
    /**
121
     * @ORM\ManyToMany(targetEntity="Chamilo\CoreBundle\Entity\Course", mappedBy="categories")
122
     */
123
    protected Collection $courses;
124
125
    public function __construct()
126
    {
127
        $this->urls = new ArrayCollection();
128
        $this->childrenCount = 0;
129
        $this->children = new ArrayCollection();
130
        $this->courses = new ArrayCollection();
131
        $this->authCatChild = 'TRUE';
132
        $this->authCourseChild = 'TRUE';
133
    }
134
135
    public function __toString(): string
136
    {
137
        $name = strip_tags($this->name);
138
139
        return sprintf('%s (%s)', $name, $this->code);
140
    }
141
142
    /**
143
     * Get id.
144
     *
145
     * @return int
146
     */
147
    public function getId()
148
    {
149
        return $this->id;
150
    }
151
152
    public function getParent(): ?self
153
    {
154
        return $this->parent;
155
    }
156
157
    public function setParent(self $parent): self
158
    {
159
        $this->parent = $parent;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return Collection
166
     */
167
    public function getChildren()
168
    {
169
        return $this->children;
170
    }
171
172
    public function addChild(self $child): self
173
    {
174
        $this->children[] = $child;
175
        $child->setParent($this);
176
177
        return $this;
178
    }
179
180
    public function setName(string $name): self
181
    {
182
        $this->name = $name;
183
184
        return $this;
185
    }
186
187
    public function getName(): string
188
    {
189
        return $this->name;
190
    }
191
192
    public function setCode(string $code): self
193
    {
194
        $this->code = $code;
195
196
        return $this;
197
    }
198
199
    /**
200
     * Get code.
201
     *
202
     * @return string
203
     */
204
    public function getCode()
205
    {
206
        return $this->code;
207
    }
208
209
    public function setTreePos(int $treePos): self
210
    {
211
        $this->treePos = $treePos;
212
213
        return $this;
214
    }
215
216
    /**
217
     * Get treePos.
218
     *
219
     * @return int
220
     */
221
    public function getTreePos()
222
    {
223
        return $this->treePos;
224
    }
225
226
    public function setChildrenCount(int $childrenCount): self
227
    {
228
        $this->childrenCount = $childrenCount;
229
230
        return $this;
231
    }
232
233
    /**
234
     * Get childrenCount.
235
     *
236
     * @return int
237
     */
238
    public function getChildrenCount()
239
    {
240
        return $this->childrenCount;
241
    }
242
243
    public function setAuthCourseChild(string $authCourseChild): self
244
    {
245
        $this->authCourseChild = $authCourseChild;
246
247
        return $this;
248
    }
249
250
    /**
251
     * Get authCourseChild.
252
     *
253
     * @return string
254
     */
255
    public function getAuthCourseChild()
256
    {
257
        return $this->authCourseChild;
258
    }
259
260
    public function setAuthCatChild(string $authCatChild): self
261
    {
262
        $this->authCatChild = $authCatChild;
263
264
        return $this;
265
    }
266
267
    /**
268
     * Get authCatChild.
269
     *
270
     * @return string
271
     */
272
    public function getAuthCatChild()
273
    {
274
        return $this->authCatChild;
275
    }
276
277
    public function getImage(): string
278
    {
279
        return $this->image;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->image 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...
280
    }
281
282
    public function setImage(string $image): self
283
    {
284
        $this->image = $image;
285
286
        return $this;
287
    }
288
289
    public function getDescription(): ?string
290
    {
291
        return $this->description;
292
    }
293
294
    public function setDescription(string $description): self
295
    {
296
        $this->description = $description;
297
298
        return $this;
299
    }
300
301
    public function getCourses(): Collection
302
    {
303
        return $this->courses;
304
    }
305
306
    public function setCourses(Collection $courses): self
307
    {
308
        $this->courses = $courses;
309
310
        return $this;
311
    }
312
313
    public function addCourse(Course $course): void
314
    {
315
        $this->courses[] = $course;
316
    }
317
318
    /**
319
     * @return AccessUrlRelCourseCategory[]|Collection
320
     */
321
    public function getUrls()
322
    {
323
        return $this->urls;
324
    }
325
326
    public function setUrls($urls): self
327
    {
328
        $this->urls = $urls;
329
330
        return $this;
331
    }
332
333
    public function addUrl(AccessUrl $accessUrl): self
334
    {
335
        if (!$this->hasUrl($accessUrl)) {
336
            $item = (new AccessUrlRelCourseCategory())
337
                ->setCourseCategory($this)
338
                ->setUrl($accessUrl)
339
            ;
340
            $this->urls->add($item);
341
        }
342
343
        return $this;
344
    }
345
346
    public function hasUrl(AccessUrl $accessUrl): bool
347
    {
348
        if (0 !== $this->urls->count()) {
349
            $criteria = Criteria::create()->where(
350
                Criteria::expr()->eq('url', $accessUrl)
351
            );
352
            $relation = $this->urls->matching($criteria);
353
354
            return $relation->count() > 0;
355
        }
356
357
        return false;
358
    }
359
}
360