Passed
Push — master ( 241b61...cc68cc )
by Julito
09:18
created

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