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\RemoteSlide; |
||
4 | |||
5 | use Kunstmaan\MediaBundle\Entity\Media; |
||
6 | use Kunstmaan\MediaBundle\Form\RemoteSlide\RemoteSlideType; |
||
7 | use Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler; |
||
8 | |||
9 | /** |
||
10 | * RemoteSlideStrategy |
||
11 | */ |
||
12 | class RemoteSlideHandler extends AbstractMediaHandler |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | const CONTENT_TYPE = 'remote/slide'; |
||
18 | |||
19 | const TYPE = 'slide'; |
||
20 | |||
21 | /** |
||
22 | * @return string |
||
23 | */ |
||
24 | public function getName() |
||
25 | { |
||
26 | return 'Remote Slide Handler'; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @return string |
||
31 | */ |
||
32 | public function getType() |
||
33 | { |
||
34 | return RemoteSlideHandler::TYPE; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @return string |
||
39 | */ |
||
40 | public function getFormType() |
||
41 | { |
||
42 | return RemoteSlideType::class; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param mixed $object |
||
47 | * |
||
48 | * @return bool |
||
49 | */ |
||
50 | View Code Duplication | public function canHandle($object) |
|
0 ignored issues
–
show
|
|||
51 | { |
||
52 | if ( |
||
53 | (is_string($object)) || |
||
54 | ($object instanceof Media && $object->getContentType() == RemoteSlideHandler::CONTENT_TYPE) |
||
55 | ) { |
||
56 | return true; |
||
57 | } |
||
58 | |||
59 | return false; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param Media $media |
||
64 | * |
||
65 | * @return RemoteSlideHelper |
||
66 | */ |
||
67 | public function getFormHelper(Media $media) |
||
68 | { |
||
69 | return new RemoteSlideHelper($media); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param Media $media |
||
74 | * |
||
75 | * @throws \RuntimeException when the file does not exist |
||
76 | */ |
||
77 | public function prepareMedia(Media $media) |
||
78 | { |
||
79 | if (null === $media->getUuid()) { |
||
80 | $uuid = uniqid(); |
||
81 | $media->setUuid($uuid); |
||
82 | } |
||
83 | $slide = new RemoteSlideHelper($media); |
||
84 | $code = $slide->getCode(); |
||
85 | // update thumbnail |
||
86 | switch ($slide->getType()) { |
||
87 | case 'slideshare': |
||
88 | try { |
||
89 | $json = json_decode( |
||
90 | file_get_contents( |
||
91 | 'https://www.slideshare.net/api/oembed/2?url=https://www.slideshare.net/slideshow/embed_code/key/' . $code . '&format=json' |
||
92 | ) |
||
93 | ); |
||
94 | $slide->setThumbnailUrl($json->thumbnail); |
||
95 | } catch (\ErrorException $e) { |
||
96 | // Silent exception - should not bubble up since failure to create a thumbnail is not a fatal error |
||
97 | } |
||
98 | |||
99 | break; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param Media $media |
||
105 | */ |
||
106 | public function saveMedia(Media $media) |
||
107 | { |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param Media $media |
||
112 | */ |
||
113 | public function removeMedia(Media $media) |
||
114 | { |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function updateMedia(Media $media) |
||
121 | { |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param array $params |
||
126 | * |
||
127 | * @return array |
||
0 ignored issues
–
show
|
|||
128 | */ |
||
129 | public function getAddUrlFor(array $params = array()) |
||
130 | { |
||
131 | return array( |
||
132 | 'slide' => array( |
||
133 | 'path' => 'KunstmaanMediaBundle_folder_slidecreate', |
||
134 | 'params' => array( |
||
135 | 'folderId' => $params['folderId'], |
||
136 | ), |
||
137 | ), |
||
138 | ); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param mixed $data |
||
143 | * |
||
144 | * @return Media |
||
0 ignored issues
–
show
|
|||
145 | */ |
||
146 | public function createNew($data) |
||
147 | { |
||
148 | $result = null; |
||
149 | if (is_string($data)) { |
||
150 | if (strpos($data, 'http') !== 0) { |
||
151 | $data = 'https://' . $data; |
||
152 | } |
||
153 | $parsedUrl = parse_url($data); |
||
154 | switch ($parsedUrl['host']) { |
||
155 | case 'www.slideshare.net': |
||
156 | case 'slideshare.net': |
||
157 | $result = new Media(); |
||
158 | $slide = new RemoteSlideHelper($result); |
||
159 | $slide->setType('slideshare'); |
||
160 | $json = json_decode( |
||
161 | file_get_contents('https://www.slideshare.net/api/oembed/2?url=' . $data . '&format=json') |
||
162 | ); |
||
163 | $slide->setCode($json->{'slideshow_id'}); |
||
164 | $result = $slide->getMedia(); |
||
165 | $result->setName('SlideShare ' . $data); |
||
166 | |||
167 | break; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | return $result; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function getShowTemplate(Media $media) |
||
178 | { |
||
179 | return 'KunstmaanMediaBundle:Media\RemoteSlide:show.html.twig'; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param Media $media The media entity |
||
184 | * @param string $basepath The base path |
||
185 | * |
||
186 | * @return string |
||
0 ignored issues
–
show
|
|||
187 | */ |
||
188 | public function getImageUrl(Media $media, $basepath) |
||
189 | { |
||
190 | $helper = new RemoteSlideHelper($media); |
||
191 | |||
192 | return $helper->getThumbnailUrl(); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return array |
||
0 ignored issues
–
show
|
|||
197 | */ |
||
198 | public function getAddFolderActions() |
||
199 | { |
||
200 | return array( |
||
201 | RemoteSlideHandler::TYPE => array( |
||
202 | 'type' => RemoteSlideHandler::TYPE, |
||
203 | 'name' => 'media.slide.add', |
||
204 | ), |
||
205 | ); |
||
206 | } |
||
207 | } |
||
208 |
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.