|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Libcast\AssetDistributor\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Libcast\AssetDistributor\Asset\Asset; |
|
6
|
|
|
use Libcast\AssetDistributor\Configuration\CategoryRegistry; |
|
7
|
|
|
use Libcast\AssetDistributor\Configuration\Configuration; |
|
8
|
|
|
use Libcast\AssetDistributor\Configuration\ConfigurationFactory; |
|
9
|
|
|
use Libcast\AssetDistributor\LoggerTrait; |
|
10
|
|
|
use Libcast\AssetDistributor\Owner; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractAdapter |
|
14
|
|
|
{ |
|
15
|
|
|
use LoggerTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
* @var mixed |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $client; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* @var Owner |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $owner; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @var Configuration |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $configuration; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $credentials; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* @var bool |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $isAuthenticated = false; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* |
|
49
|
|
|
* @param mixed $configuration |
|
50
|
|
|
* @param Owner $owner |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(Owner $owner, $configuration, LoggerInterface $logger = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->owner = $owner; |
|
55
|
|
|
|
|
56
|
|
|
if ($configuration instanceof Configuration) { |
|
57
|
|
|
$this->configuration = $configuration; |
|
58
|
|
|
} else { |
|
59
|
|
|
$this->configuration = ConfigurationFactory::build($this->getVendor(), $configuration, $logger); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->logger = $logger; |
|
63
|
|
|
|
|
64
|
|
|
// Register Vendor shared configurations |
|
65
|
|
|
CategoryRegistry::addVendorCategories($this->getVendor(), $this->configuration->getCategoryMap()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
abstract protected function getVendor(); |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $key |
|
77
|
|
|
* @param mixed $default |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getConfiguration($key, $default = null) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->configuration->get($key, $default); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Doctrine\Common\Cache\Cache |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getCache() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->owner->getCache(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getCredentials() |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->credentials) { |
|
101
|
|
|
return $this->credentials; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (!$accounts = $this->owner->getAccounts()) { |
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (!isset($accounts[$this->getVendor()]) or !$credentials = $accounts[$this->getVendor()]) { |
|
109
|
|
|
return null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->debug('Get service credentials from Owner', [ |
|
113
|
|
|
'vendor' => $this->getVendor(), |
|
114
|
|
|
'credentials' => $credentials, |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
return $this->credentials = $credentials; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* |
|
122
|
|
|
* @param mixed $credentials |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setCredentials($credentials) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->credentials = $credentials; |
|
127
|
|
|
$this->owner->setAccount($this->getVendor(), $credentials); |
|
128
|
|
|
|
|
129
|
|
|
$this->debug('Save credentials', [ |
|
130
|
|
|
'owner' => $this->owner->getIdentifier(), |
|
131
|
|
|
'vendor' => $this->getVendor(), |
|
132
|
|
|
'credentials' => $credentials, |
|
133
|
|
|
]); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* |
|
138
|
|
|
* @return boolean |
|
139
|
|
|
*/ |
|
140
|
|
|
public function isAuthenticated() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->isAuthenticated; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Maps an Asset to a Provider resource identifier |
|
147
|
|
|
* |
|
148
|
|
|
* @param Asset $asset |
|
149
|
|
|
* @param $identifier |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function remember(Asset $asset, $identifier) |
|
152
|
|
|
{ |
|
153
|
|
|
if (!$map = $this->getCache()->fetch((string) $asset)) { |
|
154
|
|
|
$map = []; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$map[$this->getVendor()] = $identifier; |
|
158
|
|
|
|
|
159
|
|
|
$this->debug('Remember Asset identifier from vendor', [ |
|
160
|
|
|
'asset' => (string) $asset, |
|
161
|
|
|
'vendor' => $this->getVendor(), |
|
162
|
|
|
'identifier' => $identifier, |
|
163
|
|
|
]); |
|
164
|
|
|
|
|
165
|
|
|
$this->getCache()->save((string) $asset, $map); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Returns the Provider identifier of an Asset if exists, or `false` otherwise |
|
170
|
|
|
* |
|
171
|
|
|
* @param Asset $asset |
|
172
|
|
|
* @return string|null |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function retrieve(Asset $asset) |
|
175
|
|
|
{ |
|
176
|
|
|
if (!$map = $this->getCache()->fetch((string) $asset)) { |
|
177
|
|
|
return null; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$identifier = isset($map[$this->getVendor()]) ? $map[$this->getVendor()] : null; |
|
181
|
|
|
|
|
182
|
|
|
$this->debug('Retrieve Asset identifier from vendor', [ |
|
183
|
|
|
'asset' => (string) $asset, |
|
184
|
|
|
'vendor' => $this->getVendor(), |
|
185
|
|
|
'identifier' => $identifier, |
|
186
|
|
|
]); |
|
187
|
|
|
|
|
188
|
|
|
return $identifier; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Remove an Asset from the map |
|
193
|
|
|
* |
|
194
|
|
|
* @param Asset $asset |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function forget(Asset $asset) |
|
197
|
|
|
{ |
|
198
|
|
|
if (!$map = $this->getCache()->fetch((string) $asset)) { |
|
199
|
|
|
return; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
if (isset($map[$this->getVendor()])) { |
|
203
|
|
|
unset($map[$this->getVendor()]); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$this->debug('Forget Asset from vendor', [ |
|
207
|
|
|
'asset' => (string) $asset, |
|
208
|
|
|
'vendor' => $this->getVendor(), |
|
209
|
|
|
]); |
|
210
|
|
|
|
|
211
|
|
|
$this->getCache()->save((string) $asset, $map); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $url |
|
217
|
|
|
* @param bool $from_client |
|
218
|
|
|
* @throws \Exception |
|
219
|
|
|
*/ |
|
220
|
|
|
public function redirect($url, $from_client = false) |
|
221
|
|
|
{ |
|
222
|
|
|
if ('cli' === php_sapi_name()) { |
|
223
|
|
|
throw new \Exception('Impossible to redirect from CLI'); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
$this->debug('Redirect client', ['url' => $url, 'from_client' => $from_client]); |
|
227
|
|
|
|
|
228
|
|
|
if ($from_client or headers_sent()) { |
|
229
|
|
|
echo sprintf('<noscript><meta http-equiv="refresh" content="0; url=%1$s" /></noscript><script type="text/javascript"> window.location.href="%1$s"; </script><a href="%1$s">%1$s</a>', $url); |
|
230
|
|
|
} else { |
|
231
|
|
|
header("Location: $url"); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
exit; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|