Completed
Push — develop ( e426e7...538b88 )
by Paul
02:06
created

Media::__call()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 2
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
		if( !$mediaType ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mediaType of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
41
			throw new BadMethodCallException( sprintf( 'Not a valid method: %s', $name ));
42
		}
43
		if( !count( $args )) {
44
			throw new BadMethodCallException( sprintf( 'Missing arguments for: %s', $name ));
45
		}
46
		if( str_replace( $mediaType, '', strtolower( $name ))) {
47
			return $this->$mediaType->get( $args[0] )->$mediaType;
48
		}
49
		return !empty( $args[1] )
50
			? $this->$mediaType->get( $args[0] )->render( $args[1] )
51
			: $this->$mediaType->get( $args[0] )->render();
52
	}
53
54
	/**
55
	 * @param string $name
56
	 * @param mixed  $args
57
	 *
58
	 * @return mixed
59
	 */
60
	public function get( $name, $args = [] )
61
	{
62
		if( $mediaType = $this->validateMethod( $name )) {
63
			return $this->$mediaType->get( $args )->$mediaType;
64
		}
65
	}
66
67
	/**
68
	 * @param string $name
69
	 *
70
	 * @return string|false
71
	 */
72
	protected function validateMethod( $name )
73
	{
74
		foreach( [$name, strtolower( substr( $name, 3 ))] as $method ) {
75
			if( in_array( $method, ['gallery', 'image', 'video'] )
76
				&& property_exists( $this, $method )
77
				&& is_object( $this->$method )) {
78
				return $method;
79
			}
80
		}
81
		return false;
82
	}
83
}
84