Passed
Push — master ( 4e7d2f...7a7af2 )
by Julito
08:07
created

Asset::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Entity;
6
7
use Doctrine\ORM\Mapping as ORM;
8
use Gedmo\Timestampable\Traits\TimestampableEntity;
9
use Symfony\Component\HttpFoundation\File\File;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
use Symfony\Component\Serializer\Annotation\Groups;
12
use Symfony\Component\Validator\Constraints as Assert;
13
use Vich\UploaderBundle\Mapping\Annotation as Vich;
14
15
/**
16
 * @ORM\Entity
17
 * @Vich\Uploadable
18
 *
19
 * @ORM\Table(name="asset")
20
 */
21
class Asset
22
{
23
    use TimestampableEntity;
24
25
    public const SCORM = 'scorm';
26
    public const WATERMARK = 'watermark';
27
    public const CSS = 'css';
28
29
    /**
30
     * @ORM\Id
31
     * @ORM\Column(type="integer")
32
     * @ORM\GeneratedValue
33
     */
34
    protected $id;
35
36
    /**
37
     * @var string
38
     *
39
     * @Assert\NotBlank()
40
     *
41
     * @ORM\Column(type="string", length=255)
42
     */
43
    protected $title;
44
45
    /**
46
     * @var string
47
     *
48
     * @Assert\Choice({Asset::SCORM, Asset::WATERMARK, Asset::CSS}, message="Choose a valid category.")
49
     *
50
     * @ORM\Column(type="string", length=255)
51
     */
52
    protected $category;
53
54
    /**
55
     * @var File
56
     *
57
     * @Assert\NotNull()
58
     * @Vich\UploadableField(
59
     *     mapping="assets",
60
     *     fileNameProperty="title",
61
     *     size="size",
62
     *     mimeType="mimeType",
63
     *     originalName="originalName",
64
     *     dimensions="dimensions"
65
     * )
66
     */
67
    protected $file;
68
69
    /**
70
     * @ORM\Column(type="boolean")
71
     */
72
    protected bool $compressed;
73
74
    /**
75
     * @var string
76
     *
77
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
78
     * @ORM\Column(type="text", nullable=true)
79
     */
80
    protected $mimeType;
81
82
    /**
83
     * @var string
84
     *
85
     * @ORM\Column(type="text", nullable=true)
86
     */
87
    protected $originalName;
88
89
    /**
90
     * @var string
91
     *
92
     * @Groups({"resource_file:read", "resource_node:read", "document:read"})
93
     * @ORM\Column(type="simple_array", nullable=true)
94
     */
95
    protected $dimensions;
96
97
    /**
98
     * @var int
99
     *
100
     * @ORM\Column(type="integer")
101
     */
102
    protected $size;
103
104
    /**
105
     * @var string
106
     *
107
     * @ORM\Column(name="crop", type="string", length=255, nullable=true)
108
     */
109
    protected $crop;
110
111
    /**
112
     * @var array
113
     *
114
     * @ORM\Column(type="array", nullable=true)
115
     */
116
    protected $metadata;
117
118
    /**
119
     * @var string
120
     *
121
     * @ORM\Column(name="description", type="text", nullable=true)
122
     */
123
    protected $description;
124
125
    public function __construct()
126
    {
127
        $this->metadata = [];
128
        $this->size = 0;
129
        $this->compressed = false;
130
    }
131
132
    /**
133
     * @return int
134
     */
135
    public function getId()
136
    {
137
        return $this->id;
138
    }
139
140
    public function getFolder()
141
    {
142
        return $this->category.'/'.$this->getOriginalName();
143
    }
144
145
    public function getFileUrl()
146
    {
147
        return $this->getFolder().'/'.$this->getOriginalName();
148
    }
149
150
    public function __toString(): string
151
    {
152
        return $this->getOriginalName();
153
    }
154
155
    public function isImage(): bool
156
    {
157
        $mimeType = $this->getMimeType();
158
        if (false !== strpos($mimeType, 'image')) {
159
            return true;
160
        }
161
162
        return false;
163
    }
164
165
    public function isVideo(): bool
166
    {
167
        $mimeType = $this->getMimeType();
168
        if (false !== strpos($mimeType, 'video')) {
169
            return true;
170
        }
171
172
        return false;
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getCrop()
179
    {
180
        return $this->crop;
181
    }
182
183
    /**
184
     * @param string $crop
185
     *
186
     * @return $this
187
     */
188
    public function setCrop($crop): self
189
    {
190
        $this->crop = $crop;
191
192
        return $this;
193
    }
194
195
    public function getSize(): int
196
    {
197
        return (int) $this->size;
198
    }
199
200
    /**
201
     * @param int $size
202
     */
203
    public function setSize($size): self
204
    {
205
        $this->size = $size;
206
207
        return $this;
208
    }
209
210
    /*public function getDescription(): string
211
    {
212
        return $this->description;
213
    }
214
215
    public function setDescription(string $description): self
216
    {
217
        $this->description = $description;
218
219
        return $this;
220
    }*/
221
222
    public function getMimeType(): string
223
    {
224
        return $this->mimeType;
225
    }
226
227
    /**
228
     * @param string $mimeType
229
     */
230
    public function setMimeType($mimeType): self
231
    {
232
        $this->mimeType = $mimeType;
233
234
        return $this;
235
    }
236
237
    public function getOriginalName(): string
238
    {
239
        return (string) $this->originalName;
240
    }
241
242
    /**
243
     * @param string $originalName
244
     */
245
    public function setOriginalName($originalName): self
246
    {
247
        $this->originalName = $originalName;
248
249
        return $this;
250
    }
251
252
    public function getDimensions(): array
253
    {
254
        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...
255
    }
256
257
    /**
258
     * @param $dimensions
259
     */
260
    public function setDimensions($dimensions): self
261
    {
262
        $this->dimensions = $dimensions;
263
264
        return $this;
265
    }
266
267
    public function getWidth(): int
268
    {
269
        $data = $this->getDimensions();
270
        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...
271
            //$data = explode(',', $data);
272
273
            return (int) $data[0];
274
        }
275
276
        return 0;
277
    }
278
279
    public function getHeight(): int
280
    {
281
        $data = $this->getDimensions();
282
283
        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...
284
            //$data = explode(',', $data);
285
286
            return (int) $data[1];
287
        }
288
289
        return 0;
290
    }
291
292
    public function getMetadata(): array
293
    {
294
        return $this->metadata;
295
    }
296
297
    public function setMetadata(array $metadata): self
298
    {
299
        $this->metadata = $metadata;
300
301
        return $this;
302
    }
303
304
    public function getDescription(): string
305
    {
306
        return $this->description;
307
    }
308
309
    public function setDescription(string $description): self
310
    {
311
        $this->description = $description;
312
313
        return $this;
314
    }
315
316
    public function getFile(): ?File
317
    {
318
        return $this->file;
319
    }
320
321
    public function hasFile()
322
    {
323
        return null !== $this->file;
324
    }
325
326
    /**
327
     * @param File|UploadedFile $file
328
     */
329
    public function setFile(File $file = null): self
330
    {
331
        $this->file = $file;
332
333
        if (null !== $file) {
334
            // It is required that at least one field changes if you are using doctrine
335
            // otherwise the event listeners won't be called and the file is lost
336
            $this->updatedAt = new \DateTimeImmutable();
337
        }
338
339
        return $this;
340
    }
341
342
    public function getTitle(): string
343
    {
344
        return $this->title;
345
    }
346
347
    public function setTitle(string $title): Asset
348
    {
349
        $this->title = $title;
350
351
        return $this;
352
    }
353
354
    public function getCategory(): string
355
    {
356
        return $this->category;
357
    }
358
359
    public function setCategory(string $category): Asset
360
    {
361
        $this->category = $category;
362
363
        return $this;
364
    }
365
366
    public function getCompressed(): bool
367
    {
368
        return $this->compressed;
369
    }
370
371
    public function setCompressed(bool $compressed)
372
    {
373
        $this->compressed = $compressed;
374
375
        return $this;
376
    }
377
}
378