Passed
Push — master ( 39a018...0017ef )
by Julito
10:02 queued 12s
created

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