Passed
Push — master ( 72f3da...dcd5ef )
by Yannick
09:41
created

ResourceFile::getAccessUrl()   A

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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Metadata\ApiFilter;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Get;
14
use ApiPlatform\Metadata\GetCollection;
15
use ApiPlatform\Metadata\Post;
16
use ApiPlatform\Serializer\Filter\PropertyFilter;
17
use Chamilo\CoreBundle\Controller\AddVariantResourceFileAction;
18
use Chamilo\CoreBundle\Controller\CreateResourceFileAction;
19
use Chamilo\CoreBundle\Repository\ResourceFileRepository;
20
use DateTime;
21
use DateTimeImmutable;
22
use Doctrine\ORM\Mapping as ORM;
23
use Gedmo\Mapping\Annotation as Gedmo;
24
use Gedmo\Timestampable\Traits\TimestampableEntity;
25
use Stringable;
26
use Symfony\Component\HttpFoundation\File\File;
27
use Symfony\Component\HttpFoundation\File\UploadedFile;
28
use Symfony\Component\Serializer\Annotation\Groups;
29
use Symfony\Component\Validator\Constraints as Assert;
30
use Vich\UploaderBundle\Mapping\Annotation as Vich;
31
32
//
33
// *     attributes={"security"="is_granted('ROLE_ADMIN')"},
34
/**
35
 * @Vich\Uploadable
36
 */
37
#[ApiResource(
38
    types: ['http://schema.org/MediaObject'],
39
    operations: [
40
        new Get(),
41
        new Post(
42
            controller: CreateResourceFileAction::class,
43
            openapiContext: [
44
                'summary' => 'Create a new resource file',
45
                'requestBody' => [
46
                    'content' => [
47
                        'multipart/form-data' => [
48
                            'schema' => [
49
                                'type' => 'object',
50
                                'properties' => [
51
                                    'file' => [
52
                                        'type' => 'string',
53
                                        'format' => 'binary',
54
                                    ],
55
                                ],
56
                            ],
57
                        ],
58
                    ],
59
                ],
60
            ],
61
            security: 'is_granted(\'ROLE_USER\')',
62
            validationContext: [
63
                'groups' => ['Default', 'media_object_create', 'document:write'],
64
            ],
65
            deserialize: false
66
        ),
67
        new Post(
68
            uriTemplate: '/resource_files/add_variant',
69
            controller: AddVariantResourceFileAction::class,
70
            openapiContext: [
71
                'summary' => 'Add a variant to an existing resource file',
72
                'requestBody' => [
73
                    'content' => [
74
                        'multipart/form-data' => [
75
                            'schema' => [
76
                                'type' => 'object',
77
                                'properties' => [
78
                                    'file' => [
79
                                        'type' => 'string',
80
                                        'format' => 'binary',
81
                                    ],
82
                                    'resourceNodeId' => [
83
                                        'type' => 'integer',
84
                                    ],
85
                                    'accessUrlId' => [
86
                                        'type' => 'integer',
87
                                    ],
88
                                ],
89
                            ],
90
                        ],
91
                    ],
92
                ],
93
            ],
94
            security: 'is_granted(\'ROLE_USER\')',
95
            deserialize: false,
96
            name: 'add_variant'
97
        ),
98
        new GetCollection(),
99
    ],
100
    normalizationContext: [
101
        'groups' => [
102
            'resource_file:read',
103
            'resource_node:read',
104
            'document:read',
105
            'media_object_read',
106
            'message:read',
107
            'personal_file:read',
108
        ],
109
    ]
110
)]
111
#[ORM\Table(name: 'resource_file')]
112
#[ORM\Entity(repositoryClass: ResourceFileRepository::class)]
113
#[ApiFilter(filterClass: PropertyFilter::class)]
114
#[ApiFilter(filterClass: SearchFilter::class, properties: ['name' => 'partial'])]
115
#[ApiFilter(filterClass: OrderFilter::class, properties: ['id', 'name', 'size', 'updatedAt'])]
116
class ResourceFile implements Stringable
117
{
118
    use TimestampableEntity;
119
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
120
    #[ORM\Id]
121
    #[ORM\Column(type: 'integer')]
122
    #[ORM\GeneratedValue]
123
    protected ?int $id = null;
124
    #[Assert\NotBlank]
125
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read'])]
126
    #[ORM\Column(type: 'string', length: 255)]
127
    protected ?string $title = null;
128
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
129
    #[ORM\Column(type: 'text', nullable: true)]
130
    protected ?string $mimeType = null;
131
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
132
    #[ORM\Column(type: 'text', nullable: true)]
133
    protected ?string $originalName = null;
134
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read'])]
135
    #[ORM\Column(type: 'simple_array', nullable: true)]
136
    protected ?array $dimensions;
137
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read', 'personal_file:read'])]
138
    #[ORM\Column(type: 'integer')]
139
    protected ?int $size = 0;
140
141
    /**
142
     * @Vich\UploadableField(
143
     *     mapping="resources",
144
     *     fileNameProperty="title",
145
     *     size="size",
146
     *     mimeType="mimeType",
147
     *     originalName="originalName",
148
     *     dimensions="dimensions"
149
     * )
150
     */
151
    //    #[Vich\UploadableField(
152
    //        mapping: 'resources',
153
    //        fileNameProperty: 'title',
154
    //        size: 'size',
155
    //        mimeType: 'mimeType',
156
    //        originalName: 'originalName',
157
    //        dimensions: 'dimensions'
158
    //    )]
159
    protected ?File $file = null;
160
    #[ORM\Column(name: 'crop', type: 'string', length: 255, nullable: true)]
161
    protected ?string $crop = null;
162
163
    /**
164
     * @var string[]
165
     */
166
    #[ORM\Column(type: 'array', nullable: true)]
167
    protected ?array $metadata = [];
168
    #[Groups(['message:read'])]
169
    protected ?bool $audio = null;
170
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
171
    protected ?bool $image = null;
172
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
173
    protected ?bool $video = null;
174
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
175
    protected ?bool $text = null;
176
    #[ORM\Column(name: 'description', type: 'text', nullable: true)]
177
    protected ?string $description = null;
178
179
    /**
180
     * @var DateTime|DateTimeImmutable
181
     */
182
    #[Gedmo\Timestampable(on: 'update')]
183
    #[ORM\Column(type: 'datetime')]
184
    protected $updatedAt;
185
186
    #[ORM\ManyToOne(targetEntity: AccessUrl::class)]
187
    #[ORM\JoinColumn(name: 'access_url_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
188
    protected ?AccessUrl $accessUrl = null;
189
190
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read'])]
191
    #[ORM\ManyToOne(inversedBy: 'resourceFiles')]
192
    private ?ResourceNode $resourceNode = null;
193
194
    public function __construct()
195
    {
196
        $this->size = 0;
197
        $this->metadata = [];
198
        $this->dimensions = [];
199
    }
200
    public function __toString(): string
201
    {
202
        return $this->getOriginalName();
203
    }
204
    public function isText(): bool
205
    {
206
        $mimeType = $this->getMimeType();
207
208
        return str_contains($mimeType, 'text');
209
    }
210
    public function isImage(): bool
211
    {
212
        $mimeType = $this->getMimeType();
213
214
        return str_contains($mimeType, 'image');
215
    }
216
    public function isVideo(): bool
217
    {
218
        $mimeType = $this->getMimeType();
219
220
        return str_contains($mimeType, 'video');
221
    }
222
    public function isAudio(): bool
223
    {
224
        $mimeType = $this->getMimeType();
225
226
        return str_contains($mimeType, 'audio');
227
    }
228
    public function getTitle(): ?string
229
    {
230
        return $this->title;
231
    }
232
    public function setTitle(?string $title): self
233
    {
234
        $this->title = $title;
235
236
        return $this;
237
    }
238
    public function getCrop(): ?string
239
    {
240
        return $this->crop;
241
    }
242
243
    /**
244
     * $crop example: 100,100,100,100 = width,height,x,y.
245
     */
246
    public function setCrop(string $crop): self
247
    {
248
        $this->crop = $crop;
249
250
        return $this;
251
    }
252
    public function getSize(): ?int
253
    {
254
        return $this->size;
255
    }
256
    public function setSize(?int $size): self
257
    {
258
        $this->size = $size;
259
260
        return $this;
261
    }
262
263
    /*public function isEnabled(): bool
264
     * {
265
     * return $this->enabled;
266
     * }
267
     * public function setEnabled(bool $enabled): self
268
     * {
269
     * $this->enabled = $enabled;
270
     * return $this;
271
     * }*/
272
    public function getId(): ?int
273
    {
274
        return $this->id;
275
    }
276
277
    /*public function getDescription(): string
278
     * {
279
     * return $this->description;
280
     * }
281
     * public function setDescription(string $description): self
282
     * {
283
     * $this->description = $description;
284
     * return $this;
285
     * }*/
286
    public function getMimeType(): ?string
287
    {
288
        return $this->mimeType;
289
    }
290
    public function setMimeType(?string $mimeType): self
291
    {
292
        $this->mimeType = $mimeType;
293
294
        return $this;
295
    }
296
    public function getOriginalName(): string
297
    {
298
        return $this->originalName;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->originalName 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...
299
    }
300
    public function setOriginalName(?string $originalName): self
301
    {
302
        $this->originalName = $originalName;
303
304
        return $this;
305
    }
306
    public function getDimensions(): array
307
    {
308
        return $this->dimensions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dimensions could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
309
    }
310
    public function setDimensions(?array $dimensions): self
311
    {
312
        $this->dimensions = $dimensions;
313
314
        return $this;
315
    }
316
    public function getWidth(): int
317
    {
318
        $data = $this->getDimensions();
319
        if ([] !== $data) {
320
            // $data = explode(',', $data);
321
            return (int) $data[0];
322
        }
323
324
        return 0;
325
    }
326
    public function getHeight(): int
327
    {
328
        $data = $this->getDimensions();
329
        if ([] !== $data) {
330
            // $data = explode(',', $data);
331
            return (int) $data[1];
332
        }
333
334
        return 0;
335
    }
336
    public function getMetadata(): array
337
    {
338
        return $this->metadata;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->metadata could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
339
    }
340
    public function setMetadata(array $metadata): self
341
    {
342
        $this->metadata = $metadata;
343
344
        return $this;
345
    }
346
    public function getDescription(): string
347
    {
348
        return $this->description;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->description 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...
349
    }
350
    public function setDescription(string $description): self
351
    {
352
        $this->description = $description;
353
354
        return $this;
355
    }
356
    public function getFile(): ?File
357
    {
358
        return $this->file;
359
    }
360
361
    public function setFile(File|UploadedFile|null $file = null): self
362
    {
363
        $this->file = $file;
364
        if (null !== $file) {
365
            // It is required that at least one field changes if you are using doctrine
366
            // otherwise the event listeners won't be called and the file is lost
367
            $this->updatedAt = new DateTimeImmutable();
368
        }
369
370
        return $this;
371
    }
372
373
    public function getAccessUrl(): ?AccessUrl
374
    {
375
        return $this->accessUrl;
376
    }
377
378
    public function setAccessUrl(?AccessUrl $accessUrl): self
379
    {
380
        $this->accessUrl = $accessUrl;
381
        return $this;
382
    }
383
384
    public function getResourceNode(): ?ResourceNode
385
    {
386
        return $this->resourceNode;
387
    }
388
389
    public function setResourceNode(?ResourceNode $resourceNode): static
390
    {
391
        $this->resourceNode = $resourceNode;
392
393
        return $this;
394
    }
395
}
396