Passed
Push — master ( 6dca6a...44ae74 )
by Julito
09:32
created

ResourceFile::getResourceNode()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * @Assert\NotBlank()
81
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
82
     *
83
     * @ORM\Column(type="string", length=255)
84
     */
85
    protected $name;
86
87
    /**
88
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
89
     * @ORM\Column(type="text", nullable=true)
90
     */
91
    protected $mimeType;
92
93
    /**
94
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
95
     * @ORM\Column(type="text", nullable=true)
96
     */
97
    protected $originalName;
98
99
    /**
100
     * @var string
101
     *
102
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
103
     * @ORM\Column(type="simple_array", nullable=true)
104
     */
105
    protected $dimensions;
106
107
    /**
108
     * @var int
109
     *
110
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
111
     *
112
     * @ORM\Column(type="integer")
113
     */
114
    protected $size;
115
116
    /**
117
     * @var File
118
     *
119
     * @Assert\NotNull()
120
     * @Vich\UploadableField(
121
     *     mapping="resources",
122
     *     fileNameProperty="name",
123
     *     size="size",
124
     *     mimeType="mimeType",
125
     *     originalName="originalName",
126
     *     dimensions="dimensions"
127
     * )
128
     */
129
    protected $file;
130
131
    /**
132
     * @ORM\Column(name="crop", type="string", length=255, nullable=true)
133
     */
134
    protected ?string $crop;
135
136
    /**
137
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\ResourceNode", mappedBy="resourceFile")
138
     */
139
    protected ResourceNode $resourceNode;
140
141
    /**
142
     * @ORM\Column(type="array", nullable=true)
143
     */
144
    protected ?array $metadata;
145
146
    /**
147
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
148
     */
149
    protected ?bool $image;
150
151
    /**
152
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
153
     */
154
    protected ?bool $video;
155
156
    /**
157
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
158
     */
159
    protected ?bool $text;
160
161
    /**
162
     * @ORM\Column(name="description", type="text", nullable=true)
163
     */
164
    protected ?string $description;
165
166
    public function __construct()
167
    {
168
        $this->metadata = [];
169
        $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...
170
        $this->size = 0;
171
    }
172
173
    public function __toString(): string
174
    {
175
        return $this->getOriginalName();
176
    }
177
178
    public function isText(): bool
179
    {
180
        $mimeType = $this->getMimeType();
181
        if (false !== strpos($mimeType, 'text')) {
182
            return true;
183
        }
184
185
        return false;
186
    }
187
188
    public function isImage(): bool
189
    {
190
        $mimeType = $this->getMimeType();
191
        if (false !== strpos($mimeType, 'image')) {
192
            return true;
193
        }
194
195
        return false;
196
    }
197
198
    public function isVideo(): bool
199
    {
200
        $mimeType = $this->getMimeType();
201
        if (false !== strpos($mimeType, 'video')) {
202
            return true;
203
        }
204
205
        return false;
206
    }
207
208
    public function getName(): string
209
    {
210
        return $this->name;
211
    }
212
213
    public function setName($name): self
214
    {
215
        $this->name = $name;
216
217
        return $this;
218
    }
219
220
    /**
221
     * @return string
222
     */
223
    public function getCrop()
224
    {
225
        return $this->crop;
226
    }
227
228
    /**
229
     * @param string $crop
230
     *
231
     * @return $this
232
     */
233
    public function setCrop($crop): self
234
    {
235
        $this->crop = $crop;
236
237
        return $this;
238
    }
239
240
    public function getSize(): int
241
    {
242
        return (int) $this->size;
243
    }
244
245
    /**
246
     * @param int $size
247
     */
248
    public function setSize($size): self
249
    {
250
        $this->size = $size;
251
252
        return $this;
253
    }
254
255
    public function getResourceNode(): ResourceNode
256
    {
257
        return $this->resourceNode;
258
    }
259
260
    public function setResourceNode(ResourceNode $resourceNode): self
261
    {
262
        $this->resourceNode = $resourceNode;
263
264
        return $this;
265
    }
266
267
    /*public function isEnabled(): bool
268
    {
269
        return $this->enabled;
270
    }
271
272
    public function setEnabled(bool $enabled): self
273
    {
274
        $this->enabled = $enabled;
275
276
        return $this;
277
    }*/
278
279
    /**
280
     * @return int
281
     */
282
    public function getId()
283
    {
284
        return $this->id;
285
    }
286
287
    /*public function getDescription(): string
288
    {
289
        return $this->description;
290
    }
291
292
    public function setDescription(string $description): self
293
    {
294
        $this->description = $description;
295
296
        return $this;
297
    }*/
298
299
    public function getMimeType(): string
300
    {
301
        return $this->mimeType;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->mimeType 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...
302
    }
303
304
    /**
305
     * @param string $mimeType
306
     */
307
    public function setMimeType($mimeType): self
308
    {
309
        $this->mimeType = $mimeType;
310
311
        return $this;
312
    }
313
314
    public function getOriginalName(): string
315
    {
316
        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...
317
    }
318
319
    /**
320
     * @param string $originalName
321
     */
322
    public function setOriginalName($originalName): self
323
    {
324
        $this->originalName = $originalName;
325
326
        return $this;
327
    }
328
329
    public function getDimensions(): array
330
    {
331
        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...
332
    }
333
334
    /**
335
     * @param $dimensions
336
     */
337
    public function setDimensions($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) {
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...
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) {
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...
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 $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
    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