Completed
Push — develop ( 7c5a9c...c5baa0 )
by Paul
02:15
created

Media   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __call() 0 14 4
A get() 0 6 2
B validateMethod() 0 11 5
1
<?php
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
use GeminiLabs\Castor\Gallery;
6
use GeminiLabs\Castor\Image;
7
use GeminiLabs\Castor\Video;
8
use BadMethodCallException;
9
10
/**
11
 * @method string      gallery( array $args )
12
 * @method \WP_Query   getGallery( array $args )
13
 * @method object|void getImage( int|string $attachment )
14
 * @method object|void getVideo( string|array $args )
15
 * @method string|void image( int|string $attachment, string|array $size )
16
 * @method string|void video( string|array $args )
17
 */
18
class Media
19
{
20
	protected $gallery;
21
	protected $image;
22
	protected $video;
23
24
	public function __construct( Gallery $gallery, Image $image, Video $video )
25
	{
26
		$this->gallery = $gallery;
27
		$this->image   = $image;
28
		$this->video   = $video;
29
	}
30
31
	/**
32
	 * @param string $name
33
	 *
34
	 * @return string|void
35
	 * @throws BadMethodCallException
36
	 */
37
	public function __call( $name, array $args )
38
	{
39
		$mediaType = $this->validateMethod( $name );
40
41
		if( !count( $args )) {
42
			throw new BadMethodCallException( sprintf( 'Missing arguments for: %s', $name ));
43
		}
44
		if( str_replace( $mediaType, '', strtolower( $name ))) {
45
			return $this->$mediaType->get( $args[0] )->$mediaType;
46
		}
47
		return !empty( $args[1] )
48
			? $this->$mediaType->get( $args[0] )->render( $args[1] )
49
			: $this->$mediaType->get( $args[0] )->render();
50
	}
51
52
	/**
53
	 * @param string $name
54
	 * @param mixed  $args
55
	 *
56
	 * @return mixed
57
	 */
58
	public function get( $name, $args = [] )
59
	{
60
		if( $mediaType = $this->validateMethod( $name )) {
61
			return $this->$mediaType->get( $args )->$mediaType;
62
		}
63
	}
64
65
	/**
66
	 * @param string $name
67
	 *
68
	 * @return string|false
69
	 */
70
	protected function validateMethod( $name )
71
	{
72
		foreach( [$name, strtolower( substr( $name, 3 ))] as $method ) {
73
			if( in_array( $method, ['gallery', 'image', 'video'] )
74
				&& property_exists( $this, $method )
75
				&& is_object( $this->$method )) {
76
				return $method;
77
			}
78
		}
79
		throw new BadMethodCallException( sprintf( 'Not a valid method: %s', $name ));
80
	}
81
}
82