|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace RicardoFiorani\Adapter\Youtube; |
|
4
|
|
|
|
|
5
|
|
|
use RicardoFiorani\Adapter\AbstractServiceAdapter; |
|
6
|
|
|
use RicardoFiorani\Adapter\Exception\InvalidThumbnailSizeException; |
|
7
|
|
|
use RicardoFiorani\Renderer\EmbedRendererInterface; |
|
8
|
|
|
|
|
9
|
|
|
class YoutubeServiceAdapter extends AbstractServiceAdapter |
|
10
|
|
|
{ |
|
11
|
|
|
const THUMBNAIL_DEFAULT = 'default'; |
|
|
|
|
|
|
12
|
|
|
const THUMBNAIL_STANDARD_DEFINITION = 'sddefault'; |
|
13
|
|
|
const THUMBNAIL_MEDIUM_QUALITY = 'mqdefault'; |
|
|
|
|
|
|
14
|
|
|
const THUMBNAIL_HIGH_QUALITY = 'hqdefault'; |
|
|
|
|
|
|
15
|
|
|
const THUMBNAIL_MAX_QUALITY = 'maxresdefault'; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
15 |
|
public function __construct(string $url, string $pattern, EmbedRendererInterface $renderer) |
|
18
|
|
|
{ |
|
19
|
15 |
|
preg_match($pattern, $url, $match); |
|
20
|
15 |
|
if (isset($match[2])) { |
|
21
|
11 |
|
$videoId = $match[2]; |
|
22
|
|
|
} |
|
23
|
15 |
|
if (empty($videoId)) { |
|
24
|
4 |
|
$videoId = $match[1]; |
|
25
|
|
|
} |
|
26
|
15 |
|
$this->setVideoId($videoId); |
|
27
|
|
|
|
|
28
|
15 |
|
return parent::__construct($url, $pattern, $renderer); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function getServiceName(): string |
|
32
|
|
|
{ |
|
33
|
1 |
|
return 'Youtube'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function hasThumbnail(): bool |
|
37
|
|
|
{ |
|
38
|
1 |
|
return true; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @throws InvalidThumbnailSizeException |
|
43
|
|
|
*/ |
|
44
|
2 |
View Code Duplication |
public function getThumbnail(string $size, bool $forceSecure = false): string |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
2 |
|
if (false == in_array($size, $this->getThumbNailSizes())) { |
|
|
|
|
|
|
47
|
1 |
|
throw new InvalidThumbnailSizeException(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return $this->getScheme($forceSecure) . '://img.youtube.com/vi/' . $this->getVideoId() . '/' . $size . '.jpg'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
public function getThumbNailSizes(): array |
|
54
|
|
|
{ |
|
55
|
|
|
return array( |
|
56
|
3 |
|
self::THUMBNAIL_DEFAULT, |
|
57
|
3 |
|
self::THUMBNAIL_STANDARD_DEFINITION, |
|
58
|
3 |
|
self::THUMBNAIL_MEDIUM_QUALITY, |
|
59
|
3 |
|
self::THUMBNAIL_HIGH_QUALITY, |
|
60
|
3 |
|
self::THUMBNAIL_MAX_QUALITY, |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public function getEmbedUrl(bool $forceAutoplay = false, bool $forceSecure = false): string |
|
65
|
|
|
{ |
|
66
|
2 |
|
$queryString = $this->generateQuerystring($forceAutoplay); |
|
67
|
|
|
|
|
68
|
2 |
|
return sprintf( |
|
69
|
2 |
|
'%s://www.youtube.com/embed/%s?%s', |
|
70
|
2 |
|
$this->getScheme($forceSecure), |
|
71
|
2 |
|
$this->getVideoId(), |
|
72
|
2 |
|
http_build_query($queryString) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function getSmallThumbnail(bool $forceSecure = false): string |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_STANDARD_DEFINITION, $forceSecure); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function getMediumThumbnail(bool $forceSecure = false): string |
|
82
|
|
|
{ |
|
83
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_MEDIUM_QUALITY, $forceSecure); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function getLargeThumbnail(bool $forceSecure = false): string |
|
87
|
|
|
{ |
|
88
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_HIGH_QUALITY, $forceSecure); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function getLargestThumbnail(bool $forceSecure = false): string |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_MAX_QUALITY, $forceSecure); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
public function isEmbeddable(): bool |
|
97
|
|
|
{ |
|
98
|
1 |
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
private function generateQuerystring(bool $forceAutoplay = false): array |
|
102
|
|
|
{ |
|
103
|
2 |
|
parse_str(parse_url($this->rawUrl, PHP_URL_QUERY), $queryString); |
|
104
|
2 |
|
unset($queryString['v']); |
|
105
|
|
|
|
|
106
|
2 |
|
if ($forceAutoplay) { |
|
107
|
|
|
$queryString['autoplay'] = (int) $forceAutoplay; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
return $queryString; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.