Media::validateMethod()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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 string|void image( int|string $attachment, string|array $size )
13
 * @method string|void video( string|array $args )
14
 * @method \WP_Query   getGallery( array $args )
15
 * @method object|void getImage( int|string $attachment )
16
 * @method object|void getVideo( 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
     * @return string|void
34
     * @throws BadMethodCallException
35
     */
36
    public function __call($name, array $args)
37
    {
38
        $mediaType = $this->validateMethod($name);
39
        $args = $this->validateArgs($args, $mediaType);
40
41
        if (str_replace($mediaType, '', strtolower($name))) {
42
            return $this->$mediaType->get($args[0])->$mediaType;
43
        }
44
        return !empty($args[1])
45
            ? $this->$mediaType->get($args[0])->render($args[1])
46
            : $this->$mediaType->get($args[0])->render();
47
    }
48
49
    /**
50
     * @param string $name
51
     * @param mixed $args
52
     * @return mixed
53
     * @throws BadMethodCallException
54
     */
55
    public function get($name, $args = [])
56
    {
57
        $mediaType = $this->validateMethod($name);
58
        return $this->$mediaType->get($args)->$mediaType;
59
    }
60
61
    /**
62
     * @param string $name
63
     * @return array
64
     * @throws BadMethodCallException
65
     */
66
    protected function validateArgs(array $args, $name)
67
    {
68
        if (!count($args) && 'image' == $name) {
69
            $args[] = get_post_thumbnail_id();
70
        }
71
        if (count($args)) {
72
            return $args;
73
        }
74
        throw new BadMethodCallException(sprintf('Missing arguments for: %s', $name));
75
    }
76
77
    /**
78
     * @param string $name
79
     * @return string|false
80
     * @throws BadMethodCallException
81
     */
82
    protected function validateMethod($name)
83
    {
84
        foreach ([$name, strtolower(substr($name, 3))] as $method) {
85
            if (property_exists($this, $method) && is_object($this->$method)) {
86
                return $method;
87
            }
88
        }
89
        throw new BadMethodCallException(sprintf('Not a valid method: %s', $name));
90
    }
91
}
92