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