Completed
Push — master ( 70cebd...08a97e )
by Julito
20:37 queued 06:13
created

ResourceFile::setDimensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
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 array
128
     *
129
     * @ORM\Column(type="array", nullable=true)
130
     */
131
    protected $metadata;
132
133
    /**
134
     * Constructor.
135
     */
136
    public function __construct()
137
    {
138
        $this->enabled = true;
139
    }
140
141
    public function __toString(): string
142
    {
143
        return (string) $this->getOriginalName();
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getName()
150
    {
151
        return $this->name;
152
    }
153
154
    /**
155
     * @param mixed $name
156
     *
157
     * @return ResourceFile
158
     */
159
    public function setName($name)
160
    {
161
        $this->name = $name;
162
163
        return $this;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getCrop()
170
    {
171
        return $this->crop;
172
    }
173
174
    /**
175
     * @param string $crop
176
     *
177
     * @return $this
178
     */
179
    public function setCrop($crop)
180
    {
181
        $this->crop = $crop;
182
183
        return $this;
184
    }
185
186
    public function getHash(): string
187
    {
188
        return $this->hash;
189
    }
190
191
    public function setHash(string $hash): ResourceFile
192
    {
193
        $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...
194
195
        return $this;
196
    }
197
198
    public function getSize(): int
199
    {
200
        return (int) $this->size;
201
    }
202
203
    /**
204
     * @param int $size
205
     */
206
    public function setSize($size): ResourceFile
207
    {
208
        $this->size = $size;
209
210
        return $this;
211
    }
212
213
    public function getCopyright(): string
214
    {
215
        return (string) $this->copyright;
216
    }
217
218
    public function getContentType(): string
219
    {
220
        return (string) $this->contentType;
221
    }
222
223
    public function setContentType(string $contentType): ResourceFile
224
    {
225
        $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...
226
227
        return $this;
228
    }
229
230
    public function getExtension(): string
231
    {
232
        return $this->extension;
233
    }
234
235
    public function setExtension(string $extension): ResourceFile
236
    {
237
        $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...
238
239
        return $this;
240
    }
241
242
    public function getResourceNode(): ResourceNode
243
    {
244
        return $this->resourceNode;
245
    }
246
247
    public function setResourceNode(ResourceNode $resourceNode): ResourceFile
248
    {
249
        $this->resourceNode = $resourceNode;
250
251
        return $this;
252
    }
253
254
    public function isEnabled(): bool
255
    {
256
        return $this->enabled;
257
    }
258
259
    public function setEnabled(bool $enabled): ResourceFile
260
    {
261
        $this->enabled = $enabled;
262
263
        return $this;
264
    }
265
266
    /**
267
     * @return int
268
     */
269
    public function getId()
270
    {
271
        return $this->id;
272
    }
273
274
    /**
275
     * @param mixed $id
276
     *
277
     * @return ResourceFile
278
     */
279
    public function setId($id)
280
    {
281
        $this->id = $id;
282
283
        return $this;
284
    }
285
286
    public function getDescription(): string
287
    {
288
        return $this->description;
289
    }
290
291
    public function setDescription(string $description): ResourceFile
292
    {
293
        $this->description = $description;
294
295
        return $this;
296
    }
297
298
    public function getMimeType(): string
299
    {
300
        return $this->mimeType;
301
    }
302
303
    /**
304
     * @param string $mimeType
305
     */
306
    public function setMimeType($mimeType): ResourceFile
307
    {
308
        $this->mimeType = $mimeType;
309
310
        return $this;
311
    }
312
313
    public function getOriginalName(): string
314
    {
315
        return $this->originalName;
316
    }
317
318
    /**
319
     * @param $originalName
320
     */
321
    public function setOriginalName($originalName): ResourceFile
322
    {
323
        $this->originalName = $originalName;
324
325
        return $this;
326
    }
327
328
    public function getDimensions(): array
329
    {
330
        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...
331
    }
332
333
    /**
334
     * @param $dimensions
335
     */
336
    public function setDimensions($dimensions): ResourceFile
337
    {
338
        $this->dimensions = $dimensions;
339
340
        return $this;
341
    }
342
343
    public function getWidth(): int
344
    {
345
        $data = $this->getDimensions();
346
        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...
347
            //$data = explode(',', $data);
348
349
            return (int) $data[0];
350
        }
351
352
        return 0;
353
    }
354
355
    public function getHeight(): int
356
    {
357
        $data = $this->getDimensions();
358
359
        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...
360
            //$data = explode(',', $data);
361
362
            return (int) $data[1];
363
        }
364
365
        return 0;
366
    }
367
368
    /**
369
     * @return array
370
     */
371
    public function getMetadata(): array
372
    {
373
        return $this->metadata;
374
    }
375
376
    /**
377
     * @param array $metadata
378
     *
379
     * @return ResourceFile
380
     */
381
    public function setMetadata(array $metadata): ResourceFile
382
    {
383
        $this->metadata = $metadata;
384
385
        return $this;
386
    }
387
388
    /**
389
     * @return File
390
     */
391
    public function getFile(): ?File
392
    {
393
        return $this->file;
394
    }
395
396
    /**
397
     * @param File|UploadedFile $file
398
     */
399
    public function setFile(File $file = null): void
400
    {
401
        $this->file = $file;
402
403
        if (null !== $file) {
404
            // It is required that at least one field changes if you are using doctrine
405
            // otherwise the event listeners won't be called and the file is lost
406
            $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...
407
        }
408
    }
409
}
410