Total Complexity | 47 |
Total Lines | 301 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like EmbedShortcodeProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EmbedShortcodeProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class EmbedShortcodeProvider implements ShortcodeHandler |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * Gets the list of shortcodes provided by this handler |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | public static function get_shortcodes() |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Embed shortcode parser from Oembed. This is a temporary workaround. |
||
43 | * Oembed class has been replaced with the Embed external service. |
||
44 | * |
||
45 | * @param array $arguments |
||
46 | * @param string $content |
||
47 | * @param ShortcodeParser $parser |
||
48 | * @param string $shortcode |
||
49 | * @param array $extra |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = []) |
||
54 | { |
||
55 | // Get service URL |
||
56 | if (!empty($content)) { |
||
57 | $serviceURL = $content; |
||
58 | } elseif (!empty($arguments['url'])) { |
||
59 | $serviceURL = $arguments['url']; |
||
60 | } else { |
||
61 | return ''; |
||
62 | } |
||
63 | |||
64 | $class = $arguments['class'] ?? ''; |
||
65 | $width = $arguments['width'] ?? ''; |
||
66 | $height = $arguments['height'] ?? ''; |
||
67 | |||
68 | // Try to use cached result |
||
69 | $cache = static::getCache(); |
||
70 | $key = static::deriveCacheKey($serviceURL, $class, $width, $height); |
||
71 | try { |
||
72 | if ($cache->has($key)) { |
||
73 | return $cache->get($key); |
||
74 | } |
||
75 | } catch (InvalidArgumentException $e) { |
||
|
|||
76 | } |
||
77 | |||
78 | // See https://github.com/oscarotero/Embed#example-with-all-options for service arguments |
||
79 | $serviceArguments = []; |
||
80 | if (!empty($arguments['width'])) { |
||
81 | $serviceArguments['min_image_width'] = $arguments['width']; |
||
82 | } |
||
83 | if (!empty($arguments['height'])) { |
||
84 | $serviceArguments['min_image_height'] = $arguments['height']; |
||
85 | } |
||
86 | |||
87 | /** @var EmbedResource $embed */ |
||
88 | $embed = Injector::inst()->create(Embeddable::class, $serviceURL); |
||
89 | if (!empty($serviceArguments)) { |
||
90 | $embed->setOptions(array_merge($serviceArguments, (array) $embed->getOptions())); |
||
91 | } |
||
92 | |||
93 | // Allow resolver to be mocked |
||
94 | $dispatcher = null; |
||
95 | if (isset($extra['resolver'])) { |
||
96 | $dispatcher = Injector::inst()->create( |
||
97 | $extra['resolver']['class'], |
||
98 | $serviceURL, |
||
99 | $extra['resolver']['config'] |
||
100 | ); |
||
101 | } elseif (Injector::inst()->has(DispatcherInterface::class)) { |
||
102 | $dispatcher = Injector::inst()->get(DispatcherInterface::class); |
||
103 | } |
||
104 | |||
105 | if ($dispatcher) { |
||
106 | $embed->setDispatcher($dispatcher); |
||
107 | } |
||
108 | |||
109 | // Process embed |
||
110 | try { |
||
111 | $embed = $embed->getEmbed(); |
||
112 | } catch (InvalidUrlException $e) { |
||
113 | $message = (Director::isDev()) |
||
114 | ? $e->getMessage() |
||
115 | : _t(__CLASS__ . '.INVALID_URL', 'There was a problem loading the media.'); |
||
116 | |||
117 | $attr = [ |
||
118 | 'class' => 'ss-media-exception embed' |
||
119 | ]; |
||
120 | |||
121 | $result = HTML::createTag( |
||
122 | 'div', |
||
123 | $attr, |
||
124 | HTML::createTag('p', [], $message) |
||
125 | ); |
||
126 | return $result; |
||
127 | } |
||
128 | |||
129 | // Convert embed object into HTML |
||
130 | if ($embed && $embed instanceof Adapter) { |
||
131 | $result = static::embedForTemplate($embed, $arguments); |
||
132 | } |
||
133 | // Fallback to link to service |
||
134 | if (!$result) { |
||
135 | $result = static::linkEmbed($arguments, $serviceURL, $serviceURL); |
||
136 | } |
||
137 | // Cache result |
||
138 | if ($result) { |
||
139 | try { |
||
140 | $cache->set($key, $result); |
||
141 | } catch (InvalidArgumentException $e) { |
||
142 | } |
||
143 | } |
||
144 | return $result; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param Adapter $embed |
||
149 | * @param array $arguments Additional shortcode params |
||
150 | * @return string |
||
151 | */ |
||
152 | public static function embedForTemplate($embed, $arguments) |
||
153 | { |
||
154 | switch ($embed->getType()) { |
||
155 | case 'video': |
||
156 | case 'rich': |
||
157 | // Attempt to inherit width (but leave height auto) |
||
158 | if (empty($arguments['width']) && $embed->getWidth()) { |
||
159 | $arguments['width'] = $embed->getWidth(); |
||
160 | } |
||
161 | return static::videoEmbed($arguments, $embed->getCode()); |
||
162 | case 'link': |
||
163 | return static::linkEmbed($arguments, $embed->getUrl(), $embed->getTitle()); |
||
164 | case 'photo': |
||
165 | return static::photoEmbed($arguments, $embed->getUrl()); |
||
166 | default: |
||
167 | return null; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Build video embed tag |
||
173 | * |
||
174 | * @param array $arguments |
||
175 | * @param string $content Raw HTML content |
||
176 | * @return string |
||
177 | */ |
||
178 | protected static function videoEmbed($arguments, $content) |
||
179 | { |
||
180 | // Ensure outer div has given width (but leave height auto) |
||
181 | if (!empty($arguments['width'])) { |
||
182 | $arguments['style'] = 'width: ' . intval($arguments['width']) . 'px;'; |
||
183 | } |
||
184 | |||
185 | // override iframe dimension attributes provided by webservice with ones specified in shortcode arguments |
||
186 | foreach (['width', 'height'] as $attr) { |
||
187 | if (!($value = $arguments[$attr] ?? false)) { |
||
188 | continue; |
||
189 | } |
||
190 | foreach (['"', "'"] as $quote) { |
||
191 | $rx = "/(<iframe .*?)$attr=$quote([0-9]+)$quote([^>]+>)/"; |
||
192 | $content = preg_replace($rx, "$1{$attr}={$quote}{$value}{$quote}$3", $content); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | $data = [ |
||
197 | 'Arguments' => $arguments, |
||
198 | 'Attributes' => static::buildAttributeListFromArguments($arguments, ['width', 'height', 'url', 'caption']), |
||
199 | 'Content' => DBField::create_field('HTMLFragment', $content) |
||
200 | ]; |
||
201 | |||
202 | return ArrayData::create($data)->renderWith(self::class . '_video')->forTemplate(); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Build <a> embed tag |
||
207 | * |
||
208 | * @param array $arguments |
||
209 | * @param string $href |
||
210 | * @param string $title Default title |
||
211 | * @return string |
||
212 | */ |
||
213 | protected static function linkEmbed($arguments, $href, $title) |
||
214 | { |
||
215 | $data = [ |
||
216 | 'Arguments' => $arguments, |
||
217 | 'Attributes' => static::buildAttributeListFromArguments($arguments, ['width', 'height', 'url', 'caption']), |
||
218 | 'Href' => $href, |
||
219 | 'Title' => !empty($arguments['caption']) ? ($arguments['caption']) : $title |
||
220 | ]; |
||
221 | |||
222 | return ArrayData::create($data)->renderWith(self::class . '_link')->forTemplate(); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Build img embed tag |
||
227 | * |
||
228 | * @param array $arguments |
||
229 | * @param string $src |
||
230 | * @return string |
||
231 | */ |
||
232 | protected static function photoEmbed($arguments, $src) |
||
233 | { |
||
234 | $data = [ |
||
235 | 'Arguments' => $arguments, |
||
236 | 'Attributes' => static::buildAttributeListFromArguments($arguments, ['url']), |
||
237 | 'Src' => $src |
||
238 | ]; |
||
239 | |||
240 | return ArrayData::create($data)->renderWith(self::class . '_photo')->forTemplate(); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Build a list of HTML attributes from embed arguments - used to preserve backward compatibility |
||
245 | * |
||
246 | * @deprecated 4.5.0 Use {$Arguments.name} directly in shortcode templates to access argument values |
||
247 | * @param array $arguments List of embed arguments |
||
248 | * @param array $exclude List of attribute names to exclude from the resulting list |
||
249 | * @return ArrayList |
||
250 | */ |
||
251 | private static function buildAttributeListFromArguments(array $arguments, array $exclude = []): ArrayList |
||
252 | { |
||
253 | $attributes = ArrayList::create(); |
||
254 | foreach ($arguments as $key => $value) { |
||
255 | if (in_array($key, $exclude)) { |
||
256 | continue; |
||
257 | } |
||
258 | |||
259 | $attributes->push(ArrayData::create([ |
||
260 | 'Name' => $key, |
||
261 | 'Value' => Convert::raw2att($value) |
||
262 | ])); |
||
263 | } |
||
264 | |||
265 | return $attributes; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param ShortcodeParser $parser |
||
270 | * @param string $content |
||
271 | */ |
||
272 | public static function flushCachedShortcodes(ShortcodeParser $parser, string $content): void |
||
295 | } |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @return CacheInterface |
||
301 | */ |
||
302 | private static function getCache(): CacheInterface |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @param string $url |
||
309 | * @return string |
||
310 | */ |
||
311 | private static function deriveCacheKey(string $url, string $class, string $width, string $height): string |
||
319 | ])); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @param string $str |
||
324 | * @return string |
||
325 | */ |
||
326 | private static function cleanKeySegment(string $str): string |
||
329 | } |
||
330 | } |
||
331 |