Completed
Push — master ( bb9871...4572bc )
by Julito
11:01 queued 10s
created

ResourceFile::getName()   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 Chamilo\MediaBundle\Entity\Media;
7
use Doctrine\ORM\Mapping as ORM;
8
use Gedmo\Timestampable\Traits\TimestampableEntity;
9
use Symfony\Component\HttpFoundation\File\File;
10
use Vich\UploaderBundle\Mapping\Annotation as Vich;
11
use Symfony\Component\Validator\Constraints as Assert;
12
13
/**
14
 * @ORM\Entity
15
 * @Vich\Uploadable
16
 * @ORM\Table(name="resource_file")
17
 */
18
class ResourceFile
19
{
20
    use TimestampableEntity;
21
22
    /**
23
     * @ORM\Id
24
     * @ORM\Column(type="integer")
25
     * @ORM\GeneratedValue
26
     */
27
    protected $id;
28
29
    /**
30
     *
31
     * @Assert\NotBlank()
32
     *
33
     * @ORM\Column(type="string", length=255)
34
     *
35
     * @var string
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
     * @var int
92
     *
93
     * @ORM\Column(type="integer")
94
     */
95
    protected $size;
96
97
    /**
98
     * @var File
99
     *
100
     * @Vich\UploadableField(mapping="resources", fileNameProperty="name", size="size", mimeType="mimeType", originalName="originalName", dimensions="dimensions")
101
     */
102
    protected $file;
103
104
//    /**
105
//     * @var string
106
//     *
107
//     * @Assert\NotBlank()
108
//     *
109
//     * @ORM\Column(name="hash", type="string", nullable=false)
110
//     */
111
//    protected $hash;
112
113
//    /**
114
//     * @Assert\NotBlank()
115
//     *
116
//     * @var string
117
//     *
118
//     * @ORM\Column(name="original_filename", type="string", nullable=false)
119
//     */
120
//    protected $originalFilename;
121
//
122
//    /**
123
//     * @Assert\NotBlank()
124
//     *
125
//     * @var string
126
//     *
127
//     * @ORM\Column(name="size", type="string", nullable=false)
128
//     */
129
//    protected $size;
130
//
131
//    /**
132
//     * @Assert\NotBlank()
133
//     *
134
//     * @var string
135
//     *
136
//     * @ORM\Column(name="width", type="string", nullable=true)
137
//     */
138
//    protected $width;
139
140
//    /**
141
//     * @Assert\NotBlank()
142
//     *
143
//     * @var string
144
//     *
145
//     * @ORM\Column(name="height", type="string", nullable=true)
146
//     */
147
//    protected $height;
148
//
149
//    /**
150
//     * @var string
151
//     *
152
//     * @ORM\Column(name="copyright", type="string", nullable=true)
153
//     */
154
//    protected $copyright;
155
156
//    /**
157
//     * @var string
158
//     *
159
//     * @ORM\Column(name="contentType", type="string", nullable=true)
160
//     */
161
//    protected $contentType;
162
//
163
//    /**
164
//     * @var string
165
//     *
166
//     * @ORM\Column(name="extension", type="string", nullable=false)
167
//     */
168
//    protected $extension;
169
170
    /**
171
     * @var ResourceNode
172
     *
173
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", mappedBy="resourceFile")
174
     */
175
    protected $resourceNode;
176
177
    /**
178
     * @var bool
179
     *
180
     * @ORM\Column(name="enabled", type="boolean")
181
     */
182
    //protected $enabled;
183
184
    /**
185
     * Constructor.
186
     */
187
    public function __construct()
188
    {
189
        $this->enabled = true;
190
        $this->setOriginalFilename(uniqid());
191
    }
192
193
    /**
194
     * @return mixed
195
     */
196
    public function getName()
197
    {
198
        return $this->name;
199
    }
200
201
    /**
202
     * @param mixed $name
203
     *
204
     * @return ResourceFile
205
     */
206
    public function setName($name)
207
    {
208
        $this->name = $name;
209
210
        return $this;
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    public function getHash(): string
217
    {
218
        return $this->hash;
219
    }
220
221
    /**
222
     * @param string $hash
223
     *
224
     * @return ResourceFile
225
     */
226
    public function setHash(string $hash): ResourceFile
227
    {
228
        $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...
229
230
        return $this;
231
    }
232
233
    /**
234
     * @return string
235
     */
236
    public function getOriginalFilename(): string
237
    {
238
        return $this->originalFilename;
239
    }
240
241
    /**
242
     * @param string $originalFilename
243
     *
244
     * @return ResourceFile
245
     */
246
    public function setOriginalFilename(string $originalFilename): ResourceFile
247
    {
248
        $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...
249
250
        return $this;
251
    }
252
253
    /**
254
     * @return string
255
     */
256
    public function getSize(): string
257
    {
258
        return $this->size;
259
    }
260
261
    /**
262
     * @param string $size
263
     *
264
     * @return ResourceFile
265
     */
266
    public function setSize(string $size): ResourceFile
267
    {
268
        $this->size = $size;
0 ignored issues
show
Documentation Bug introduced by
The property $size was declared of type integer, but $size is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
269
270
        return $this;
271
    }
272
273
    /**
274
     * @return string
275
     */
276
    public function getCopyright(): string
277
    {
278
        return (string) $this->copyright;
279
    }
280
281
    /**
282
     * @return string
283
     */
284
    public function getContentType(): string
285
    {
286
        return (string) $this->contentType;
287
    }
288
289
    /**
290
     * @param string $contentType
291
     *
292
     * @return ResourceFile
293
     */
294
    public function setContentType(string $contentType): ResourceFile
295
    {
296
        $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...
297
298
        return $this;
299
    }
300
301
    /**
302
     * @return string
303
     */
304
    public function getExtension(): string
305
    {
306
        return $this->extension;
307
    }
308
309
    /**
310
     * @param string $extension
311
     *
312
     * @return ResourceFile
313
     */
314
    public function setExtension(string $extension): ResourceFile
315
    {
316
        $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...
317
318
        return $this;
319
    }
320
321
    /**
322
     * @return ResourceNode
323
     */
324
    public function getResourceNode(): ResourceNode
325
    {
326
        return $this->resourceNode;
327
    }
328
329
    /**
330
     * @param ResourceNode $resourceNode
331
     *
332
     * @return ResourceFile
333
     */
334
    public function setResourceNode(ResourceNode $resourceNode): ResourceFile
335
    {
336
        $this->resourceNode = $resourceNode;
337
338
        return $this;
339
    }
340
341
    /**
342
     * @return bool
343
     */
344
    public function isEnabled(): bool
345
    {
346
        return $this->enabled;
347
    }
348
349
    /**
350
     * @param bool $enabled
351
     *
352
     * @return ResourceFile
353
     */
354
    public function setEnabled(bool $enabled): ResourceFile
355
    {
356
        $this->enabled = $enabled;
357
358
        return $this;
359
    }
360
361
    /**
362
     * @return Media
363
     */
364
    public function getMedia()
365
    {
366
        return $this->media;
367
    }
368
369
    /**
370
     * @param Media $media
371
     *
372
     * @return ResourceFile
373
     */
374
    public function setMedia($media)
375
    {
376
        $this->media = $media;
0 ignored issues
show
Bug Best Practice introduced by
The property media does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
377
378
        return $this;
379
    }
380
381
    /**
382
     * @return mixed
383
     */
384
    public function getId()
385
    {
386
        return $this->id;
387
    }
388
389
    /**
390
     * @param mixed $id
391
     *
392
     * @return ResourceFile
393
     */
394
    public function setId($id)
395
    {
396
        $this->id = $id;
397
398
        return $this;
399
    }
400
401
    /**
402
     * @return string
403
     */
404
    public function getDescription(): string
405
    {
406
        return $this->description;
407
    }
408
409
    /**
410
     * @param string $description
411
     *
412
     * @return ResourceFile
413
     */
414
    public function setDescription(string $description): ResourceFile
415
    {
416
        $this->description = $description;
417
418
        return $this;
419
    }
420
421
    /**
422
     * @return string
423
     */
424
    public function getMimeType(): string
425
    {
426
        return $this->mimeType;
427
    }
428
429
    /**
430
     * @param string $mimeType
431
     *
432
     * @return ResourceFile
433
     */
434
    public function setMimeType(string $mimeType): ResourceFile
435
    {
436
        $this->mimeType = $mimeType;
437
438
        return $this;
439
    }
440
441
    /**
442
     * @return string
443
     */
444
    public function getOriginalName(): string
445
    {
446
        return $this->originalName;
447
    }
448
449
    /**
450
     * @param string $originalName
451
     *
452
     * @return ResourceFile
453
     */
454
    public function setOriginalName(string $originalName): ResourceFile
455
    {
456
        $this->originalName = $originalName;
457
458
        return $this;
459
    }
460
461
    /**
462
     * @return array
463
     */
464
    public function getDimensions(): array
465
    {
466
        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...
467
    }
468
469
    /**
470
     * @param array $dimensions
471
     *
472
     * @return ResourceFile
473
     */
474
    public function setDimensions(array $dimensions): ResourceFile
475
    {
476
        $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...
477
478
        return $this;
479
    }
480
481
    /**
482
     * @return int
483
     */
484
    public function getWidth(): int
485
    {
486
        $data = $this->getDimensions();
487
488
        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...
489
            $data = explode(',', $data);
490
491
            return (int) $data[0];
492
        }
493
494
        return 0;
495
    }
496
497
    /**
498
     * @return int
499
     */
500
    public function getHeight(): int
501
    {
502
        $data = $this->getDimensions();
503
504
        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...
505
            $data = explode(',', $data);
506
507
            return (int) $data[1];
508
        }
509
510
        return 0;
511
    }
512
513
    /**
514
     * @return File
515
     */
516
    public function getFile(): ?File
517
    {
518
        return $this->file;
519
    }
520
521
    /**
522
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
523
     */
524
    public function setFile(File $file = null): void
525
    {
526
        $this->file = $file;
527
528
        if (null !== $file) {
529
            // It is required that at least one field changes if you are using doctrine
530
            // otherwise the event listeners won't be called and the file is lost
531
            $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...
532
        }
533
    }
534
}
535