Completed
Push — master ( a3ace3...0ae69c )
by Julito
11:10
created

ResourceFile::getWidth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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