Completed
Push — master ( 3de9bb...339b13 )
by Julito
09:11
created

ResourceFile::isImage()   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
nc 2
nop 0
dl 0
loc 8
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\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", "document:write"},
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
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
74
     * @ORM\Id
75
     * @ORM\Column(type="integer")
76
     * @ORM\GeneratedValue
77
     */
78
    protected $id;
79
80
    /**
81
     * @var string
82
     *
83
     * @Assert\NotBlank()
84
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
85
     *
86
     * @ORM\Column(type="string", length=255)
87
     */
88
    protected $name;
89
90
    /**
91
     * @var string
92
     *
93
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
94
     * @ORM\Column(type="text", nullable=true)
95
     */
96
    protected $mimeType;
97
98
    /**
99
     * @var string
100
     *
101
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
102
     * @ORM\Column(type="text", nullable=true)
103
     */
104
    protected $originalName;
105
106
    /**
107
     * @var string
108
     *
109
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
110
     * @ORM\Column(type="simple_array", nullable=true)
111
     */
112
    protected $dimensions;
113
114
    /**
115
     * @var int
116
     *
117
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
118
     *
119
     * @ORM\Column(type="integer", nullable=true)
120
     */
121
    protected $size;
122
123
    /**
124
     * @var File
125
     *
126
     * @Assert\NotNull()
127
     * @Vich\UploadableField(
128
     *     mapping="resources",
129
     *     fileNameProperty="name",
130
     *     size="size",
131
     *     mimeType="mimeType",
132
     *     originalName="originalName",
133
     *     dimensions="dimensions"
134
     * )
135
     */
136
    protected $file;
137
138
    /**
139
     * @var string
140
     *
141
     * @ORM\Column(name="crop", type="string", length=255, nullable=true)
142
     *
143
     */
144
    protected $crop;
145
146
    /**
147
     * @var ResourceNode
148
     *
149
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\ResourceNode", mappedBy="resourceFile")
150
     */
151
    protected $resourceNode;
152
153
    /**
154
     * @var array
155
     *
156
     * @ORM\Column(type="array", nullable=true)
157
     */
158
    protected $metadata;
159
160
    /**
161
     * @var boolean
162
     *
163
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
164
     */
165
    protected $isImage;
166
167
    /**
168
     * Constructor.
169
     */
170
    public function __construct()
171
    {
172
        $this->metadata = [];
173
        $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...
174
    }
175
176
    public function isImage(): bool
177
    {
178
        $mimeType = $this->getMimeType();
179
        if (false !== strpos($mimeType, 'image')) {
180
            return true;
181
        }
182
183
        return false;
184
    }
185
186
    public function __toString(): string
187
    {
188
        return $this->getOriginalName();
189
    }
190
191
    /**
192
     * @return string
193
     */
194
    public function getName()
195
    {
196
        return $this->name;
197
    }
198
199
    /**
200
     * @return ResourceFile
201
     */
202
    public function setName($name)
203
    {
204
        $this->name = $name;
205
206
        return $this;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getCrop()
213
    {
214
        return $this->crop;
215
    }
216
217
    /**
218
     * @param string $crop
219
     *
220
     * @return $this
221
     */
222
    public function setCrop($crop)
223
    {
224
        $this->crop = $crop;
225
226
        return $this;
227
    }
228
229
    public function getSize(): int
230
    {
231
        return (int) $this->size;
232
    }
233
234
    /**
235
     * @param int $size
236
     */
237
    public function setSize($size): self
238
    {
239
        $this->size = $size;
240
241
        return $this;
242
    }
243
244
    public function getResourceNode(): ResourceNode
245
    {
246
        return $this->resourceNode;
247
    }
248
249
    public function setResourceNode(ResourceNode $resourceNode): self
250
    {
251
        $this->resourceNode = $resourceNode;
252
253
        return $this;
254
    }
255
256
    /*public function isEnabled(): bool
257
    {
258
        return $this->enabled;
259
    }
260
261
    public function setEnabled(bool $enabled): self
262
    {
263
        $this->enabled = $enabled;
264
265
        return $this;
266
    }*/
267
268
    /**
269
     * @return int
270
     */
271
    public function getId()
272
    {
273
        return $this->id;
274
    }
275
276
    public function setId($id): self
277
    {
278
        $this->id = $id;
279
280
        return $this;
281
    }
282
283
    /*public function getDescription(): string
284
    {
285
        return $this->description;
286
    }
287
288
    public function setDescription(string $description): self
289
    {
290
        $this->description = $description;
291
292
        return $this;
293
    }*/
294
295
    public function getMimeType(): string
296
    {
297
        return $this->mimeType;
298
    }
299
300
    /**
301
     * @param string $mimeType
302
     */
303
    public function setMimeType($mimeType): self
304
    {
305
        $this->mimeType = $mimeType;
306
307
        return $this;
308
    }
309
310
    public function getOriginalName(): string
311
    {
312
        return (string) $this->originalName;
313
    }
314
315
    /**
316
     * @param string $originalName
317
     */
318
    public function setOriginalName($originalName): self
319
    {
320
        $this->originalName = $originalName;
321
322
        return $this;
323
    }
324
325
    public function getDimensions(): array
326
    {
327
        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...
328
    }
329
330
    /**
331
     * @param $dimensions
332
     */
333
    public function setDimensions($dimensions): self
334
    {
335
        $this->dimensions = $dimensions;
336
337
        return $this;
338
    }
339
340
    public function getWidth(): int
341
    {
342
        $data = $this->getDimensions();
343
        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...
344
            //$data = explode(',', $data);
345
346
            return (int) $data[0];
347
        }
348
349
        return 0;
350
    }
351
352
    public function getHeight(): int
353
    {
354
        $data = $this->getDimensions();
355
356
        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...
357
            //$data = explode(',', $data);
358
359
            return (int) $data[1];
360
        }
361
362
        return 0;
363
    }
364
365
    public function getMetadata(): array
366
    {
367
        return $this->metadata;
368
    }
369
370
    public function setMetadata(array $metadata): self
371
    {
372
        $this->metadata = $metadata;
373
374
        return $this;
375
    }
376
377
    /**
378
     * @return File
379
     */
380
    public function getFile(): ?File
381
    {
382
        return $this->file;
383
    }
384
385
    /**
386
     * @param File|UploadedFile $file
387
     */
388
    public function setFile(File $file = null): void
389
    {
390
        $this->file = $file;
391
392
        if (null !== $file) {
393
            // It is required that at least one field changes if you are using doctrine
394
            // otherwise the event listeners won't be called and the file is lost
395
            $this->updatedAt = new \DateTimeImmutable();
396
        }
397
    }
398
399
    public function getContentUrl(): ?string
400
    {
401
        return $this->contentUrl;
402
    }
403
404
    public function setContentUrl(?string $contentUrl): self
405
    {
406
        $this->contentUrl = $contentUrl;
407
408
        return $this;
409
    }
410
}
411