Completed
Push — master ( c58ed8...556108 )
by Julito
10:31
created

ResourceFile   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 359
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 359
rs 10
c 0
b 0
f 0
wmc 29

26 Methods

Rating   Name   Duplication   Size   Complexity  
A getDimensions() 0 3 1
A setOriginalName() 0 5 1
A setCrop() 0 5 1
A getOriginalName() 0 3 1
A getId() 0 3 1
A __toString() 0 3 1
A getResourceNode() 0 3 1
A setName() 0 5 1
A getContentUrl() 0 3 1
A setSize() 0 5 1
A getMimeType() 0 3 1
A getName() 0 3 1
A setContentUrl() 0 5 1
A setResourceNode() 0 5 1
A setDimensions() 0 5 1
A setId() 0 5 1
A setMetadata() 0 5 1
A __construct() 0 3 1
A getWidth() 0 10 2
A getMetadata() 0 3 1
A getCrop() 0 3 1
A setMimeType() 0 5 1
A getHeight() 0 11 2
A getFile() 0 3 1
A setFile() 0 8 2
A getSize() 0 3 1
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\ApiProperty;
9
use ApiPlatform\Core\Annotation\ApiResource;
10
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
11
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
12
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
13
use Chamilo\CoreBundle\Controller\CreateResourceFileAction;
14
use Doctrine\ORM\Mapping as ORM;
15
use Gedmo\Timestampable\Traits\TimestampableEntity;
16
use Symfony\Component\HttpFoundation\File\File;
17
use Symfony\Component\HttpFoundation\File\UploadedFile;
18
use Symfony\Component\Serializer\Annotation\Groups;
19
use Symfony\Component\Validator\Constraints as Assert;
20
use Vich\UploaderBundle\Mapping\Annotation as Vich;
21
22
//
23
//*     attributes={"security"="is_granted('ROLE_ADMIN')"},
24
/**
25
 * @ApiResource(
26
 *     iri="http://schema.org/MediaObject",
27
 *     normalizationContext={
28
 *      "groups"={"resource_file:read", "resource_node:read", "document:read", "media_object_read"}
29
 *     },
30
 *     collectionOperations={
31
 *         "post"={
32
 *             "controller"=CreateResourceFileAction::class,
33
 *             "deserialize"=false,
34
 *             "security"="is_granted('ROLE_USER')",
35
 *             "validation_groups"={"Default", "media_object_create"},
36
 *             "openapi_context"={
37
 *                 "requestBody"={
38
 *                     "content"={
39
 *                         "multipart/form-data"={
40
 *                             "schema"={
41
 *                                 "type"="object",
42
 *                                 "properties"={
43
 *                                     "file"={
44
 *                                         "type"="string",
45
 *                                         "format"="binary"
46
 *                                     }
47
 *                                 }
48
 *                             }
49
 *                         }
50
 *                     }
51
 *                 }
52
 *             }
53
 *         },
54
 *         "get"
55
 *     },
56
 *     itemOperations={
57
 *         "get"
58
 *     }
59
 * )
60
 * @ApiFilter(SearchFilter::class, properties={"name": "partial"})
61
 * @ApiFilter(PropertyFilter::class)
62
 * @ApiFilter(OrderFilter::class, properties={"id", "name", "size", "updatedAt"})
63
 * @ORM\Entity
64
 * @Vich\Uploadable
65
 *
66
 * @ORM\Table(name="resource_file")
67
 */
68
class ResourceFile
69
{
70
    use TimestampableEntity;
71
72
    /**
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
     * @var string
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
     * @ORM\Column(type="text", nullable=true)
101
     */
102
    protected $originalName;
103
104
    /**
105
     * @var string
106
     *
107
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
108
     * @ORM\Column(type="simple_array", nullable=true)
109
     */
110
    protected $dimensions;
111
112
    /**
113
     * @var int
114
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
115
     *
116
     * @ORM\Column(type="integer", nullable=true)
117
     */
118
    protected $size;
119
120
    /**
121
     * @var File
122
     *
123
     * @Assert\NotNull(groups={"media_object_create"})
124
     * @Vich\UploadableField(
125
     *     mapping="resources",
126
     *     fileNameProperty="name",
127
     *     size="size",
128
     *     mimeType="mimeType",
129
     *     originalName="originalName",
130
     *     dimensions="dimensions"
131
     * )
132
     */
133
    protected $file;
134
135
    /**
136
     * @var string
137
     *
138
     * @ORM\Column(name="crop", type="string", length=255, nullable=true)
139
     */
140
    protected $crop;
141
142
    /**
143
     * @var string|null
144
     *
145
     * @ApiProperty(iri="http://schema.org/contentUrl")
146
     * @Groups({"resource_file:read", "resource_node:read", "document:read", "media_object_read"})
147
     */
148
    public $contentUrl;
149
150
    /**
151
     * @var ResourceNode
152
     *
153
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\ResourceNode", mappedBy="resourceFile")
154
     */
155
    protected $resourceNode;
156
157
    /**
158
     * @var array
159
     *
160
     * @ORM\Column(type="array", nullable=true)
161
     */
162
    protected $metadata;
163
164
    /**
165
     * Constructor.
166
     */
167
    public function __construct()
168
    {
169
        $this->metadata = [];
170
    }
171
172
    public function __toString(): string
173
    {
174
        return (string) $this->getOriginalName();
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getName()
181
    {
182
        return $this->name;
183
    }
184
185
    /**
186
     * @return ResourceFile
187
     */
188
    public function setName($name)
189
    {
190
        $this->name = $name;
191
192
        return $this;
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getCrop()
199
    {
200
        return $this->crop;
201
    }
202
203
    /**
204
     * @param string $crop
205
     *
206
     * @return $this
207
     */
208
    public function setCrop($crop)
209
    {
210
        $this->crop = $crop;
211
212
        return $this;
213
    }
214
215
    public function getSize(): int
216
    {
217
        return (int) $this->size;
218
    }
219
220
    /**
221
     * @param int $size
222
     */
223
    public function setSize($size): self
224
    {
225
        $this->size = $size;
226
227
        return $this;
228
    }
229
230
    /*public function getCopyright(): string
231
    {
232
        return (string) $this->copyright;
233
    }*/
234
235
    /*public function getContentType(): string
236
    {
237
        return (string) $this->contentType;
238
    }
239
240
    public function setContentType(string $contentType): self
241
    {
242
        $this->contentType = $contentType;
243
244
        return $this;
245
    }*/
246
247
    /*public function getExtension(): string
248
    {
249
        return $this->extension;
250
    }
251
252
    public function setExtension(string $extension): self
253
    {
254
        $this->extension = $extension;
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
    /**
292
     * @return ResourceFile
293
     */
294
    public function setId($id)
295
    {
296
        $this->id = $id;
297
298
        return $this;
299
    }
300
301
    /*public function getDescription(): string
302
    {
303
        return $this->description;
304
    }
305
306
    public function setDescription(string $description): self
307
    {
308
        $this->description = $description;
309
310
        return $this;
311
    }*/
312
313
    public function getMimeType(): string
314
    {
315
        return $this->mimeType;
316
    }
317
318
    /**
319
     * @param string $mimeType
320
     */
321
    public function setMimeType($mimeType): self
322
    {
323
        $this->mimeType = $mimeType;
324
325
        return $this;
326
    }
327
328
    public function getOriginalName(): string
329
    {
330
        return $this->originalName;
331
    }
332
333
    /**
334
     * @param string $originalName
335
     */
336
    public function setOriginalName($originalName): self
337
    {
338
        $this->originalName = $originalName;
339
340
        return $this;
341
    }
342
343
    public function getDimensions(): array
344
    {
345
        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...
346
    }
347
348
    /**
349
     * @param $dimensions
350
     */
351
    public function setDimensions($dimensions): self
352
    {
353
        $this->dimensions = $dimensions;
354
355
        return $this;
356
    }
357
358
    public function getWidth(): int
359
    {
360
        $data = $this->getDimensions();
361
        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...
362
            //$data = explode(',', $data);
363
364
            return (int) $data[0];
365
        }
366
367
        return 0;
368
    }
369
370
    public function getHeight(): int
371
    {
372
        $data = $this->getDimensions();
373
374
        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...
375
            //$data = explode(',', $data);
376
377
            return (int) $data[1];
378
        }
379
380
        return 0;
381
    }
382
383
    public function getMetadata(): array
384
    {
385
        return $this->metadata;
386
    }
387
388
    public function setMetadata(array $metadata): self
389
    {
390
        $this->metadata = $metadata;
391
392
        return $this;
393
    }
394
395
    /**
396
     * @return File
397
     */
398
    public function getFile(): ?File
399
    {
400
        return $this->file;
401
    }
402
403
    /**
404
     * @param File|UploadedFile $file
405
     */
406
    public function setFile(File $file = null): void
407
    {
408
        $this->file = $file;
409
410
        if (null !== $file) {
411
            // It is required that at least one field changes if you are using doctrine
412
            // otherwise the event listeners won't be called and the file is lost
413
            $this->updatedAt = new \DateTimeImmutable();
0 ignored issues
show
Bug Best Practice introduced by
The property updatedAt does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
414
        }
415
    }
416
417
    public function getContentUrl(): ?string
418
    {
419
        return $this->contentUrl;
420
    }
421
422
    public function setContentUrl(?string $contentUrl): self
423
    {
424
        $this->contentUrl = $contentUrl;
425
426
        return $this;
427
    }
428
}
429