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