Completed
Push — master ( a88956...84abaf )
by Julito
13:00
created

ResourceFile::isVideo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiFilter;
8
use ApiPlatform\Core\Annotation\ApiResource;
9
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
12
use Chamilo\CoreBundle\Controller\CreateResourceFileAction;
13
use Doctrine\ORM\Mapping as ORM;
14
use Gedmo\Timestampable\Traits\TimestampableEntity;
15
use Symfony\Component\HttpFoundation\File\File;
16
use Symfony\Component\HttpFoundation\File\UploadedFile;
17
use Symfony\Component\Serializer\Annotation\Groups;
18
use Symfony\Component\Validator\Constraints as Assert;
19
use Vich\UploaderBundle\Mapping\Annotation as Vich;
20
21
//
22
//*     attributes={"security"="is_granted('ROLE_ADMIN')"},
23
/**
24
 * @ApiResource(
25
 *     iri="http://schema.org/MediaObject",
26
 *     normalizationContext={
27
 *      "groups"={"resource_file:read", "resource_node:read", "document:read", "media_object_read"}
28
 *     },
29
 *     collectionOperations={
30
 *         "post"={
31
 *             "controller"=CreateResourceFileAction::class,
32
 *             "deserialize"=false,
33
 *             "security"="is_granted('ROLE_USER')",
34
 *             "validation_groups"={"Default", "media_object_create", "document:write"},
35
 *             "openapi_context"={
36
 *                 "requestBody"={
37
 *                     "content"={
38
 *                         "multipart/form-data"={
39
 *                             "schema"={
40
 *                                 "type"="object",
41
 *                                 "properties"={
42
 *                                     "file"={
43
 *                                         "type"="string",
44
 *                                         "format"="binary"
45
 *                                     }
46
 *                                 }
47
 *                             }
48
 *                         }
49
 *                     }
50
 *                 }
51
 *             }
52
 *         },
53
 *         "get"
54
 *     },
55
 *     itemOperations={
56
 *         "get"
57
 *     }
58
 * )
59
 * @ApiFilter(SearchFilter::class, properties={"name": "partial"})
60
 * @ApiFilter(PropertyFilter::class)
61
 * @ApiFilter(OrderFilter::class, properties={"id", "name", "size", "updatedAt"})
62
 * @ORM\Entity
63
 * @Vich\Uploadable
64
 *
65
 * @ORM\Table(name="resource_file")
66
 */
67
class ResourceFile
68
{
69
    use TimestampableEntity;
70
71
    /**
72
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
73
     * @ORM\Id
74
     * @ORM\Column(type="integer")
75
     * @ORM\GeneratedValue
76
     */
77
    protected $id;
78
79
    /**
80
     * @var string
81
     *
82
     * @Assert\NotBlank()
83
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
84
     *
85
     * @ORM\Column(type="string", length=255)
86
     */
87
    protected $name;
88
89
    /**
90
     * @var string
91
     *
92
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
93
     * @ORM\Column(type="text", nullable=true)
94
     */
95
    protected $mimeType;
96
97
    /**
98
     * @var string
99
     *
100
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
101
     * @ORM\Column(type="text", nullable=true)
102
     */
103
    protected $originalName;
104
105
    /**
106
     * @var string
107
     *
108
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
109
     * @ORM\Column(type="simple_array", nullable=true)
110
     */
111
    protected $dimensions;
112
113
    /**
114
     * @var int
115
     *
116
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
117
     *
118
     * @ORM\Column(type="integer", nullable=true)
119
     */
120
    protected $size;
121
122
    /**
123
     * @var File
124
     *
125
     * @Assert\NotNull()
126
     * @Vich\UploadableField(
127
     *     mapping="resources",
128
     *     fileNameProperty="name",
129
     *     size="size",
130
     *     mimeType="mimeType",
131
     *     originalName="originalName",
132
     *     dimensions="dimensions"
133
     * )
134
     */
135
    protected $file;
136
137
    /**
138
     * @var string
139
     *
140
     * @ORM\Column(name="crop", type="string", length=255, nullable=true)
141
     */
142
    protected $crop;
143
144
    /**
145
     * @var ResourceNode
146
     *
147
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\ResourceNode", mappedBy="resourceFile")
148
     */
149
    protected $resourceNode;
150
151
    /**
152
     * @var array
153
     *
154
     * @ORM\Column(type="array", nullable=true)
155
     */
156
    protected $metadata;
157
158
    /**
159
     * @var bool
160
     *
161
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
162
     */
163
    protected $image;
164
165
    /**
166
     * @var bool
167
     *
168
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
169
     */
170
    protected $video;
171
172
    /**
173
     * Constructor.
174
     */
175
    public function __construct()
176
    {
177
        $this->metadata = [];
178
        $this->dimensions = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type string of property $dimensions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
179
    }
180
181
    public function isImage(): bool
182
    {
183
        $mimeType = $this->getMimeType();
184
        if (false !== strpos($mimeType, 'image')) {
185
            return true;
186
        }
187
188
        return false;
189
    }
190
191
    public function isVideo(): bool
192
    {
193
        $mimeType = $this->getMimeType();
194
        if (false !== strpos($mimeType, 'video')) {
195
            return true;
196
        }
197
198
        return false;
199
    }
200
201
    public function __toString(): string
202
    {
203
        return $this->getOriginalName();
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function getName()
210
    {
211
        return $this->name;
212
    }
213
214
    /**
215
     * @return ResourceFile
216
     */
217
    public function setName($name)
218
    {
219
        $this->name = $name;
220
221
        return $this;
222
    }
223
224
    /**
225
     * @return string
226
     */
227
    public function getCrop()
228
    {
229
        return $this->crop;
230
    }
231
232
    /**
233
     * @param string $crop
234
     *
235
     * @return $this
236
     */
237
    public function setCrop($crop)
238
    {
239
        $this->crop = $crop;
240
241
        return $this;
242
    }
243
244
    public function getSize(): int
245
    {
246
        return (int) $this->size;
247
    }
248
249
    /**
250
     * @param int $size
251
     */
252
    public function setSize($size): self
253
    {
254
        $this->size = $size;
255
256
        return $this;
257
    }
258
259
    public function getResourceNode(): ResourceNode
260
    {
261
        return $this->resourceNode;
262
    }
263
264
    public function setResourceNode(ResourceNode $resourceNode): self
265
    {
266
        $this->resourceNode = $resourceNode;
267
268
        return $this;
269
    }
270
271
    /*public function isEnabled(): bool
272
    {
273
        return $this->enabled;
274
    }
275
276
    public function setEnabled(bool $enabled): self
277
    {
278
        $this->enabled = $enabled;
279
280
        return $this;
281
    }*/
282
283
    /**
284
     * @return int
285
     */
286
    public function getId()
287
    {
288
        return $this->id;
289
    }
290
291
    public function setId($id): self
292
    {
293
        $this->id = $id;
294
295
        return $this;
296
    }
297
298
    /*public function getDescription(): string
299
    {
300
        return $this->description;
301
    }
302
303
    public function setDescription(string $description): self
304
    {
305
        $this->description = $description;
306
307
        return $this;
308
    }*/
309
310
    public function getMimeType(): string
311
    {
312
        return $this->mimeType;
313
    }
314
315
    /**
316
     * @param string $mimeType
317
     */
318
    public function setMimeType($mimeType): self
319
    {
320
        $this->mimeType = $mimeType;
321
322
        return $this;
323
    }
324
325
    public function getOriginalName(): string
326
    {
327
        return (string) $this->originalName;
328
    }
329
330
    /**
331
     * @param string $originalName
332
     */
333
    public function setOriginalName($originalName): self
334
    {
335
        $this->originalName = $originalName;
336
337
        return $this;
338
    }
339
340
    public function getDimensions(): array
341
    {
342
        return $this->dimensions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dimensions returns the type string which is incompatible with the type-hinted return array.
Loading history...
343
    }
344
345
    /**
346
     * @param $dimensions
347
     */
348
    public function setDimensions($dimensions): self
349
    {
350
        $this->dimensions = $dimensions;
351
352
        return $this;
353
    }
354
355
    public function getWidth(): int
356
    {
357
        $data = $this->getDimensions();
358
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
359
            //$data = explode(',', $data);
360
361
            return (int) $data[0];
362
        }
363
364
        return 0;
365
    }
366
367
    public function getHeight(): int
368
    {
369
        $data = $this->getDimensions();
370
371
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
372
            //$data = explode(',', $data);
373
374
            return (int) $data[1];
375
        }
376
377
        return 0;
378
    }
379
380
    public function getMetadata(): array
381
    {
382
        return $this->metadata;
383
    }
384
385
    public function setMetadata(array $metadata): self
386
    {
387
        $this->metadata = $metadata;
388
389
        return $this;
390
    }
391
392
    /**
393
     * @return File
394
     */
395
    public function getFile(): ?File
396
    {
397
        return $this->file;
398
    }
399
400
    /**
401
     * @param File|UploadedFile $file
402
     */
403
    public function setFile(File $file = null): void
404
    {
405
        $this->file = $file;
406
407
        if (null !== $file) {
408
            // It is required that at least one field changes if you are using doctrine
409
            // otherwise the event listeners won't be called and the file is lost
410
            $this->updatedAt = new \DateTimeImmutable();
411
        }
412
    }
413
414
    public function getContentUrl(): ?string
415
    {
416
        return $this->contentUrl;
417
    }
418
419
    public function setContentUrl(?string $contentUrl): self
420
    {
421
        $this->contentUrl = $contentUrl;
422
423
        return $this;
424
    }
425
}
426