1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Helper; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\MediaBundle\Entity\Media; |
6
|
|
|
use Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
8
|
|
|
use Symfony\Component\Mime\MimeTypesInterface; |
9
|
|
|
use Symfony\Component\Validator\Constraints\Image; |
10
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* MediaManager |
14
|
|
|
*/ |
15
|
|
|
class MediaManager |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var AbstractMediaHandler[] |
19
|
|
|
*/ |
20
|
|
|
protected $handlers = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var AbstractMediaHandler |
24
|
|
|
*/ |
25
|
|
|
protected $defaultHandler; |
26
|
|
|
|
27
|
|
|
/** @var MimeTypesInterface */ |
28
|
|
|
private $mimeTypes; |
29
|
|
|
|
30
|
|
|
/** @var ValidatorInterface */ |
31
|
|
|
private $validator; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
private $imageExtensions; |
35
|
|
|
|
36
|
|
|
/** @var array */ |
37
|
|
|
private $allowedExtensions; |
38
|
|
|
|
39
|
|
|
/** @var bool */ |
40
|
|
|
private $limitAllowedExtesions; |
41
|
|
|
|
42
|
14 |
|
public function __construct(MimeTypesInterface $mimeTypes, ValidatorInterface $validator, array $imageExtesions, array $allowedExtensions, bool $limitAllowedExtensions = true) |
43
|
|
|
{ |
44
|
14 |
|
$this->mimeTypes = $mimeTypes; |
45
|
14 |
|
$this->validator = $validator; |
46
|
14 |
|
$this->imageExtensions = $imageExtesions; |
47
|
14 |
|
$this->allowedExtensions = $allowedExtensions; |
48
|
14 |
|
$this->limitAllowedExtesions = $limitAllowedExtensions; |
49
|
14 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param AbstractMediaHandler $handler Media handler |
53
|
|
|
* |
54
|
|
|
* @return MediaManager |
55
|
|
|
*/ |
56
|
10 |
|
public function addHandler(AbstractMediaHandler $handler) |
57
|
|
|
{ |
58
|
10 |
|
$this->handlers[$handler->getName()] = $handler; |
59
|
|
|
|
60
|
10 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param AbstractMediaHandler $handler Media handler |
65
|
|
|
* |
66
|
|
|
* @return MediaManager |
67
|
|
|
*/ |
68
|
14 |
|
public function setDefaultHandler(AbstractMediaHandler $handler) |
69
|
|
|
{ |
70
|
14 |
|
$this->defaultHandler = $handler; |
71
|
|
|
|
72
|
14 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns handler with the highest priority to handle the Media item which can handle the item. If no handler is found, it returns FileHandler |
77
|
|
|
* |
78
|
|
|
* @param Media|File $media |
79
|
|
|
* |
80
|
|
|
* @return AbstractMediaHandler |
81
|
|
|
*/ |
82
|
10 |
View Code Duplication |
public function getHandler($media) |
|
|
|
|
83
|
|
|
{ |
84
|
10 |
|
$bestHandler = $this->defaultHandler; |
85
|
10 |
|
foreach ($this->handlers as $handler) { |
86
|
6 |
|
if ($handler->canHandle($media) && $handler->getPriority() > $bestHandler->getPriority()) { |
87
|
6 |
|
$bestHandler = $handler; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
10 |
|
return $bestHandler; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns handler with the highest priority to handle the Media item based on the Type. If no handler is found, it returns FileHandler |
96
|
|
|
* |
97
|
|
|
* @param string $type |
98
|
|
|
* |
99
|
|
|
* @return AbstractMediaHandler |
100
|
|
|
*/ |
101
|
1 |
View Code Duplication |
public function getHandlerForType($type) |
|
|
|
|
102
|
|
|
{ |
103
|
1 |
|
$bestHandler = $this->defaultHandler; |
104
|
1 |
|
foreach ($this->handlers as $handler) { |
105
|
1 |
|
if ($handler->getType() == $type && $handler->getPriority() > $bestHandler->getPriority()) { |
106
|
1 |
|
$bestHandler = $handler; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $bestHandler; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return AbstractMediaHandler[] |
115
|
|
|
*/ |
116
|
1 |
|
public function getHandlers() |
117
|
|
|
{ |
118
|
1 |
|
return $this->handlers; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return MediaManager |
123
|
|
|
*/ |
124
|
2 |
|
public function prepareMedia(Media $media) |
125
|
|
|
{ |
126
|
2 |
|
$handler = $this->getHandler($media); |
127
|
2 |
|
$handler->prepareMedia($media); |
128
|
|
|
|
129
|
2 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param Media $media The media |
134
|
|
|
* @param bool $new Is new |
135
|
|
|
*/ |
136
|
3 |
|
public function saveMedia(Media $media, $new = false) |
137
|
|
|
{ |
138
|
3 |
|
$handler = $this->getHandler($media); |
139
|
|
|
|
140
|
3 |
|
if ($new) { |
141
|
2 |
|
$handler->saveMedia($media); |
142
|
|
|
} else { |
143
|
2 |
|
$handler->updateMedia($media); |
144
|
|
|
} |
145
|
3 |
|
} |
146
|
|
|
|
147
|
2 |
|
public function removeMedia(Media $media) |
148
|
|
|
{ |
149
|
2 |
|
$handler = $this->getHandler($media); |
150
|
2 |
|
$handler->removeMedia($media); |
151
|
2 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param mixed $data |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
1 |
|
public function createNew($data) |
159
|
|
|
{ |
160
|
1 |
|
if ($this->isExtensionAllowed($data)) { |
161
|
1 |
|
foreach ($this->handlers as $handler) { |
162
|
1 |
|
$result = $handler->createNew($data); |
163
|
1 |
|
if ($result) { |
164
|
1 |
|
return $result; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
1 |
|
public function getFolderAddActions() |
176
|
|
|
{ |
177
|
1 |
|
$result = []; |
178
|
1 |
|
foreach ($this->handlers as $handler) { |
179
|
1 |
|
$actions = $handler->getAddFolderActions(); |
180
|
1 |
|
if ($actions) { |
|
|
|
|
181
|
1 |
|
$result = array_merge($actions, $result); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
return $result; |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
public function isExtensionAllowed(File $file): bool |
189
|
|
|
{ |
190
|
1 |
|
if (!$this->limitAllowedExtesions) { |
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
$mimeType = $this->mimeTypes->guessMimeType($file->getRealPath()); |
195
|
1 |
|
$extensions = $this->mimeTypes->getExtensions($mimeType); |
196
|
1 |
|
$extension = array_shift($extensions); |
197
|
1 |
|
if (!in_array($extension, $this->allowedExtensions, true)) { |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
if (!in_array($extension, $this->imageExtensions, true)) { |
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
1 |
|
$errors = $this->validator->validate($file, new Image(['detectCorrupted' => true])); |
206
|
|
|
|
207
|
1 |
|
return 0 === $errors->count(); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
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.