Completed
Push — add/amp-pwa-experiment ( efea12 )
by
unknown
11:53
created

AMP_Vine_Embed_Handler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 42.31 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 22
loc 52
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register_embed() 0 3 1
A unregister_embed() 0 3 1
A get_scripts() 0 7 2
A oembed() 0 3 1
A render() 22 22 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
require_once( AMP__ROOT__ . '/includes/embeds/class-amp-base-embed-handler.php' );
4
5
class AMP_Vine_Embed_Handler extends AMP_Base_Embed_Handler {
6
	const URL_PATTERN = '#https?://vine\.co/v/([^/?]+)#i';
7
8
	protected $DEFAULT_WIDTH = 400;
9
	protected $DEFAULT_HEIGHT = 400;
10
11
	private static $script_slug = 'amp-vine';
12
	private static $script_src = 'https://cdn.ampproject.org/v0/amp-vine-0.1.js';
13
14
	public function register_embed() {
15
		wp_embed_register_handler( 'amp-vine', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
16
	}
17
18
	public function unregister_embed() {
19
		wp_embed_unregister_handler( 'amp-vine', -1 );
20
	}
21
22
	public function get_scripts() {
23
		if ( ! $this->did_convert_elements ) {
24
			return array();
25
		}
26
27
		return array( self::$script_slug => self::$script_src );
28
	}
29
30
	public function oembed( $matches, $attr, $url, $rawattr ) {
31
		return $this->render( array( 'url' => $url, 'vine_id' =>  end( $matches ) ) );
32
	}
33
34 View Code Duplication
	public function render( $args ) {
35
		$args = wp_parse_args( $args, array(
36
			'url' => false,
37
			'vine_id' => false,
38
		) );
39
40
		if ( empty( $args['vine_id'] ) ) {
41
			return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) );
42
		}
43
44
		$this->did_convert_elements = true;
45
46
		return AMP_HTML_Utils::build_tag(
47
			'amp-vine',
48
			array(
49
				'data-vineid' => $args['vine_id'],
50
				'layout' => 'responsive',
51
				'width' => $this->args['width'],
52
				'height' => $this->args['height'],
53
			)
54
		);
55
	}
56
}
57