1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace RicardoFiorani\Adapter\Vimeo; |
4
|
|
|
|
5
|
|
|
use RicardoFiorani\Adapter\AbstractServiceAdapter; |
6
|
|
|
use RicardoFiorani\Adapter\Exception\InvalidThumbnailSizeException; |
7
|
|
|
use RicardoFiorani\Adapter\Exception\InvalidUrlException; |
8
|
|
|
use RicardoFiorani\Adapter\Exception\ServiceApiNotAvailable; |
9
|
|
|
use RicardoFiorani\Renderer\EmbedRendererInterface; |
10
|
|
|
|
11
|
|
|
class VimeoServiceAdapter extends AbstractServiceAdapter |
12
|
|
|
{ |
13
|
|
|
const THUMBNAIL_SMALL = 'thumbnail_small'; |
|
|
|
|
14
|
|
|
const THUMBNAIL_MEDIUM = 'thumbnail_medium'; |
15
|
|
|
const THUMBNAIL_LARGE = 'thumbnail_large'; |
|
|
|
|
16
|
|
|
|
17
|
|
|
public $title; |
18
|
|
|
public $description; |
19
|
|
|
public $thumbnails; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @throws ServiceApiNotAvailable |
23
|
|
|
*/ |
24
|
12 |
|
public function __construct(string $url, string $pattern, EmbedRendererInterface $renderer) |
25
|
|
|
{ |
26
|
12 |
|
$videoId = $this->getVideoIdByPattern($url, $pattern); |
27
|
12 |
|
$this->setVideoId($videoId); |
28
|
12 |
|
$videoData = $this->getVideoDataFromServiceApi(); |
29
|
|
|
|
30
|
12 |
|
$this->setThumbnails(array( |
31
|
12 |
|
self::THUMBNAIL_SMALL => $videoData[self::THUMBNAIL_SMALL], |
32
|
12 |
|
self::THUMBNAIL_MEDIUM => $videoData[self::THUMBNAIL_MEDIUM], |
33
|
12 |
|
self::THUMBNAIL_LARGE => $videoData[self::THUMBNAIL_LARGE], |
34
|
|
|
)); |
35
|
|
|
|
36
|
12 |
|
$this->setTitle($videoData['title']); |
37
|
12 |
|
$this->setDescription($videoData['description']); |
38
|
|
|
|
39
|
12 |
|
return parent::__construct($url, $pattern, $renderer); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function getServiceName(): string |
43
|
|
|
{ |
44
|
1 |
|
return 'Vimeo'; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function hasThumbnail(): bool |
48
|
|
|
{ |
49
|
1 |
|
return false == empty($this->thumbnails); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function getTitle(): string |
53
|
|
|
{ |
54
|
1 |
|
return $this->title; |
55
|
|
|
} |
56
|
|
|
|
57
|
12 |
|
public function setTitle(string $title) |
58
|
|
|
{ |
59
|
12 |
|
$this->title = $title; |
60
|
12 |
|
} |
61
|
|
|
|
62
|
1 |
|
public function getDescription(): string |
63
|
|
|
{ |
64
|
1 |
|
return $this->description; |
65
|
|
|
} |
66
|
|
|
|
67
|
12 |
|
public function setDescription(string $description) |
68
|
|
|
{ |
69
|
12 |
|
$this->description = $description; |
70
|
12 |
|
} |
71
|
|
|
|
72
|
12 |
|
private function setThumbnails(array $thumbnails) |
73
|
|
|
{ |
74
|
12 |
|
foreach ($thumbnails as $key => $thumbnail) { |
75
|
12 |
|
$this->thumbnails[$key] = parse_url($thumbnail); |
76
|
|
|
} |
77
|
12 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @throws InvalidThumbnailSizeException |
81
|
|
|
* @throws InvalidUrlException |
82
|
|
|
*/ |
83
|
2 |
|
public function getThumbnail(string $size, bool $forceSecure = false): string |
84
|
|
|
{ |
85
|
2 |
|
if (false == in_array($size, $this->getThumbNailSizes())) { |
|
|
|
|
86
|
1 |
|
throw new InvalidThumbnailSizeException(); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return sprintf( |
90
|
1 |
|
'%s://%s%s', |
91
|
1 |
|
$this->getScheme($forceSecure), |
92
|
1 |
|
$this->thumbnails[$size]['host'], |
93
|
1 |
|
$this->thumbnails[$size]['path'] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws InvalidUrlException |
99
|
|
|
*/ |
100
|
2 |
|
public function getEmbedUrl(bool $forceAutoplay = false, bool $forceSecure = false): string |
101
|
|
|
{ |
102
|
2 |
|
return $this->getScheme($forceSecure) . '://player.vimeo.com/video/' . $this->getVideoId() . ($forceAutoplay ? '?autoplay=1' : ''); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
public function getThumbNailSizes(): array |
106
|
|
|
{ |
107
|
|
|
return array( |
108
|
3 |
|
self::THUMBNAIL_SMALL, |
109
|
3 |
|
self::THUMBNAIL_MEDIUM, |
110
|
3 |
|
self::THUMBNAIL_LARGE, |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @throws InvalidThumbnailSizeException |
116
|
|
|
* @throws InvalidUrlException |
117
|
|
|
*/ |
118
|
1 |
|
public function getSmallThumbnail(bool $forceSecure = false): string |
119
|
|
|
{ |
120
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_SMALL,$forceSecure); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @throws InvalidThumbnailSizeException |
125
|
|
|
* @throws InvalidUrlException |
126
|
|
|
*/ |
127
|
1 |
|
public function getMediumThumbnail(bool $forceSecure = false): string |
128
|
|
|
{ |
129
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_MEDIUM,$forceSecure); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @throws InvalidThumbnailSizeException |
134
|
|
|
* @throws InvalidUrlException |
135
|
|
|
*/ |
136
|
1 |
|
public function getLargeThumbnail(bool $forceSecure = false): string |
137
|
|
|
{ |
138
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @throws InvalidThumbnailSizeException |
143
|
|
|
* @throws InvalidUrlException |
144
|
|
|
*/ |
145
|
1 |
|
public function getLargestThumbnail(bool $forceSecure = false): string |
146
|
|
|
{ |
147
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
public function isEmbeddable(): bool |
151
|
|
|
{ |
152
|
1 |
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
12 |
|
private function getVideoIdByPattern(string $url, string $pattern): string |
156
|
|
|
{ |
157
|
12 |
|
$match = array(); |
158
|
12 |
|
preg_match($pattern, $url, $match); |
159
|
12 |
|
$videoId = $match[2]; |
160
|
|
|
|
161
|
12 |
|
return $videoId; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* TODO make this better by using guzzle |
|
|
|
|
166
|
|
|
* @throws ServiceApiNotAvailable |
167
|
|
|
*/ |
168
|
12 |
|
private function getVideoDataFromServiceApi(): array |
169
|
|
|
{ |
170
|
12 |
|
$contents = file_get_contents('http://vimeo.com/api/v2/video/' . $this->getVideoId() . '.php'); |
171
|
12 |
|
if (false === $contents) { |
172
|
|
|
throw new ServiceApiNotAvailable( |
173
|
|
|
'Service "%s" could not reach it\'s API. Check if file_get_contents() function is available.', |
174
|
|
|
$this->getServiceName() |
175
|
|
|
); |
176
|
|
|
} |
177
|
12 |
|
$hash = unserialize($contents); |
178
|
|
|
|
179
|
12 |
|
return reset($hash); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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.