Completed
Push — master ( c49bae...43ad9f )
by
unknown
09:25
created

YouTubeProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 79.31%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
lcom 0
cbo 2
dl 0
loc 92
ccs 23
cts 29
cp 0.7931
rs 10
c 3
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getOembedUrl() 0 4 1
A getImageUrl() 0 10 2
A getIcon() 0 4 1
A getType() 0 4 1
A getEmbedTemplate() 0 4 1
B parseProviderReference() 0 24 5
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Provider;
4
5
class YouTubeProvider extends AbstractOembedProvider implements ProviderInterface
6
{
7
    const URL_OEMBED = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=%s&format=json';
8
    const URL_IMAGE_MAX_RES = 'https://i.ytimg.com/vi/%s/maxresdefault.jpg';
9
    const URL_IMAGE_HQ = 'https://i.ytimg.com/vi/%s/hqdefault.jpg';
10
11
    /**
12
     * @param string $id
13
     * @return string
14
     */
15 1
    public function getOembedUrl($id)
16
    {
17 1
        return sprintf(self::URL_OEMBED, $id);
18
    }
19
20
    /**
21
     * @param string $id
22
     * @return string
23
     */
24 1
    public function getImageUrl($id)
25
    {
26
        // try to get max res image (only available for 720P videos)
27 1
        $urlMaxRes = sprintf(self::URL_IMAGE_MAX_RES, $id);
28 1
        if ($this->getHttpClient()->exists($urlMaxRes)) {
29
            return $urlMaxRes;
30
        }
31
32 1
        return sprintf(self::URL_IMAGE_HQ, $id); // this one always exists
33
    }
34
35
    /**
36
     * @param $value
37
     * @return string
38
     * @throws \Exception
39
     */
40 1
    public function parseProviderReference($value)
41
    {
42 1
        if (strpos($value, 'youtube.com')) {
43 1
            $url = parse_url($value);
44 1
            if (empty($url['query'])) {
45
                throw new \Exception('The supplied URL does not look like a Youtube URL');
46
            }
47 1
            parse_str($url['query'], $params);
48 1
            if (empty($params['v'])) {
49
                throw new \Exception('The supplied URL does not look like a Youtube URL');
50
            }
51
52 1
            return $params['v'];
53
        }
54
55 1
        if (strpos($value, 'youtu.be')) {
56
            $url = parse_url($value);
57
            $id = substr($url['path'], 1);
58
59
            return $id;
60
        }
61
62 1
        return $value;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 5
    public function getIcon()
69
    {
70 5
        return 'fa fa-youtube-play';
71
    }
72
73
    /**
74
     * @return string
75
     */
76 5
    public function getName()
77
    {
78 5
        return 'youtube';
79
    }
80
81
    /**
82
     * @return string
83
     */
84 1
    public function getType()
85
    {
86 1
        return AbstractProvider::TYPE_VIDEO;
87
    }
88
89
    /**
90
     * @return string
91
     */
92 1
    public function getEmbedTemplate()
93
    {
94 1
        return 'MediaMonksSonataMediaBundle:Provider:youtube_embed.html.twig';
95
    }
96
}
97