1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\MediaBundle\Helper; |
4
|
|
|
|
5
|
|
|
use Victoire\Bundle\MediaBundle\Entity\Media; |
6
|
|
|
use Victoire\Bundle\MediaBundle\Helper\File\FileHandler; |
7
|
|
|
use Victoire\Bundle\MediaBundle\Helper\Media\AbstractMediaHandler; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* MediaManager. |
11
|
|
|
*/ |
12
|
|
|
class MediaManager |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var AbstractMediaHandler[] |
16
|
|
|
*/ |
17
|
|
|
protected $handlers = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param AbstractMediaHandler $handler Media handler |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function addHandler(AbstractMediaHandler $handler) |
25
|
|
|
{ |
26
|
|
|
$this->handlers[$handler->getName()] = $handler; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns handler to handle the Media item which can handle the item. If no handler is found, it returns FileHandler. |
31
|
|
|
* |
32
|
|
|
* @param Media $media |
33
|
|
|
* |
34
|
|
|
* @return AbstractMediaHandler |
35
|
|
|
*/ |
36
|
|
|
public function getHandler($media) |
37
|
|
|
{ |
38
|
|
|
foreach ($this->handlers as $handler) { |
39
|
|
|
if ($handler->canHandle($media)) { |
40
|
|
|
return $handler; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return new FileHandler(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns handler to handle the Media item based on the Type. If no handler is found, it returns FileHandler. |
49
|
|
|
* |
50
|
|
|
* @param string $type |
51
|
|
|
* |
52
|
|
|
* @return AbstractMediaHandler |
53
|
|
|
*/ |
54
|
|
|
public function getHandlerForType($type) |
55
|
|
|
{ |
56
|
|
|
foreach ($this->handlers as $handler) { |
57
|
|
|
if ($handler->getType() == $type) { |
58
|
|
|
return $handler; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return new FileHandler(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return AbstractMediaHandler[] |
67
|
|
|
*/ |
68
|
|
|
public function getHandlers() |
69
|
|
|
{ |
70
|
|
|
return $this->handlers; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \Victoire\Bundle\MediaBundle\Entity\Media $media |
75
|
|
|
*/ |
76
|
|
|
public function prepareMedia(Media $media) |
77
|
|
|
{ |
78
|
|
|
$handler = $this->getHandler($media); |
79
|
|
|
$handler->prepareMedia($media); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Media $media The media |
84
|
|
|
* @param bool $new Is new |
85
|
|
|
*/ |
86
|
|
|
public function saveMedia(Media $media, $new = false) |
87
|
|
|
{ |
88
|
|
|
$handler = $this->getHandler($media); |
89
|
|
|
|
90
|
|
|
if ($new) { |
91
|
|
|
$handler->saveMedia($media); |
92
|
|
|
} else { |
93
|
|
|
$handler->updateMedia($media); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \Victoire\Bundle\MediaBundle\Entity\Media $media |
99
|
|
|
*/ |
100
|
|
|
public function removeMedia(Media $media) |
101
|
|
|
{ |
102
|
|
|
$handler = $this->getHandler($media); |
103
|
|
|
$handler->removeMedia($media); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $data |
108
|
|
|
* |
109
|
|
|
* @return Media |
110
|
|
|
*/ |
111
|
|
|
public function createNew($data) |
112
|
|
|
{ |
113
|
|
|
foreach ($this->handlers as $handler) { |
114
|
|
|
$result = $handler->createNew($data); |
115
|
|
|
if ($result) { |
116
|
|
|
return $result; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function getFolderAddActions() |
125
|
|
|
{ |
126
|
|
|
$result = []; |
127
|
|
|
foreach ($this->handlers as $handler) { |
128
|
|
|
$actions = $handler->getAddFolderActions(); |
129
|
|
|
if ($actions) { |
|
|
|
|
130
|
|
|
$result = array_merge($actions, $result); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.