Completed
Push — develop ( 13bd87...d494ed )
by Paul
02:08
created

Video   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 8.79 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 8
loc 91
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A get() 0 6 1
A renderPlayButton() 0 13 1
A renderScreenshot() 0 8 2
A render() 0 9 2
A setImage() 0 7 2
A setUrl() 0 6 2
A normalize() 0 15 3

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
namespace GeminiLabs\Castor;
4
5
use GeminiLabs\Castor\Helpers\PostMeta;
6
use GeminiLabs\Castor\Helpers\Theme;
7
use GeminiLabs\Castor\Helpers\Utility;
8
use GeminiLabs\Castor\Image;
9
use GeminiLabs\Castor\Oembed;
10
11
class Video
12
{
13
	public $image;
14
	public $postmeta;
15
	public $oembed;
16
	public $theme;
17
	public $utility;
18
19
	protected $args;
20
	protected $video;
21
22 View Code Duplication
	public function __construct( Image $image, Oembed $oembed, PostMeta $postmeta, Theme $theme, Utility $utility )
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
23
	{
24
		$this->image    = $image;
25
		$this->oembed   = $oembed;
26
		$this->postmeta = $postmeta;
27
		$this->theme    = $theme;
28
		$this->utility  = $utility;
29
	}
30
31
	public function get( array $args = [] )
32
	{
33
		$args = $this->normalize( $args );
34
		$this->video = $this->oembed->request( $args['url'], $args['player'] );
35
		return $this;
36
	}
37
38
	public function renderPlayButton()
39
	{
40
		return sprintf(
41
			'<div class="video-play">' .
42
				'<div class="video-play-pulse pulse1"></div>' .
43
				'<div class="video-play-pulse pulse2"></div>' .
44
				'<div class="video-play-pulse pulse3"></div>' .
45
				'<a href="%s" class="video-play-btn">%s</a>' .
46
			'</div>',
47
			$this->args['url'],
48
			$this->theme->svg( 'play.svg' )
49
		);
50
	}
51
52
	public function renderScreenshot()
53
	{
54
		if( !$this->args['image'] )return;
55
		return sprintf( '<div class="video-screenshot" style="background-image: url(%s)">%s</div>',
56
			$this->args['image'],
57
			$this->renderPlayButton()
58
		);
59
	}
60
61
	public function render()
62
	{
63
		if( !isset( $this->video->html ))return;
64
		return sprintf(
65
			'<div class="video embed">%s%s</div>',
66
			$this->renderScreenshot(),
67
			$this->video->html
68
		);
69
	}
70
71
	protected function setImage( $image )
72
	{
73
		$image = $this->image->get( $image );
74
		$this->args['image'] = isset( $image->large )
75
			? $image->large['url']
76
			: null;
77
	}
78
79
	protected function setUrl( $url )
80
	{
81
		$this->args['url'] = !filter_var( $url, FILTER_VALIDATE_URL )
82
			? $this->postmeta->get( $url )
83
			: $url;
84
	}
85
86
	protected function normalize( array $args = [] )
87
	{
88
		$this->args = shortcode_atts([
89
			'image'  => '', // string || int
90
			'player' => '', // string || array
91
			'url'    => '', // string
92
		], $args );
93
94
		foreach( $this->args as $key => $value ) {
95
			$method = $this->utility->buildMethodName( $key, 'set' );
96
			if( !method_exists( $this, $method ))continue;
97
			call_user_func([ $this, $method ], $value );
98
		}
99
		return $this->args;
100
	}
101
}
102