Completed
Push — master ( 9745cb...81e988 )
by Paul
06:43
created

Media::prePersist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 1
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Victoire\Bundle\MediaBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Gedmo\Timestampable\Traits\TimestampableEntity;
7
8
/**
9
 * Media.
10
 *
11
 * @ORM\Entity(repositoryClass="Victoire\Bundle\MediaBundle\Repository\MediaRepository")
12
 * @ORM\Table(name="vic_media")
13
 * @ORM\HasLifecycleCallbacks
14
 */
15
class Media
16
{
17
    use TimestampableEntity;
18
19
    /**
20
     * @ORM\Id
21
     * @ORM\Column(type="bigint")
22
     * @ORM\GeneratedValue(strategy="AUTO")
23
     */
24
    protected $id;
25
26
    /**
27
     * @var string
28
     *
29
     * @ORM\Column(type="string", unique=true, length=255)
30
     * @ORM\GeneratedValue(strategy="AUTO")
31
     */
32
    protected $uuid;
33
34
    /**
35
     * @var string
36
     *
37
     * @ORM\Column(type="string", nullable=true)
38
     */
39
    protected $name;
40
41
    /**
42
     * @var string
43
     *
44
     * @ORM\Column(type="string", nullable=true, name="original_filename")
45
     */
46
    protected $originalFilename;
47
48
    /**
49
     * @var string
50
     *
51
     * @ORM\Column(type="string", name="location", nullable=true)
52
     */
53
    protected $location;
54
55
    /**
56
     * @var string
57
     *
58
     * @ORM\Column(type="string", name="content_type")
59
     */
60
    protected $contentType;
61
62
    /**
63
     * @var array
64
     *
65
     * @ORM\Column(type="array")
66
     */
67
    protected $metadata = [];
68
69
    /**
70
     * @var Folder
71
     *
72
     * @ORM\ManyToOne(targetEntity="Folder", inversedBy="media")
73
     * @ORM\JoinColumn(name="folder_id", referencedColumnName="id")
74
     */
75
    protected $folder;
76
77
    /**
78
     * @var mixed
79
     */
80
    protected $content;
81
82
    /**
83
     * @var int
84
     *
85
     * @ORM\Column(type="integer", nullable=true)
86
     */
87
    protected $filesize;
88
89
    /**
90
     * @var string
91
     *
92
     * @ORM\Column(type="string", nullable=true)
93
     */
94
    protected $url;
95
96
    /**
97
     * @var bool
98
     *
99
     * @ORM\Column(type="boolean")
100
     */
101
    protected $deleted;
102
103
    /**
104
     * constructor.
105
     */
106
    public function __construct()
107
    {
108
        $this->setCreatedAt(new \DateTime());
109
        $this->setUpdatedAt(new \DateTime());
110
        $this->deleted = false;
111
    }
112
113
    /**
114
     * Return string representation of entity; we return the url display directly the media without using any getter.
115
     *
116
     * @return string
117
     */
118
    public function __toString()
119
    {
120
        return $this->url;
121
    }
122
123
    /**
124
     * Get id.
125
     *
126
     * @return int
127
     */
128
    public function getId()
129
    {
130
        return $this->id;
131
    }
132
133
    /**
134
     * Set id.
135
     *
136
     * @param int $id The unique identifier
137
     */
138
    public function setId($id)
139
    {
140
        $this->id = $id;
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getFileSize()
147
    {
148
        $size = $this->filesize;
149
        if ($size < 1024) {
150
            return $size.'b';
151
        } else {
152
            $help = $size / 1024;
153
            if ($help < 1024) {
154
                return round($help, 1).'kb';
155
            } else {
156
                return round(($help / 1024), 1).'mb';
157
            }
158
        }
159
    }
160
161
    /**
162
     * @param int $filesize
163
     */
164
    public function setFileSize($filesize)
165
    {
166
        $this->filesize = $filesize;
167
    }
168
169
    /**
170
     * Set uuid.
171
     *
172
     * @param string $uuid
173
     */
174
    public function setUuid($uuid)
175
    {
176
        $this->uuid = $uuid;
177
    }
178
179
    /**
180
     * Get uuid.
181
     *
182
     * @return string
183
     */
184
    public function getUuid()
185
    {
186
        return $this->uuid;
187
    }
188
189
    /**
190
     * Set name.
191
     *
192
     * @param string $name
193
     */
194
    public function setName($name)
195
    {
196
        $this->name = $name;
197
    }
198
199
    /**
200
     * Get name.
201
     *
202
     * @return string
203
     */
204
    public function getName()
205
    {
206
        return $this->name;
207
    }
208
209
    /**
210
     * @param string $originalFilename
211
     *
212
     * @return Media
213
     */
214
    public function setOriginalFilename($originalFilename)
215
    {
216
        $this->originalFilename = $originalFilename;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    public function getOriginalFilename()
225
    {
226
        return $this->originalFilename;
227
    }
228
229
    /**
230
     * Set location.
231
     *
232
     * @param string $location
233
     */
234
    public function setLocation($location)
235
    {
236
        $this->location = $location;
237
    }
238
239
    /**
240
     * Get location.
241
     *
242
     * @return string
243
     */
244
    public function getLocation()
245
    {
246
        return $this->location;
247
    }
248
249
    /**
250
     * Set contentType.
251
     *
252
     * @param string $contentType
253
     */
254
    public function setContentType($contentType)
255
    {
256
        $this->contentType = $contentType;
257
    }
258
259
    /**
260
     * Get contentType.
261
     *
262
     * @return string
263
     */
264
    public function getContentType()
265
    {
266
        return $this->contentType;
267
    }
268
269
    /**
270
     * Get contentType.
271
     *
272
     * @return string
273
     */
274
    public function getContentTypeShort()
275
    {
276
        $contentType = $this->contentType;
277
        $array = explode('/', $contentType);
278
        $contentType = end($array);
279
280
        return $contentType;
281
    }
282
283
    /**
284
     * Set metadata.
285
     *
286
     * @param array $metadata
287
     *
288
     * @return Media
289
     */
290
    public function setMetadata($metadata)
291
    {
292
        $this->metadata = $metadata;
293
294
        return $this;
295
    }
296
297
    /**
298
     * Get metadata.
299
     *
300
     * @return array
301
     */
302
    public function getMetadata()
303
    {
304
        return $this->metadata;
305
    }
306
307
    /**
308
     * Set the specified metadata value.
309
     *
310
     * @param string $key
311
     * @param mixed  $value
312
     *
313
     * @return Media
314
     */
315
    public function setMetadataValue($key, $value)
316
    {
317
        $this->metadata[$key] = $value;
318
319
        return $this;
320
    }
321
322
    /**
323
     * Get the specified metadata value.
324
     *
325
     * @param string $key
326
     *
327
     * @return mixed|null
328
     */
329
    public function getMetadataValue($key)
330
    {
331
        return isset($this->metadata[$key]) ? $this->metadata[$key] : null;
332
    }
333
334
    /**
335
     * Set content.
336
     *
337
     * @param mixed $content
338
     *
339
     * @return Media
340
     */
341
    public function setContent($content)
342
    {
343
        $this->content = $content;
344
        $this->setUpdatedAt(new \DateTime());
345
346
        return $this;
347
    }
348
349
    /**
350
     * Get content.
351
     *
352
     * @return mixed
353
     */
354
    public function getContent()
355
    {
356
        return $this->content;
357
    }
358
359
    /**
360
     * Set folder.
361
     *
362
     * @param Folder $folder
363
     *
364
     * @return Media
365
     */
366
    public function setFolder($folder)
367
    {
368
        $this->folder = $folder;
369
370
        return $this;
371
    }
372
373
    /**
374
     * Get folder.
375
     *
376
     * @return Folder
377
     */
378
    public function getFolder()
379
    {
380
        return $this->folder;
381
    }
382
383
    /**
384
     * @return bool
385
     */
386
    public function isDeleted()
387
    {
388
        return $this->deleted;
389
    }
390
391
    /**
392
     * @param bool $deleted
393
     */
394
    public function setDeleted($deleted)
395
    {
396
        $this->deleted = $deleted;
397
    }
398
399
    /**
400
     * @return int
401
     */
402
    public function getDeleted()
403
    {
404
        return $this->deleted;
405
    }
406
407
    /**
408
     * @return string
409
     */
410
    public function getUrl()
411
    {
412
        return $this->url;
413
    }
414
415
    /**
416
     * @param string $url
417
     */
418
    public function setUrl($url)
419
    {
420
        $this->url = $url;
421
    }
422
423
    /**
424
     * @return string
425
     */
426
    public function getClassType()
427
    {
428
        $class = explode('\\', get_class($this));
429
        $classname = end($class);
430
431
        return $classname;
432
    }
433
434
    /**
435
     * @ORM\PrePersist
436
     */
437
    public function prePersist()
438
    {
439
        if (empty($this->name)) {
440
            $this->setName($this->getOriginalFilename());
441
        }
442
    }
443
}
444