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
|
5 |
|
public function parseProviderReference($value) |
41
|
|
|
{ |
42
|
5 |
|
if (strpos($value, 'youtube.com')) { |
43
|
4 |
|
$url = parse_url($value); |
44
|
4 |
|
if (empty($url['query'])) { |
45
|
2 |
|
throw new \Exception('The supplied URL does not look like a Youtube URL'); |
46
|
|
|
} |
47
|
2 |
|
parse_str($url['query'], $params); |
48
|
2 |
|
if (empty($params['v'])) { |
49
|
|
|
throw new \Exception('The supplied URL does not look like a Youtube URL'); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
return $params['v']; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
View Code Duplication |
if (strpos($value, 'youtu.be')) { |
|
|
|
|
56
|
2 |
|
$url = parse_url($value); |
57
|
2 |
|
if (empty($url['path']) || empty(trim($url['path'], '/'))) { |
58
|
1 |
|
throw new \Exception('The supplied URL does not look like a Youtube URL'); |
59
|
|
|
} |
60
|
1 |
|
$id = trim($url['path'], '/'); |
61
|
|
|
|
62
|
1 |
|
return $id; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
8 |
|
public function getIcon() |
72
|
|
|
{ |
73
|
8 |
|
return 'fa fa-youtube-play'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
9 |
|
public function getName() |
80
|
|
|
{ |
81
|
9 |
|
return 'youtube'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
1 |
|
public function getType() |
88
|
|
|
{ |
89
|
1 |
|
return AbstractProvider::TYPE_VIDEO; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
1 |
|
public function getEmbedTemplate() |
96
|
|
|
{ |
97
|
1 |
|
return 'MediaMonksSonataMediaBundle:Provider:youtube_embed.html.twig'; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.