1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace STS\StorageConnect\Drivers; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Http\RedirectResponse; |
7
|
|
|
use SocialiteProviders\Manager\OAuth2\AbstractProvider; |
8
|
|
|
use STS\StorageConnect\Events\CloudStorageSetup; |
9
|
|
|
use STS\StorageConnect\Models\CloudStorage; |
10
|
|
|
use STS\StorageConnect\Models\CustomManagedCloudStorage; |
11
|
|
|
use STS\StorageConnect\Models\Quota; |
12
|
|
|
use STS\StorageConnect\UploadRequest; |
13
|
|
|
use STS\StorageConnect\UploadResponse; |
14
|
|
|
|
15
|
|
|
abstract class AbstractAdapter |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $driver; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $providerClass; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var AbstractProvider |
34
|
|
|
*/ |
35
|
|
|
protected $provider; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var mixed |
39
|
|
|
*/ |
40
|
|
|
protected $service; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $token; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var callable |
49
|
|
|
*/ |
50
|
|
|
protected $tokenUpdateCallback; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* DropboxAdapter constructor. |
54
|
|
|
* |
55
|
|
|
* @param array $config |
56
|
|
|
*/ |
57
|
|
|
public function __construct(array $config) |
58
|
|
|
{ |
59
|
|
|
$this->config = $config; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $token |
64
|
|
|
* @param null $callback |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
* @paran $callback |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
|
public function setToken($token, $callback = null) |
71
|
|
|
{ |
72
|
|
|
$this->token = $token; |
73
|
|
|
$this->tokenUpdateCallback = $callback; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $token |
80
|
|
|
*/ |
81
|
|
|
protected function updateToken($token) |
82
|
|
|
{ |
83
|
|
|
if ($this->tokenUpdateCallback) { |
84
|
|
|
call_user_func($this->tokenUpdateCallback, $token); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param CloudStorage $storage |
90
|
|
|
* @param null $redirectUrl |
91
|
|
|
* |
92
|
|
|
* @return RedirectResponse |
93
|
|
|
*/ |
94
|
|
|
public function authorize(CloudStorage $storage, $redirectUrl = null) |
95
|
|
|
{ |
96
|
|
|
if (!$storage->exists) { |
97
|
|
|
$storage->save(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($storage instanceof CustomManagedCloudStorage) { |
101
|
|
|
$this->provider()->session()->put('storage-connect.custom', true); |
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$this->provider()->session()->put('storage-connect.id', $storage->id); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($redirectUrl != null) { |
107
|
|
|
$this->provider()->session()->put('storage-connect.redirect', $redirectUrl); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->provider()->redirect(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param CloudStorage $storage |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function finish(CloudStorage $storage) |
119
|
|
|
{ |
120
|
|
|
$this->setToken($this->provider()->user()->accessTokenResponseBody); |
121
|
|
|
|
122
|
|
|
$storage->update(array_merge( |
123
|
|
|
[ |
124
|
|
|
'token' => $this->provider()->user()->accessTokenResponseBody, |
125
|
|
|
'connected' => 1, |
126
|
|
|
'enabled' => 1, |
127
|
|
|
'enabled_at' => Carbon::now() |
128
|
|
|
], |
129
|
|
|
$this->mapUserDetails($this->provider()->user()) |
130
|
|
|
)); |
131
|
|
|
$storage->updateQuota($this->getQuota()); |
132
|
|
|
|
133
|
|
|
event(new CloudStorageSetup($storage)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function driver() |
140
|
|
|
{ |
141
|
|
|
return $this->driver; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return AbstractProvider |
146
|
|
|
*/ |
147
|
|
|
public function provider() |
148
|
|
|
{ |
149
|
|
|
if (!$this->provider) { |
150
|
|
|
$this->setProvider($this->makeProvider()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->provider; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function setProvider(AbstractProvider $provider) |
157
|
|
|
{ |
158
|
|
|
$this->provider = $provider; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
public function service() |
165
|
|
|
{ |
166
|
|
|
if (!$this->service) { |
167
|
|
|
$this->setService($this->makeService()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->service; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param $service |
175
|
|
|
* |
176
|
|
|
* @return AbstractAdapter |
177
|
|
|
*/ |
178
|
|
|
public function setService($service) |
179
|
|
|
{ |
180
|
|
|
$this->service = $service; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return AbstractProvider |
187
|
|
|
*/ |
188
|
|
|
protected function makeProvider() { |
189
|
|
|
return app($this->providerClass); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
|
|
abstract protected function makeService(); |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return Quota |
199
|
|
|
*/ |
200
|
|
|
abstract function getQuota(); |
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param $user |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
abstract protected function mapUserDetails($user); |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param UploadRequest $request |
211
|
|
|
* |
212
|
|
|
* @return UploadResponse |
213
|
|
|
*/ |
214
|
|
|
abstract function upload(UploadRequest $request); |
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param UploadResponse $response |
218
|
|
|
* |
219
|
|
|
* @return UploadResponse |
220
|
|
|
*/ |
221
|
|
|
abstract function checkUploadStatus(UploadResponse $response); |
|
|
|
|
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $remotePath |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
abstract function pathExists($remotePath); |
|
|
|
|
229
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: