Completed
Push — master ( 9fe4f1...0c649e )
by Julito
10:07
created

ResourceFile::getSize()   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
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Entity\Resource;
5
6
use Doctrine\ORM\Mapping as ORM;
7
use Gedmo\Timestampable\Traits\TimestampableEntity;
8
use Symfony\Component\HttpFoundation\File\File;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
use Symfony\Component\Validator\Constraints as Assert;
11
use Vich\UploaderBundle\Mapping\Annotation as Vich;
12
13
/**
14
 * @ORM\Entity
15
 *
16
 * @Vich\Uploadable
17
 *
18
 * @ORM\Table(name="resource_file")
19
 */
20
class ResourceFile
21
{
22
    use TimestampableEntity;
23
24
    /**
25
     * @ORM\Id
26
     * @ORM\Column(type="integer")
27
     * @ORM\GeneratedValue
28
     */
29
    protected $id;
30
31
    /**
32
     * @Assert\NotBlank()
33
     *
34
     * @var string
35
     *
36
     * @ORM\Column(type="string", length=255)
37
     */
38
    protected $name;
39
40
    /**
41
     * @var string
42
     */
43
    protected $description;
44
45
    /**
46
     * @var bool
47
     */
48
    protected $enabled = false;
49
50
    /**
51
     * @var int
52
     */
53
    protected $width;
54
55
    /**
56
     * @var int
57
     */
58
    protected $height;
59
60
    /**
61
     * @var float
62
     */
63
    protected $length;
64
65
    /**
66
     * @var string
67
     */
68
    protected $copyright;
69
70
    /**
71
     * @var string
72
     *
73
     * @ORM\Column(type="text", nullable=true)
74
     */
75
    protected $mimeType;
76
77
    /**
78
     * @var string
79
     *
80
     * @ORM\Column(type="text", nullable=true)
81
     */
82
    protected $originalName;
83
84
    /**
85
     * @var string
86
     *
87
     * @ORM\Column(type="simple_array", nullable=true)
88
     */
89
    protected $dimensions;
90
91
    /**
92
     * @var int
93
     *
94
     * @ORM\Column(type="integer", nullable=true)
95
     */
96
    protected $size;
97
98
    /**
99
     * @var File
100
     *
101
     * @Vich\UploadableField(
102
     *     mapping="resources",
103
     *     fileNameProperty="name",
104
     *     size="size",
105
     *     mimeType="mimeType",
106
     *     originalName="originalName",
107
     *     dimensions="dimensions"
108
     * )
109
     */
110
    protected $file;
111
112
    /**
113
     * @var string
114
     *
115
     * @ORM\Column(name="crop", type="string", length=255, nullable=true)
116
     */
117
    protected $crop;
118
119
    /**
120
     * @var ResourceNode
121
     *
122
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", mappedBy="resourceFile")
123
     */
124
    protected $resourceNode;
125
126
//    /**
127
//     * @var string
128
//     *
129
//     * @Assert\NotBlank()
130
//     *
131
//     * @ORM\Column(name="hash", type="string", nullable=false)
132
//     */
133
//    protected $hash;
134
135
//    /**
136
//     * @Assert\NotBlank()
137
//     *
138
//     * @var string
139
//     *
140
//     * @ORM\Column(name="original_filename", type="string", nullable=false)
141
//     */
142
//    protected $originalFilename;
143
//
144
//    /**
145
//     * @Assert\NotBlank()
146
//     *
147
//     * @var string
148
//     *
149
//     * @ORM\Column(name="size", type="string", nullable=false)
150
//     */
151
//    protected $size;
152
//
153
//    /**
154
//     * @Assert\NotBlank()
155
//     *
156
//     * @var string
157
//     *
158
//     * @ORM\Column(name="width", type="string", nullable=true)
159
//     */
160
//    protected $width;
161
162
//    /**
163
//     * @Assert\NotBlank()
164
//     *
165
//     * @var string
166
//     *
167
//     * @ORM\Column(name="height", type="string", nullable=true)
168
//     */
169
//    protected $height;
170
//
171
//    /**
172
//     * @var string
173
//     *
174
//     * @ORM\Column(name="copyright", type="string", nullable=true)
175
//     */
176
//    protected $copyright;
177
178
//    /**
179
//     * @var string
180
//     *
181
//     * @ORM\Column(name="contentType", type="string", nullable=true)
182
//     */
183
//    protected $contentType;
184
//
185
//    /**
186
//     * @var string
187
//     *
188
//     * @ORM\Column(name="extension", type="string", nullable=false)
189
//     */
190
//    protected $extension;
191
192
    /**
193
     * @var bool
194
     *
195
     * @ORM\Column(name="enabled", type="boolean")
196
     */
197
    //protected $enabled;
198
199
    /**
200
     * Constructor.
201
     */
202
    public function __construct()
203
    {
204
        $this->enabled = true;
205
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function __toString(): string
211
    {
212
        return (string) $this->getName();
213
    }
214
215
    /**
216
     * @return string
217
     */
218
    public function getName()
219
    {
220
        return $this->name;
221
    }
222
223
    /**
224
     * @param mixed $name
225
     *
226
     * @return ResourceFile
227
     */
228
    public function setName($name)
229
    {
230
        $this->name = $name;
231
232
        return $this;
233
    }
234
235
    /**
236
     * @return string
237
     */
238
    public function getCrop()
239
    {
240
        return $this->crop;
241
    }
242
243
    /**
244
     * @param string $crop
245
     *
246
     * @return $this
247
     */
248
    public function setCrop($crop)
249
    {
250
        $this->crop = $crop;
251
252
        return $this;
253
    }
254
255
    public function getHash(): string
256
    {
257
        return $this->hash;
258
    }
259
260
    public function setHash(string $hash): ResourceFile
261
    {
262
        $this->hash = $hash;
0 ignored issues
show
Bug Best Practice introduced by
The property hash does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
263
264
        return $this;
265
    }
266
267
    public function getSize(): int
268
    {
269
        return (int) $this->size;
270
    }
271
272
    /**
273
     * @param int $size
274
     */
275
    public function setSize($size): ResourceFile
276
    {
277
        $this->size = $size;
278
279
        return $this;
280
    }
281
282
    public function getCopyright(): string
283
    {
284
        return (string) $this->copyright;
285
    }
286
287
    public function getContentType(): string
288
    {
289
        return (string) $this->contentType;
290
    }
291
292
    public function setContentType(string $contentType): ResourceFile
293
    {
294
        $this->contentType = $contentType;
0 ignored issues
show
Bug Best Practice introduced by
The property contentType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
295
296
        return $this;
297
    }
298
299
    public function getExtension(): string
300
    {
301
        return $this->extension;
302
    }
303
304
    public function setExtension(string $extension): ResourceFile
305
    {
306
        $this->extension = $extension;
0 ignored issues
show
Bug Best Practice introduced by
The property extension does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
307
308
        return $this;
309
    }
310
311
    /**
312
     * @return ResourceNode
313
     */
314
    public function getResourceNode(): ResourceNode
315
    {
316
        return $this->resourceNode;
317
    }
318
319
    /**
320
     * @param ResourceNode $resourceNode
321
     *
322
     * @return ResourceFile
323
     */
324
    public function setResourceNode(ResourceNode $resourceNode): ResourceFile
325
    {
326
        $this->resourceNode = $resourceNode;
327
328
        return $this;
329
    }
330
331
    public function isEnabled(): bool
332
    {
333
        return $this->enabled;
334
    }
335
336
    public function setEnabled(bool $enabled): ResourceFile
337
    {
338
        $this->enabled = $enabled;
339
340
        return $this;
341
    }
342
343
    /**
344
     * @return int
345
     */
346
    public function getId()
347
    {
348
        return $this->id;
349
    }
350
351
    /**
352
     * @param mixed $id
353
     *
354
     * @return ResourceFile
355
     */
356
    public function setId($id)
357
    {
358
        $this->id = $id;
359
360
        return $this;
361
    }
362
363
    public function getDescription(): string
364
    {
365
        return $this->description;
366
    }
367
368
    public function setDescription(string $description): ResourceFile
369
    {
370
        $this->description = $description;
371
372
        return $this;
373
    }
374
375
    public function getMimeType(): string
376
    {
377
        return $this->mimeType;
378
    }
379
380
    /**
381
     * @param string $mimeType
382
     */
383
    public function setMimeType($mimeType): ResourceFile
384
    {
385
        $this->mimeType = $mimeType;
386
387
        return $this;
388
    }
389
390
    /**
391
     * @return string
392
     */
393
    public function getOriginalName(): string
394
    {
395
        return $this->originalName;
396
    }
397
398
    /**
399
     * @param $originalName
400
     *
401
     * @return ResourceFile
402
     */
403
    public function setOriginalName($originalName): ResourceFile
404
    {
405
        $this->originalName = $originalName;
406
407
        return $this;
408
    }
409
410
    /**
411
     * @return array
412
     */
413
    public function getDimensions(): array
414
    {
415
        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...
416
    }
417
418
    /**
419
     * @param $dimensions
420
     *
421
     * @return ResourceFile
422
     */
423
    public function setDimensions($dimensions): ResourceFile
424
    {
425
        $this->dimensions = $dimensions;
426
427
        return $this;
428
    }
429
430
    /**
431
     * @return int
432
     */
433
    public function getWidth(): int
434
    {
435
        $data = $this->getDimensions();
436
437
        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...
438
            $data = explode(',', $data);
439
440
            return (int) $data[0];
441
        }
442
443
        return 0;
444
    }
445
446
    /**
447
     * @return int
448
     */
449
    public function getHeight(): int
450
    {
451
        $data = $this->getDimensions();
452
453
        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...
454
            $data = explode(',', $data);
455
456
            return (int) $data[1];
457
        }
458
459
        return 0;
460
    }
461
462
    /**
463
     * @return File
464
     */
465
    public function getFile(): ?File
466
    {
467
        return $this->file;
468
    }
469
470
    /**
471
     * @param File|UploadedFile $file
472
     */
473
    public function setFile(File $file = null): void
474
    {
475
        $this->file = $file;
476
477
        if (null !== $file) {
478
            // It is required that at least one field changes if you are using doctrine
479
            // otherwise the event listeners won't be called and the file is lost
480
            $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...
481
        }
482
    }
483
}
484