|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace RicardoFiorani\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use RicardoFiorani\Adapter\Exception\InvalidUrlException; |
|
6
|
|
|
use RicardoFiorani\Adapter\Exception\NotEmbeddableException; |
|
7
|
|
|
use RicardoFiorani\Renderer\EmbedRendererInterface; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractServiceAdapter implements VideoAdapterInterface |
|
10
|
|
|
{ |
|
11
|
|
|
public $rawUrl; |
|
12
|
|
|
public $videoId; |
|
13
|
|
|
public $pattern; |
|
14
|
|
|
public $renderer; |
|
15
|
|
|
|
|
16
|
56 |
|
public function __construct(string $url, string $pattern, EmbedRendererInterface $renderer) |
|
17
|
|
|
{ |
|
18
|
56 |
|
$this->rawUrl = $url; |
|
|
|
|
|
|
19
|
56 |
|
$this->pattern = $pattern; |
|
|
|
|
|
|
20
|
56 |
|
$this->renderer = $renderer; |
|
21
|
56 |
|
} |
|
22
|
|
|
|
|
23
|
2 |
|
public function getRawUrl(): string |
|
24
|
|
|
{ |
|
25
|
2 |
|
return $this->rawUrl; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function setRawUrl(string $rawUrl) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->rawUrl = $rawUrl; |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
19 |
|
public function getVideoId(): string |
|
34
|
|
|
{ |
|
35
|
19 |
|
return $this->videoId; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
56 |
|
public function setVideoId(string $videoId) |
|
39
|
|
|
{ |
|
40
|
56 |
|
$this->videoId = $videoId; |
|
41
|
56 |
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
public function getPattern(): string |
|
44
|
|
|
{ |
|
45
|
2 |
|
return $this->pattern; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function setPattern(string $pattern) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->pattern = $pattern; |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
public function getRenderer(): EmbedRendererInterface |
|
54
|
|
|
{ |
|
55
|
2 |
|
return $this->renderer; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function setRenderer(EmbedRendererInterface $renderer) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->renderer = $renderer; |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @throws NotEmbeddableException |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function getEmbedCode( |
|
67
|
|
|
int $width, |
|
68
|
|
|
int $height, |
|
69
|
|
|
bool $forceAutoplay = false, |
|
70
|
|
|
bool $forceSecure = false |
|
71
|
|
|
): string { |
|
72
|
1 |
|
if (false === $this->isEmbeddable()) { |
|
73
|
|
|
throw new NotEmbeddableException( |
|
74
|
|
|
sprintf('The service "%s" does not provide embeddable videos', $this->getServiceName()) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
return $this->getRenderer()->renderVideoEmbedCode( |
|
79
|
1 |
|
$this->getEmbedUrl($forceAutoplay, $forceSecure), |
|
80
|
1 |
|
$width, |
|
81
|
1 |
|
$height |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @throws InvalidUrlException |
|
87
|
|
|
*/ |
|
88
|
14 |
|
public function getScheme(bool $forceSecure = false): string |
|
89
|
|
|
{ |
|
90
|
14 |
|
if ($forceSecure) { |
|
91
|
5 |
|
return 'https'; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
14 |
|
$parsedUrlSchema = parse_url($this->rawUrl, PHP_URL_SCHEME); |
|
95
|
|
|
|
|
96
|
14 |
|
if (false !== $parsedUrlSchema) { |
|
97
|
14 |
|
return $parsedUrlSchema; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
throw new InvalidUrlException(sprintf('The URL %s is not valid', $this->rawUrl)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function isThumbnailSizeAvailable($intendedSize): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return in_array($intendedSize, $this->getThumbNailSizes()); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
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.