Video::setImage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
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
9
class Video
10
{
11
    public $video;
12
13
    protected $args;
14
    protected $image;
15
    protected $oembed;
16
    protected $postmeta;
17
    protected $supported = ['youtube'];
18
    protected $theme;
19
    protected $utility;
20
21
    public function __construct(Image $image, Oembed $oembed, PostMeta $postmeta, Theme $theme, Utility $utility)
22
    {
23
        $this->image = $image;
24
        $this->oembed = $oembed;
25
        $this->postmeta = $postmeta;
26
        $this->theme = $theme;
27
        $this->utility = $utility;
28
    }
29
30
    public function get($args = [])
31
    {
32
        $args = $this->normalize($args);
33
        $embed = $this->oembed->request($args['url'], $args['player']);
34
        if (isset($embed->type) && 'video' == $embed->type) {
35
            $this->video = $embed;
36
        }
37
        return $this;
38
    }
39
40
    public function render()
41
    {
42
        if (!isset($this->video->html)) {
43
            return;
44
        }
45
        return sprintf(
46
            '<div class="video embed">%s%s</div>',
47
            $this->renderScreenshot(),
48
            $this->video->html
49
        );
50
    }
51
52
    public function renderPlayButton()
53
    {
54
        return sprintf(
55
            '<div class="video-play">'.
56
                '<div class="video-play-pulse pulse1"></div>'.
57
                '<div class="video-play-pulse pulse2"></div>'.
58
                '<div class="video-play-pulse pulse3"></div>'.
59
                '<a href="%s" class="video-play-btn">%s</a>'.
60
            '</div>',
61
            $this->args['url'],
62
            $this->theme->svg('play.svg')
63
        );
64
    }
65
66
    public function renderScreenshot()
67
    {
68
        if ($this->args['image']
69
            && in_array(strtolower($this->video->provider_name), $this->supported)) {
70
            return sprintf('%s<div class="video-poster" style="background-image: url(%s)">%s</div>',
71
                $this->renderSpinner(),
72
                $this->args['image'],
73
                $this->renderPlayButton()
74
            );
75
        }
76
    }
77
78
    public function renderSpinner()
79
    {
80
        return sprintf(
81
            '<div class="video-spinner">'.
82
                '<div class="spinner"><div class="spinner-dots">%s</div></div>'.
83
            '</div>',
84
            implode('', array_fill(0, 8, '<div class="spinner-dot"></div>'))
85
        );
86
    }
87
88
    protected function setImage($image)
89
    {
90
        $image = $this->image->get($image)->image;
91
        $this->args['image'] = isset($image->large)
92
            ? $image->large['url']
93
            : null;
94
    }
95
96
    protected function setUrl($url)
97
    {
98
        $this->args['url'] = !filter_var($url, FILTER_VALIDATE_URL)
99
            ? $this->postmeta->get($url)
100
            : $url;
101
    }
102
103
    protected function normalize($args)
104
    {
105
        if (is_string($args)) {
106
            $args = ['url' => $args];
107
        }
108
109
        $this->args = shortcode_atts([
110
            'image' => '', // string || int
111
            'player' => '', // string || array
112
            'url' => '', // string
113
        ], $args);
114
115
        foreach ($this->args as $key => $value) {
116
            $method = $this->utility->buildMethodName($key, 'set');
117
            if (!method_exists($this, $method)) {
118
                continue;
119
            }
120
            call_user_func([$this, $method], $value);
121
        }
122
        return $this->args;
123
    }
124
}
125