Completed
Push — master ( 5cace7...20fac0 )
by Sam
32:27 queued 20:32
created

EmbedShortcodeProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 10
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_shortcodes() 0 4 1
A handle_shortcode() 0 8 3
B embedForTemplate() 0 19 6
1
<?php
2
3
namespace SilverStripe\Forms\HtmlEditor;
4
5
use Embed\Adapters\Adapter;
6
use Embed\Embed;
7
use ShortcodeHandler;
8
9
/**
10
 * Class EmbedShortcodeProvider
11
 *
12
 * Provider for the [embed] shortcode tag used by the embedding service
13
 * in the HTML Editor field.
14
 * Provides the html needed for the frontend and the editor field itself.
15
 *
16
 *
17
 * @package SilverStripe\Forms\HtmlEditor
18
 */
19
class EmbedShortcodeProvider implements ShortcodeHandler
20
{
21
22
	/**
23
	 * Gets the list of shortcodes provided by this handler
24
	 *
25
	 * @return mixed
26
	 */
27
	public static function get_shortcodes()
28
	{
29
		return array('embed');
30
	}
31
32
	/**
33
	 * Embed shortcode parser from Oembed. This is a temporary workaround.
34
	 * Oembed class has been replaced with the Embed external service.
35
	 *
36
	 * @param $arguments
37
	 * @param $content
38
	 * @param $parser
39
	 * @param $shortcode
40
	 * @param array $extra
41
	 *
42
	 * @return string
43
	 */
44
	public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array()) {
45
		$embed = Embed::create($content, $arguments);
46
		if($embed && $embed instanceof \Embed\Adapters\Adapter) {
47
			return self::embedForTemplate($embed);
48
		} else {
49
			return '<a href="' . $content . '">' . $content . '</a>';
50
		}
51
	}
52
53
	/**
54
	 * @param Adapter $embed
55
	 *
56
	 * @return string
57
	 */
58
	public static function embedForTemplate($embed)
59
	{
60
		switch ($embed->type) {
61
			case 'video':
62
			case 'rich':
63
				if ($embed->extraClass) {
64
					return "<div class='media $embed->extraClass'>$embed->code</div>";
65
				} else {
66
					return "<div class='media'>$embed->code</div>";
67
				}
68
				break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
69
			case 'link':
70
				return '<a class="' . $embed->extraClass . '" href="' . $embed->origin . '">' . $embed->title . '</a>';
71
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
72
			case 'photo':
73
				return "<img src='$embed->url' width='$embed->width' height='$embed->height' class='$embed->extraClass' />";
74
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
75
		}
76
	}
77
}
78