| Conditions | 18 |
| Paths | 4035 |
| Total Lines | 88 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 54 | public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = []) |
||
| 55 | { |
||
| 56 | // Get service URL |
||
| 57 | if (!empty($content)) { |
||
| 58 | $serviceURL = $content; |
||
| 59 | } elseif (!empty($arguments['url'])) { |
||
| 60 | $serviceURL = $arguments['url']; |
||
| 61 | } else { |
||
| 62 | return ''; |
||
| 63 | } |
||
| 64 | |||
| 65 | // Try to use cached result |
||
| 66 | $cache = static::getCache(); |
||
| 67 | $key = static::deriveCacheKey($serviceURL); |
||
| 68 | try { |
||
| 69 | if ($cache->has($key)) { |
||
| 70 | return $cache->get($key); |
||
| 71 | } |
||
| 72 | } catch (InvalidArgumentException $e) { |
||
|
|
|||
| 73 | } |
||
| 74 | |||
| 75 | // See https://github.com/oscarotero/Embed#example-with-all-options for service arguments |
||
| 76 | $serviceArguments = []; |
||
| 77 | if (!empty($arguments['width'])) { |
||
| 78 | $serviceArguments['min_image_width'] = $arguments['width']; |
||
| 79 | } |
||
| 80 | if (!empty($arguments['height'])) { |
||
| 81 | $serviceArguments['min_image_height'] = $arguments['height']; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** @var EmbedResource $embed */ |
||
| 85 | $embed = Injector::inst()->create(Embeddable::class, $serviceURL); |
||
| 86 | if (!empty($serviceArguments)) { |
||
| 87 | $embed->setOptions(array_merge($serviceArguments, (array) $embed->getOptions())); |
||
| 88 | } |
||
| 89 | |||
| 90 | // Allow resolver to be mocked |
||
| 91 | $dispatcher = null; |
||
| 92 | if (isset($extra['resolver'])) { |
||
| 93 | $dispatcher = Injector::inst()->create( |
||
| 94 | $extra['resolver']['class'], |
||
| 95 | $serviceURL, |
||
| 96 | $extra['resolver']['config'] |
||
| 97 | ); |
||
| 98 | } elseif (Injector::inst()->has(DispatcherInterface::class)) { |
||
| 99 | $dispatcher = Injector::inst()->get(DispatcherInterface::class); |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($dispatcher) { |
||
| 103 | $embed->setDispatcher($dispatcher); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Process embed |
||
| 107 | try { |
||
| 108 | $embed = $embed->getEmbed(); |
||
| 109 | } catch (InvalidUrlException $e) { |
||
| 110 | $message = (Director::isDev()) |
||
| 111 | ? $e->getMessage() |
||
| 112 | : _t(__CLASS__ . '.INVALID_URL', 'There was a problem loading the media.'); |
||
| 113 | |||
| 114 | $attr = [ |
||
| 115 | 'class' => 'ss-media-exception embed' |
||
| 116 | ]; |
||
| 117 | |||
| 118 | $result = HTML::createTag( |
||
| 119 | 'div', |
||
| 120 | $attr, |
||
| 121 | HTML::createTag('p', [], $message) |
||
| 122 | ); |
||
| 123 | return $result; |
||
| 124 | } |
||
| 125 | |||
| 126 | // Convert embed object into HTML |
||
| 127 | if ($embed && $embed instanceof Adapter) { |
||
| 128 | $result = static::embedForTemplate($embed, $arguments); |
||
| 129 | } |
||
| 130 | // Fallback to link to service |
||
| 131 | if (!$result) { |
||
| 132 | $result = static::linkEmbed($arguments, $serviceURL, $serviceURL); |
||
| 133 | } |
||
| 134 | // Cache result |
||
| 135 | if ($result) { |
||
| 136 | try { |
||
| 137 | $cache->set($key, $result); |
||
| 138 | } catch (InvalidArgumentException $e) { |
||
| 139 | } |
||
| 140 | } |
||
| 141 | return $result; |
||
| 142 | } |
||
| 311 |