Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

src/Kunstmaan/MediaBundle/Entity/Media.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MediaBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Gedmo\Mapping\Annotation as Gedmo;
7
use Kunstmaan\AdminBundle\Entity\AbstractEntity;
8
9
/**
10
 * Media
11
 *
12
 * @ORM\Entity(repositoryClass="Kunstmaan\MediaBundle\Repository\MediaRepository")
13
 * @ORM\Table(name="kuma_media", indexes={
14
 *      @ORM\Index(name="idx_media_name", columns={"name"}),
15
 *      @ORM\Index(name="idx_media_deleted", columns={"deleted"})
16
 * })
17
 * @ORM\HasLifecycleCallbacks
18
 */
19
class Media extends AbstractEntity
20
{
21
    /**
22
     * @var string
23
     *
24
     * @Gedmo\Locale
25
     * Used locale to override Translation listener`s locale
26
     * this is not a mapped field of entity metadata, just a simple property
27
     */
28
    protected $locale;
29
30
    /**
31
     * @var string
32
     *
33
     * @ORM\Column(type="string", unique=true, length=255)
34
     * @ORM\GeneratedValue(strategy="AUTO")
35
     */
36
    protected $uuid;
37
38
    /**
39
     * @var string
40
     *
41
     * @ORM\Column(type="string", nullable=true)
42
     */
43
    protected $name;
44
45
    /**
46
     * @var string
47
     *
48
     * @ORM\Column(name="description", type="text", nullable=true)
49
     * @Gedmo\Translatable
50
     */
51
    protected $description;
52
53
    /**
54
     * @var string
55
     *
56
     * @ORM\Column(name="copyright", type="string", nullable=true)
57
     * @Gedmo\Translatable
58
     */
59
    protected $copyright;
60
61
    /**
62
     * @var string
63
     *
64
     * @ORM\Column(type="string", name="location", nullable=true)
65
     */
66
    protected $location;
67
68
    /**
69
     * @var string
70
     *
71
     * @ORM\Column(type="string", name="content_type")
72
     */
73
    protected $contentType;
74
75
    /**
76
     * @var array
77
     *
78
     * @ORM\Column(type="array")
79
     */
80
    protected $metadata = array();
81
82
    /**
83
     * @var \DateTime
84
     *
85
     * @ORM\Column(type="datetime", name="created_at")
86
     */
87
    protected $createdAt;
88
89
    /**
90
     * @var \DateTime
91
     *
92
     * @ORM\Column(type="datetime", name="updated_at")
93
     */
94
    protected $updatedAt;
95
96
    /**
97
     * @var Folder
98
     *
99
     * @ORM\ManyToOne(targetEntity="Folder", inversedBy="media")
100
     * @ORM\JoinColumn(name="folder_id", referencedColumnName="id")
101
     */
102
    protected $folder;
103
104
    /**
105
     * @var mixed
106
     */
107
    protected $content;
108
109
    /**
110
     * @var int
111
     *
112
     * @ORM\Column(type="integer", nullable=true)
113
     */
114
    protected $filesize;
115
116
    /**
117
     * @var string
118
     *
119
     * @ORM\Column(type="string", nullable=true)
120
     */
121
    protected $url;
122
123
    /**
124
     * @var string
125
     *
126
     * @ORM\Column(type="string", nullable=true, name="original_filename")
127
     */
128
    protected $originalFilename;
129
130
    /**
131
     * @var bool
132
     *
133
     * @ORM\Column(type="boolean")
134
     */
135
    protected $deleted;
136
137
    /**
138
     * @var bool
139
     *
140
     * @ORM\Column(type="boolean", name="removed_from_file_system")
141
     */
142
    protected $removedFromFileSystem;
143
144
    /**
145
     * constructor
146
     */
147 View Code Duplication
    public function __construct()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        $this->setCreatedAt(new \DateTime());
150
        $this->setUpdatedAt(new \DateTime());
151
        $this->deleted = false;
152
        $this->removedFromFileSystem = false;
153
    }
154
155
    /**
156
     * @param string $locale
157
     *
158
     * @return Media
159
     */
160
    public function setTranslatableLocale($locale)
161
    {
162
        $this->locale = $locale;
163
164
        return $this;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getFileSize()
171
    {
172
        $size = $this->filesize;
173
        if ($size === null) {
174
            return '';
175
        }
176
177
        if ($size < 1024) {
178
            return $size . 'b';
179
        } else {
180
            $help = $size / 1024;
181
            if ($help < 1024) {
182
                return round($help, 1) . 'kb';
183
            } else {
184
                return round(($help / 1024), 1) . 'mb';
185
            }
186
        }
187
    }
188
189
    /**
190
     * @return int
191
     */
192
    public function getFileSizeBytes()
193
    {
194
        return $this->filesize;
195
    }
196
197
    /**
198
     * @param int $filesize
199
     *
200
     * @return Media
201
     */
202
    public function setFileSize($filesize)
203
    {
204
        $this->filesize = $filesize;
205
206
        return $this;
207
    }
208
209
    /**
210
     * Set uuid
211
     *
212
     * @param string $uuid
213
     *
214
     * @return Media
215
     */
216
    public function setUuid($uuid)
217
    {
218
        $this->uuid = $uuid;
219
220
        return $this;
221
    }
222
223
    /**
224
     * Get uuid
225
     *
226
     * @return string
227
     */
228
    public function getUuid()
229
    {
230
        return $this->uuid;
231
    }
232
233
    /**
234
     * Set name
235
     *
236
     * @param string $name
237
     *
238
     * @return Media
239
     */
240
    public function setName($name)
241
    {
242
        $this->name = $name;
243
244
        return $this;
245
    }
246
247
    /**
248
     * Get name
249
     *
250
     * @return string
251
     */
252
    public function getName()
253
    {
254
        return $this->name;
255
    }
256
257
    /**
258
     * Set location
259
     *
260
     * @param string $location
261
     *
262
     * @return Media
263
     */
264
    public function setLocation($location)
265
    {
266
        $this->location = $location;
267
268
        return $this;
269
    }
270
271
    /**
272
     * Get location
273
     *
274
     * @return string
275
     */
276
    public function getLocation()
277
    {
278
        return $this->location;
279
    }
280
281
    /**
282
     * Set contentType
283
     *
284
     * @param string $contentType
285
     *
286
     * @return Media
287
     */
288
    public function setContentType($contentType)
289
    {
290
        $this->contentType = $contentType;
291
292
        return $this;
293
    }
294
295
    /**
296
     * Get contentType
297
     *
298
     * @return string
299
     */
300
    public function getContentType()
301
    {
302
        return $this->contentType;
303
    }
304
305
    /**
306
     * Get contentType
307
     *
308
     * @return string
309
     */
310
    public function getContentTypeShort()
311
    {
312
        $contentType = $this->contentType;
313
        $array = explode('/', $contentType);
314
        $contentType = end($array);
315
316
        return $contentType;
317
    }
318
319
    /**
320
     * Set metadata
321
     *
322
     * @param array $metadata
323
     *
324
     * @return Media
325
     */
326
    public function setMetadata($metadata)
327
    {
328
        $this->metadata = $metadata;
329
330
        return $this;
331
    }
332
333
    /**
334
     * Get metadata
335
     *
336
     * @return array
337
     */
338
    public function getMetadata()
339
    {
340
        return $this->metadata;
341
    }
342
343
    /**
344
     * Set the specified metadata value
345
     *
346
     * @param string $key
347
     * @param mixed  $value
348
     *
349
     * @return Media
350
     */
351
    public function setMetadataValue($key, $value)
352
    {
353
        $this->metadata[$key] = $value;
354
355
        return $this;
356
    }
357
358
    /**
359
     * Get the specified metadata value
360
     *
361
     * @param string $key
362
     *
363
     * @return mixed|null
364
     */
365
    public function getMetadataValue($key)
366
    {
367
        return isset($this->metadata[$key]) ? $this->metadata[$key] : null;
368
    }
369
370
    /**
371
     * Set createdAt
372
     *
373
     * @param \DateTime $createdAt
374
     *
375
     * @return Media
376
     */
377
    public function setCreatedAt($createdAt)
378
    {
379
        $this->createdAt = $createdAt;
380
381
        return $this;
382
    }
383
384
    /**
385
     * Get createdAt
386
     *
387
     * @return \DateTime
388
     */
389
    public function getCreatedAt()
390
    {
391
        return $this->createdAt;
392
    }
393
394
    /**
395
     * Set updatedAt
396
     *
397
     * @param \DateTime $updatedAt
398
     *
399
     * @return Media
400
     */
401
    public function setUpdatedAt($updatedAt)
402
    {
403
        $this->updatedAt = $updatedAt;
404
405
        return $this;
406
    }
407
408
    /**
409
     * Get updatedAt
410
     *
411
     * @return \DateTime
412
     */
413
    public function getUpdatedAt()
414
    {
415
        return $this->updatedAt;
416
    }
417
418
    /**
419
     * Set content
420
     *
421
     * @param mixed $content
422
     *
423
     * @return Media
424
     */
425
    public function setContent($content)
426
    {
427
        $this->content = $content;
428
        $this->setUpdatedAt(new \DateTime());
429
430
        return $this;
431
    }
432
433
    /**
434
     * Get content
435
     *
436
     * @return mixed
437
     */
438
    public function getContent()
439
    {
440
        return $this->content;
441
    }
442
443
    /**
444
     * Set folder
445
     *
446
     * @param Folder $folder
447
     *
448
     * @return Media
449
     */
450
    public function setFolder(Folder $folder)
451
    {
452
        $this->folder = $folder;
453
454
        return $this;
455
    }
456
457
    /**
458
     * Get folder
459
     *
460
     * @return Folder
461
     */
462
    public function getFolder()
463
    {
464
        return $this->folder;
465
    }
466
467
    /**
468
     * @return bool
469
     */
470
    public function isDeleted()
471
    {
472
        return $this->deleted;
473
    }
474
475
    /**
476
     * @param bool $deleted
477
     *
478
     * @return Media
479
     */
480
    public function setDeleted($deleted)
481
    {
482
        $this->deleted = $deleted;
483
484
        return $this;
485
    }
486
487
    /**
488
     * @return string
489
     */
490
    public function getUrl()
491
    {
492
        return $this->url;
493
    }
494
495
    /**
496
     * @param string $url
497
     *
498
     * @return Media
499
     */
500
    public function setUrl($url)
501
    {
502
        $this->url = $url;
503
504
        return $this;
505
    }
506
507
    /**
508
     * @param string $copyright
509
     *
510
     * @return Media
511
     */
512
    public function setCopyright($copyright)
513
    {
514
        $this->copyright = $copyright;
515
516
        return $this;
517
    }
518
519
    /**
520
     * @return string
521
     */
522
    public function getCopyright()
523
    {
524
        return $this->copyright;
525
    }
526
527
    /**
528
     * @param string $originalFilename
529
     *
530
     * @return Media
531
     */
532
    public function setOriginalFilename($originalFilename)
533
    {
534
        $this->originalFilename = $originalFilename;
535
536
        return $this;
537
    }
538
539
    /**
540
     * @return string
541
     */
542
    public function getOriginalFilename()
543
    {
544
        return $this->originalFilename;
545
    }
546
547
    /**
548
     * @param string $description
549
     *
550
     * @return Media
551
     */
552
    public function setDescription($description)
553
    {
554
        $this->description = $description;
555
556
        return $this;
557
    }
558
559
    /**
560
     * @return string
561
     */
562
    public function getDescription()
563
    {
564
        return $this->description;
565
    }
566
567
    /**
568
     * @return bool
569
     */
570
    public function isRemovedFromFileSystem()
571
    {
572
        return $this->removedFromFileSystem;
573
    }
574
575
    /**
576
     * @param bool $removedFromFileSystem
577
     */
578
    public function setRemovedFromFileSystem($removedFromFileSystem)
579
    {
580
        $this->removedFromFileSystem = $removedFromFileSystem;
581
    }
582
583
    /**
584
     * @ORM\PreUpdate
585
     */
586
    public function preUpdate()
587
    {
588
        $this->setUpdatedAt(new \DateTime());
589
    }
590
591
    /**
592
     * @ORM\PrePersist
593
     */
594
    public function prePersist()
595
    {
596
        if (empty($this->name)) {
597
            $this->setName($this->getOriginalFilename());
598
        }
599
    }
600
}
601