1 | <?php |
||
2 | |||
3 | namespace Pelmered\LaravelHttpOAuthHelper; |
||
4 | |||
5 | use Illuminate\Http\Client\Factory; |
||
6 | use Illuminate\Http\Client\PendingRequest; |
||
7 | use Illuminate\Support\Facades\Http; |
||
8 | use Illuminate\Support\ServiceProvider; |
||
9 | |||
10 | class LaravelHttpOAuthHelperServiceProvider extends ServiceProvider |
||
11 | { |
||
12 | 46 | public function boot(): void |
|
13 | { |
||
14 | 46 | Http::macro('withRefreshToken', function ( |
|
15 | 46 | string $refreshUrl, |
|
16 | 46 | string|array|Credentials $credentials = [ |
|
17 | 46 | 'refresh_token' => '', |
|
18 | 46 | 'client_id' => '', |
|
19 | 46 | 'client_secret' => '', |
|
20 | 46 | ], |
|
21 | 46 | array|Options $options = [], |
|
22 | 46 | ): PendingRequest { |
|
23 | |||
24 | 5 | $options = $options instanceof Options ? $options : Options::make($options); |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
25 | 5 | $credentials = $credentials instanceof Credentials ? $credentials : new Credentials($credentials); |
|
0 ignored issues
–
show
|
|||
26 | |||
27 | 5 | $accessToken = TokenStore::get( |
|
28 | 5 | refreshUrl: $refreshUrl, |
|
29 | 5 | credentials: $credentials->setOptions($options), |
|
30 | 5 | options: $options, |
|
31 | 5 | ); |
|
32 | |||
33 | /** @var PendingRequest|Factory $httpClient */ |
||
34 | 5 | $httpClient = $this; |
|
35 | |||
36 | // If we get a factory, we can create a new pending request |
||
37 | 5 | if ($httpClient instanceof Factory) { |
|
38 | 5 | $httpClient = $httpClient->createPendingRequest(); |
|
39 | } |
||
40 | |||
41 | 5 | return $accessToken->getHttpClient($httpClient); |
|
42 | 46 | }); |
|
43 | } |
||
44 | } |
||
45 |