Passed
Push — master ( ab20b0...4728a8 )
by Julito
09:22
created

ResourceFile::setDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
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")
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
     * @var string
174
     *
175
     * @ORM\Column(name="description", type="text", nullable=true)
176
     */
177
    protected $description;
178
179
    /**
180
     * Constructor.
181
     */
182
    public function __construct()
183
    {
184
        $this->metadata = [];
185
        $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...
186
        $this->size = 0;
187
    }
188
189
    public function __toString(): string
190
    {
191
        return $this->getOriginalName();
192
    }
193
194
    public function isImage(): bool
195
    {
196
        $mimeType = $this->getMimeType();
197
        if (false !== strpos($mimeType, 'image')) {
198
            return true;
199
        }
200
201
        return false;
202
    }
203
204
    public function isVideo(): bool
205
    {
206
        $mimeType = $this->getMimeType();
207
        if (false !== strpos($mimeType, 'video')) {
208
            return true;
209
        }
210
211
        return false;
212
    }
213
214
    public function getName(): string
215
    {
216
        return $this->name;
217
    }
218
219
    public function setName($name): self
220
    {
221
        $this->name = $name;
222
223
        return $this;
224
    }
225
226
    /**
227
     * @return string
228
     */
229
    public function getCrop()
230
    {
231
        return $this->crop;
232
    }
233
234
    /**
235
     * @param string $crop
236
     *
237
     * @return $this
238
     */
239
    public function setCrop($crop): self
240
    {
241
        $this->crop = $crop;
242
243
        return $this;
244
    }
245
246
    public function getSize(): int
247
    {
248
        return (int) $this->size;
249
    }
250
251
    /**
252
     * @param int $size
253
     */
254
    public function setSize($size): self
255
    {
256
        $this->size = $size;
257
258
        return $this;
259
    }
260
261
    public function getResourceNode(): ResourceNode
262
    {
263
        return $this->resourceNode;
264
    }
265
266
    public function setResourceNode(ResourceNode $resourceNode): self
267
    {
268
        $this->resourceNode = $resourceNode;
269
270
        return $this;
271
    }
272
273
    /*public function isEnabled(): bool
274
    {
275
        return $this->enabled;
276
    }
277
278
    public function setEnabled(bool $enabled): self
279
    {
280
        $this->enabled = $enabled;
281
282
        return $this;
283
    }*/
284
285
    /**
286
     * @return int
287
     */
288
    public function getId()
289
    {
290
        return $this->id;
291
    }
292
293
    /*public function getDescription(): string
294
    {
295
        return $this->description;
296
    }
297
298
    public function setDescription(string $description): self
299
    {
300
        $this->description = $description;
301
302
        return $this;
303
    }*/
304
305
    public function getMimeType(): string
306
    {
307
        return $this->mimeType;
308
    }
309
310
    /**
311
     * @param string $mimeType
312
     */
313
    public function setMimeType($mimeType): self
314
    {
315
        $this->mimeType = $mimeType;
316
317
        return $this;
318
    }
319
320
    public function getOriginalName(): string
321
    {
322
        return (string) $this->originalName;
323
    }
324
325
    /**
326
     * @param string $originalName
327
     */
328
    public function setOriginalName($originalName): self
329
    {
330
        $this->originalName = $originalName;
331
332
        return $this;
333
    }
334
335
    public function getDimensions(): array
336
    {
337
        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...
338
    }
339
340
    /**
341
     * @param $dimensions
342
     */
343
    public function setDimensions($dimensions): self
344
    {
345
        $this->dimensions = $dimensions;
346
347
        return $this;
348
    }
349
350
    public function getWidth(): int
351
    {
352
        $data = $this->getDimensions();
353
        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...
354
            //$data = explode(',', $data);
355
356
            return (int) $data[0];
357
        }
358
359
        return 0;
360
    }
361
362
    public function getHeight(): int
363
    {
364
        $data = $this->getDimensions();
365
366
        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...
367
            //$data = explode(',', $data);
368
369
            return (int) $data[1];
370
        }
371
372
        return 0;
373
    }
374
375
    public function getMetadata(): array
376
    {
377
        return $this->metadata;
378
    }
379
380
    public function setMetadata(array $metadata): self
381
    {
382
        $this->metadata = $metadata;
383
384
        return $this;
385
    }
386
387
    public function getDescription(): string
388
    {
389
        return $this->description;
390
    }
391
392
    public function setDescription(string $description): self
393
    {
394
        $this->description = $description;
395
396
        return $this;
397
    }
398
399
    public function getFile(): ?File
400
    {
401
        return $this->file;
402
    }
403
404
    /**
405
     * @param File|UploadedFile $file
406
     */
407
    public function setFile(File $file = null): self
408
    {
409
        $this->file = $file;
410
411
        if (null !== $file) {
412
            // It is required that at least one field changes if you are using doctrine
413
            // otherwise the event listeners won't be called and the file is lost
414
            $this->updatedAt = new \DateTimeImmutable();
415
        }
416
417
        return $this;
418
    }
419
420
    public function getContentUrl(): ?string
421
    {
422
        return $this->contentUrl;
423
    }
424
425
    public function setContentUrl(?string $contentUrl): self
426
    {
427
        $this->contentUrl = $contentUrl;
428
429
        return $this;
430
    }
431
}
432