1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jackal\Downloader; |
4
|
|
|
|
5
|
|
|
use Jackal\Downloader\Downloader\DownloaderInterface; |
6
|
|
|
use Jackal\Downloader\Exception\DownloadException; |
7
|
|
|
use Jackal\Downloader\Parser\URLRegexParser; |
8
|
|
|
|
9
|
|
|
class VideoDownloader |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string[] |
13
|
|
|
*/ |
14
|
|
|
protected $downloaders = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string|object $downloaderClass |
18
|
|
|
* @throws DownloadException |
19
|
|
|
*/ |
20
|
|
|
public function registerDownloader($downloaderClass) : void |
21
|
|
|
{ |
22
|
|
|
if (is_object($downloaderClass)) { |
23
|
|
|
$downloaderClass = get_class($downloaderClass); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (array_key_exists($downloaderClass::getType(), $this->downloaders)) { |
27
|
|
|
throw DownloadException::alreadyRegistered($downloaderClass::getType()); |
28
|
|
|
} |
29
|
|
|
$this->downloaders[$downloaderClass::getType()] = $downloaderClass; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string[] |
34
|
|
|
*/ |
35
|
|
|
public function getRegisteredDownloaders() : array |
36
|
|
|
{ |
37
|
|
|
return $this->downloaders; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $name |
42
|
|
|
* @param mixed $id |
43
|
|
|
* @param array $config |
44
|
|
|
* @return DownloaderInterface |
45
|
|
|
* @throws DownloadException |
46
|
|
|
*/ |
47
|
|
|
public function getDownloader(string $name, $id, array $config = []) : DownloaderInterface |
48
|
|
|
{ |
49
|
|
|
if (!array_key_exists($name, $this->downloaders)) { |
50
|
|
|
throw DownloadException::invalidName($name); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return new $this->downloaders[$name]($id, $config); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $publicUrl |
58
|
|
|
* @param array $config |
59
|
|
|
* @return DownloaderInterface |
60
|
|
|
* @throws DownloadException |
61
|
|
|
*/ |
62
|
|
|
public function getDownloaderByPublicUrl(string $publicUrl, $config = []) : DownloaderInterface{ |
63
|
|
|
foreach ($this->getRegisteredDownloaders() as $downloaderClass){ |
64
|
|
|
$regexParser = new URLRegexParser($downloaderClass); |
65
|
|
|
if($regexParser->isValidUrl($publicUrl)){ |
66
|
|
|
return new $downloaderClass($regexParser->parse($publicUrl), $config); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw DownloadException::invalidPublicUrl($publicUrl); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|