Passed
Push — master ( 2cabbf...4cd979 )
by Julito
12:06
created

CourseCategory::setAsset()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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
     * @Groups({"course_category:read", "course_category:write", "course:read"})
61
     * @ORM\Column(name="name", type="text", nullable=false)
62
     */
63
    #[Assert\NotBlank]
64
    protected string $name;
65
66
    /**
67
     * @Groups({"course_category:read", "course_category:write", "course:read"})
68
     * @ORM\Column(name="code", type="string", length=40, nullable=false)
69
     */
70
    #[Assert\NotBlank]
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\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Asset", inversedBy="courseCategories", cascade={"remove"} )
101
     * @ORM\JoinColumn(name="asset_id", referencedColumnName="id", onDelete="SET NULL")
102
     */
103
    protected ?Asset $asset = null;
104
105
    /**
106
     * @Groups({"course_category:read", "course_category:write"})
107
     * @ORM\Column(name="description", type="text", nullable=true)
108
     */
109
    protected ?string $description = null;
110
111
    /**
112
     * @ORM\OneToMany(
113
     *     targetEntity="Chamilo\CoreBundle\Entity\AccessUrlRelCourseCategory",
114
     *     mappedBy="courseCategory", cascade={"persist"}, orphanRemoval=true
115
     * )
116
     *
117
     * @var AccessUrlRelCourseCategory[]|Collection
118
     */
119
    protected Collection $urls;
120
121
    /**
122
     * @var Course[]|Collection
123
     * @ORM\ManyToMany(targetEntity="Course", mappedBy="categories")
124
     */
125
    protected Collection $courses;
126
127
    public function __construct()
128
    {
129
        $this->childrenCount = 0;
130
        $this->authCatChild = 'TRUE';
131
        $this->authCourseChild = 'TRUE';
132
        $this->urls = new ArrayCollection();
133
        $this->children = new ArrayCollection();
134
        $this->courses = new ArrayCollection();
135
    }
136
137
    public function __toString(): string
138
    {
139
        $name = strip_tags($this->name);
140
141
        return sprintf('%s (%s)', $name, $this->code);
142
    }
143
144
    /**
145
     * Get id.
146
     *
147
     * @return int
148
     */
149
    public function getId()
150
    {
151
        return $this->id;
152
    }
153
154
    public function getParent(): ?self
155
    {
156
        return $this->parent;
157
    }
158
159
    public function setParent(self $parent): self
160
    {
161
        $this->parent = $parent;
162
163
        return $this;
164
    }
165
166
    /**
167
     * @return Collection
168
     */
169
    public function getChildren()
170
    {
171
        return $this->children;
172
    }
173
174
    public function addChild(self $child): self
175
    {
176
        $this->children[] = $child;
177
        $child->setParent($this);
178
179
        return $this;
180
    }
181
182
    public function setName(string $name): self
183
    {
184
        $this->name = $name;
185
186
        return $this;
187
    }
188
189
    public function getName(): string
190
    {
191
        return $this->name;
192
    }
193
194
    public function setCode(string $code): self
195
    {
196
        $this->code = $code;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Get code.
203
     *
204
     * @return string
205
     */
206
    public function getCode()
207
    {
208
        return $this->code;
209
    }
210
211
    public function setTreePos(int $treePos): self
212
    {
213
        $this->treePos = $treePos;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Get treePos.
220
     *
221
     * @return int
222
     */
223
    public function getTreePos()
224
    {
225
        return $this->treePos;
226
    }
227
228
    public function setChildrenCount(int $childrenCount): self
229
    {
230
        $this->childrenCount = $childrenCount;
231
232
        return $this;
233
    }
234
235
    /**
236
     * Get childrenCount.
237
     *
238
     * @return int
239
     */
240
    public function getChildrenCount()
241
    {
242
        return $this->childrenCount;
243
    }
244
245
    public function setAuthCourseChild(string $authCourseChild): self
246
    {
247
        $this->authCourseChild = $authCourseChild;
248
249
        return $this;
250
    }
251
252
    public function getAuthCourseChild(): ?string
253
    {
254
        return $this->authCourseChild;
255
    }
256
257
    public function setAuthCatChild(string $authCatChild): self
258
    {
259
        $this->authCatChild = $authCatChild;
260
261
        return $this;
262
    }
263
264
    public function getAuthCatChild(): ?string
265
    {
266
        return $this->authCatChild;
267
    }
268
269
    public function getDescription(): ?string
270
    {
271
        return $this->description;
272
    }
273
274
    public function setDescription(string $description): self
275
    {
276
        $this->description = $description;
277
278
        return $this;
279
    }
280
281
    public function getAsset(): ?Asset
282
    {
283
        return $this->asset;
284
    }
285
286
    public function hasAsset(): bool
287
    {
288
        return null !== $this->asset;
289
    }
290
291
    public function setAsset(?Asset $asset): self
292
    {
293
        $this->asset = $asset;
294
295
        return $this;
296
    }
297
298
    public function getCourses(): Collection
299
    {
300
        return $this->courses;
301
    }
302
303
    public function setCourses(Collection $courses): self
304
    {
305
        $this->courses = $courses;
306
307
        return $this;
308
    }
309
310
    public function addCourse(Course $course): void
311
    {
312
        $this->courses[] = $course;
313
    }
314
315
    /**
316
     * @return AccessUrlRelCourseCategory[]|Collection
317
     */
318
    public function getUrls()
319
    {
320
        return $this->urls;
321
    }
322
323
    /**
324
     * @param AccessUrlRelCourseCategory[]|Collection $urls
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