1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Irazasyed\VideoDownloader; |
4
|
|
|
|
5
|
|
|
use Irazasyed\VideoDownloader\Exceptions\VideoDownloaderException; |
6
|
|
|
use Irazasyed\VideoDownloader\Providers\Facebook; |
7
|
|
|
use Irazasyed\VideoDownloader\Providers\ProviderInterface; |
8
|
|
|
|
9
|
|
|
class Factory |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ProviderInterface |
13
|
|
|
*/ |
14
|
|
|
protected $provider; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array Get Default Available Providers. |
18
|
|
|
*/ |
19
|
|
|
protected $providers = [ |
20
|
|
|
'facebook' => Facebook::class, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Factory constructor. |
25
|
|
|
* |
26
|
|
|
* @param null $provider |
27
|
|
|
*/ |
28
|
|
|
public function __construct($provider = null) |
29
|
|
|
{ |
30
|
|
|
if (isset($provider)) { |
31
|
|
|
return $this->make($provider); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $provider |
37
|
|
|
* |
38
|
|
|
* @throws VideoDownloaderException |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function make($provider) |
43
|
|
|
{ |
44
|
|
|
if (!is_object($provider)) { |
45
|
|
|
if (array_key_exists($provider, $this->providers)) { |
46
|
|
|
$provider = $this->providers[$provider]; |
47
|
|
|
} elseif (!class_exists($provider)) { |
48
|
|
|
throw new VideoDownloaderException( |
49
|
|
|
sprintf( |
50
|
|
|
'Provider class "%s" not found! Please make sure the class exists.', |
51
|
|
|
$provider |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$provider = new $provider(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($provider instanceof ProviderInterface) { |
60
|
|
|
/* |
61
|
|
|
* At this stage we definitely have a proper provider to use. |
62
|
|
|
* |
63
|
|
|
* @var Provider $provider |
64
|
|
|
*/ |
65
|
|
|
return $this->provider = $provider; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
throw new VideoDownloaderException( |
69
|
|
|
sprintf( |
70
|
|
|
'Provider class "%s" should be an instance of "Irazasyed\VideoDownloader\Providers\ProviderInterface"', |
71
|
|
|
get_class($provider) |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets Video Download Links with Meta Data from the Provider. |
78
|
|
|
* Returns HD & SD Quality Links. |
79
|
|
|
* |
80
|
|
|
* @param $url |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getVideoInfo($url) |
85
|
|
|
{ |
86
|
|
|
return $this->provider->getVideoInfo($url); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Download remote file from server |
91
|
|
|
* and save it locally using HTTP Client. |
92
|
|
|
* |
93
|
|
|
* @param string $url The URL to Remote File to Download. |
94
|
|
|
* @param string $dstFilename Destination Filename (Accepts File Path too). |
95
|
|
|
* @param bool $isAsyncRequest |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function download($url, $dstFilename, $isAsyncRequest = false) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
return $this->provider->download($url); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param null $provider |
106
|
|
|
* |
107
|
|
|
* @return ProviderInterface |
108
|
|
|
*/ |
109
|
|
|
public function getInstance($provider = null) |
110
|
|
|
{ |
111
|
|
|
if (isset($this->provider)) { |
112
|
|
|
return $this->provider; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->providers[$provider]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Add Provider to List. |
120
|
|
|
* |
121
|
|
|
* @param ProviderInterface $provider |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function setProvider(ProviderInterface $provider) |
126
|
|
|
{ |
127
|
|
|
$class = get_class($provider); |
128
|
|
|
if (!array_key_exists($class, $this->providers)) { |
129
|
|
|
$this->providers[$class] = $provider; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns Current Provider Instance. |
137
|
|
|
*/ |
138
|
|
|
public function getProvider() |
139
|
|
|
{ |
140
|
|
|
return $this->provider; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|