CLink::getCategory()   A
last analyzed

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 ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
10
use ApiPlatform\Metadata\ApiFilter;
11
use ApiPlatform\Metadata\ApiProperty;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Delete;
14
use ApiPlatform\Metadata\Get;
15
use ApiPlatform\Metadata\GetCollection;
16
use ApiPlatform\Metadata\Post;
17
use ApiPlatform\Metadata\Put;
18
use ApiPlatform\OpenApi\Model\Operation;
19
use ApiPlatform\OpenApi\Model\Parameter;
20
use ApiPlatform\OpenApi\Model\RequestBody;
21
use ArrayObject;
22
use Chamilo\CoreBundle\Controller\Api\CheckCLinkAction;
23
use Chamilo\CoreBundle\Controller\Api\CLinkDetailsController;
24
use Chamilo\CoreBundle\Controller\Api\CLinkImageController;
25
use Chamilo\CoreBundle\Controller\Api\CreateCLinkAction;
26
use Chamilo\CoreBundle\Controller\Api\GetLinksCollectionController;
27
use Chamilo\CoreBundle\Controller\Api\UpdateCLinkAction;
28
use Chamilo\CoreBundle\Controller\Api\UpdatePositionLink;
29
use Chamilo\CoreBundle\Controller\Api\UpdateVisibilityLink;
30
use Chamilo\CoreBundle\Entity\AbstractResource;
31
use Chamilo\CoreBundle\Entity\Asset;
32
use Chamilo\CoreBundle\Entity\ResourceInterface;
33
use Chamilo\CoreBundle\Entity\ResourceShowCourseResourcesInSessionInterface;
34
use Chamilo\CourseBundle\Repository\CLinkRepository;
35
use Doctrine\ORM\Mapping as ORM;
36
use Stringable;
37
use Symfony\Component\Serializer\Annotation\Groups;
38
use Symfony\Component\Validator\Constraints as Assert;
39
40
#[ApiResource(
41
    shortName: 'Link',
42
    operations: [
43
        new Put(
44
            controller: UpdateCLinkAction::class,
45
            denormalizationContext: [
46
                'groups' => ['link:write'],
47
            ],
48
            security: "is_granted('EDIT', object.resourceNode)",
49
            validationContext: [
50
                'groups' => ['media_object_create', 'link:write'],
51
            ],
52
            deserialize: false
53
        ),
54
        new Put(
55
            uriTemplate: '/links/{iid}/toggle_visibility',
56
            controller: UpdateVisibilityLink::class,
57
            security: "is_granted('EDIT', object.resourceNode)",
58
            deserialize: false
59
        ),
60
        new Put(
61
            uriTemplate: '/links/{iid}/move',
62
            controller: UpdatePositionLink::class,
63
            security: "is_granted('EDIT', object.resourceNode)",
64
            deserialize: false
65
        ),
66
        new Post(
67
            controller: CreateCLinkAction::class,
68
            openapi: new Operation(
69
                summary: 'Create a new link resource',
70
                requestBody: new RequestBody(
71
                    content: new ArrayObject([
72
                        'application/json' => [
73
                            'schema' => [
74
                                'type' => 'object',
75
                                'properties' => [
76
                                    'url' => ['type' => 'string'],
77
                                    'title' => ['type' => 'string'],
78
                                    'description' => ['type' => 'string'],
79
                                    'category_id' => ['type' => 'integer'],
80
                                    'target' => ['type' => 'string'],
81
                                    'parentResourceNodeId' => ['type' => 'integer'],
82
                                    'resourceLinkList' => [
83
                                        'type' => 'array',
84
                                        'items' => [
85
                                            'type' => 'object',
86
                                            'properties' => [
87
                                                'visibility' => ['type' => 'integer'],
88
                                                'cid' => ['type' => 'integer'],
89
                                                'gid' => ['type' => 'integer'],
90
                                                'sid' => ['type' => 'integer'],
91
                                            ],
92
                                        ],
93
                                    ],
94
                                ],
95
                                'required' => ['url', 'title'],
96
                            ],
97
                        ],
98
                    ]),
99
                ),
100
            ),
101
            security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')",
102
            validationContext: ['groups' => ['Default', 'media_object_create', 'link:write']],
103
            deserialize: false
104
        ),
105
        new Post(
106
            uriTemplate: '/links/{iid}/upload-image',
107
            controller: CLinkImageController::class,
108
            openapi: new Operation(
109
                summary: 'Upload a custom image for a link',
110
                requestBody: new RequestBody(
111
                    content: new ArrayObject([
112
                        'multipart/form-data' => [
113
                            'schema' => [
114
                                'type' => 'object',
115
                                'properties' => [
116
                                    'customImage' => [
117
                                        'type' => 'string',
118
                                        'format' => 'binary',
119
                                    ],
120
                                ],
121
                                'required' => ['customImage'],
122
                            ],
123
                        ],
124
                    ]),
125
                ),
126
            ),
127
            security: "is_granted('EDIT', object.resourceNode)",
128
            deserialize: false
129
        ),
130
        new Get(security: "is_granted('VIEW', object.resourceNode)"),
131
        new Get(
132
            uriTemplate: '/links/{iid}/details',
133
            controller: CLinkDetailsController::class,
134
            openapi: new Operation(
135
                summary: 'Gets the details of a link, including whether it is on the homepage',
136
            ),
137
            security: "is_granted('VIEW', object.resourceNode)"
138
        ),
139
        new Get(
140
            uriTemplate: '/links/{iid}/check',
141
            controller: CheckCLinkAction::class,
142
            openapi: new Operation(
143
                summary: 'Check if a link URL is valid',
144
            ),
145
            security: "is_granted('VIEW', object.resourceNode)"
146
        ),
147
        new Delete(security: "is_granted('DELETE', object.resourceNode)"),
148
        new GetCollection(
149
            controller: GetLinksCollectionController::class,
150
            openapi: new Operation(
151
                parameters: [
152
                    new Parameter(
153
                        name: 'resourceNode.parent',
154
                        in: 'query',
155
                        description: 'Resource node Parent',
156
                        required: true,
157
                        schema: ['type' => 'integer'],
158
                    ),
159
                    new Parameter(
160
                        name: 'cid',
161
                        in: 'query',
162
                        description: 'Course id',
163
                        required: true,
164
                        schema: ['type' => 'integer'],
165
                    ),
166
                    new Parameter(
167
                        name: 'sid',
168
                        in: 'query',
169
                        description: 'Session id',
170
                        required: false,
171
                        schema: ['type' => 'integer'],
172
                    ),
173
                ],
174
            )
175
        ),
176
    ],
177
    normalizationContext: [
178
        'groups' => ['link:read', 'resource_node:read'],
179
    ],
180
    denormalizationContext: [
181
        'groups' => ['link:write'],
182
    ],
183
)]
184
#[ApiFilter(SearchFilter::class, properties: ['title' => 'partial', 'resourceNode.parent' => 'exact'])]
185
#[ORM\Table(name: 'c_link')]
186
#[ORM\Entity(repositoryClass: CLinkRepository::class)]
187
class CLink extends AbstractResource implements ResourceInterface, ResourceShowCourseResourcesInSessionInterface, Stringable
188
{
189
    #[ApiProperty(identifier: true)]
190
    #[Groups(['link:read'])]
191
    #[ORM\Column(name: 'iid', type: 'integer')]
192
    #[ORM\Id]
193
    #[ORM\GeneratedValue]
194
    protected ?int $iid = null;
195
196
    #[Groups(['link:read', 'link:write', 'link:browse'])]
197
    #[Assert\NotBlank]
198
    #[ORM\Column(name: 'url', type: 'text', nullable: false)]
199
    protected string $url;
200
201
    #[Groups(['link:read', 'link:write', 'link:browse'])]
202
    #[Assert\NotBlank]
203
    #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
204
    protected string $title;
205
206
    #[Groups(['link:read', 'link:write', 'link:browse'])]
207
    #[ORM\Column(name: 'description', type: 'text', nullable: true)]
208
    protected ?string $description;
209
210
    #[Groups(['link:read', 'link:write', 'link:browse'])]
211
    #[ORM\ManyToOne(targetEntity: CLinkCategory::class, inversedBy: 'links')]
212
    #[ORM\JoinColumn(name: 'category_id', referencedColumnName: 'iid', onDelete: 'SET NULL')]
213
    protected ?CLinkCategory $category = null;
214
215
    #[Groups(['link:read', 'link:write', 'link:browse'])]
216
    #[ORM\Column(name: 'target', type: 'string', length: 10, nullable: true)]
217
    protected ?string $target = null;
218
219
    #[Groups(['link:read', 'link:browse'])]
220
    protected bool $linkVisible = true;
221
222
    #[Groups(['cshortcut:read'])]
223
    #[ORM\ManyToOne(targetEntity: Asset::class, cascade: ['remove'])]
224
    #[ORM\JoinColumn(name: 'custom_image_id', referencedColumnName: 'id', onDelete: 'SET NULL')]
225
    private ?Asset $customImage = null;
226
227
    public function __construct()
228
    {
229
        $this->description = '';
230
    }
231
232
    public function __toString(): string
233
    {
234
        return $this->getTitle();
235
    }
236
237
    public function setUrl(string $url): self
238
    {
239
        $this->url = $url;
240
241
        return $this;
242
    }
243
244
    public function getUrl(): string
245
    {
246
        return $this->url;
247
    }
248
249
    public function setTitle(string $title): self
250
    {
251
        $this->title = $title;
252
253
        return $this;
254
    }
255
256
    public function getTitle(): string
257
    {
258
        return $this->title;
259
    }
260
261
    public function setDescription(string $description): self
262
    {
263
        $this->description = $description;
264
265
        return $this;
266
    }
267
268
    public function getDescription(): ?string
269
    {
270
        return $this->description;
271
    }
272
273
    public function setTarget(string $target): self
274
    {
275
        $this->target = $target;
276
277
        return $this;
278
    }
279
280
    /**
281
     * Get target.
282
     *
283
     * @return string
284
     */
285
    public function getTarget()
286
    {
287
        return $this->target;
288
    }
289
290
    public function getIid(): int
291
    {
292
        return $this->iid;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->iid 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...
293
    }
294
295
    public function getCategory(): ?CLinkCategory
296
    {
297
        return $this->category;
298
    }
299
300
    public function setCategory(?CLinkCategory $category): self
301
    {
302
        $this->category = $category;
303
304
        return $this;
305
    }
306
307
    public function getCustomImage(): ?Asset
308
    {
309
        return $this->customImage;
310
    }
311
312
    public function setCustomImage(?Asset $customImage): self
313
    {
314
        $this->customImage = $customImage;
315
316
        return $this;
317
    }
318
319
    public function toggleVisibility(): void
320
    {
321
        $this->linkVisible = !$this->getFirstResourceLink()->getVisibility();
322
    }
323
324
    public function getLinkVisible(): bool
325
    {
326
        $this->linkVisible = (bool) $this->getFirstResourceLink()->getVisibility();
327
328
        return $this->linkVisible;
329
    }
330
331
    public function getResourceIdentifier(): int
332
    {
333
        return $this->iid;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->iid 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...
334
    }
335
336
    public function getResourceName(): string
337
    {
338
        return $this->getTitle();
339
    }
340
341
    public function setResourceName(string $name): self
342
    {
343
        return $this->setTitle($name);
344
    }
345
}
346