Google Sheet - OAuth "client" powered integrations (#415)
* fix `helpers.php` * fix `.eslintrc.cjs` * spreadsheet manager * fetch providers. set `oauth_id` for integrations * create spreadsheet on integration create event * connect OAuth accounts * display actions. connect account if missing * cleanup * handle form field change * map integration data object to `SpreadsheetData` * validate request * wip * redirect to integrations page * fix refresh token * add helper text * add extra integration info * refactor * refresh google token * fix validation * add tests * Fix linting issue * Update composer lock file --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
55
app/Integrations/Google/Google.php
Normal file
55
app/Integrations/Google/Google.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Integrations\Google;
|
||||
|
||||
use App\Integrations\Google\Sheets\SpreadsheetManager;
|
||||
use App\Models\Integration\FormIntegration;
|
||||
use Google\Client as Client;
|
||||
|
||||
class Google
|
||||
{
|
||||
protected Client $client;
|
||||
protected ?string $token;
|
||||
protected ?string $refreshToken;
|
||||
|
||||
public function __construct(
|
||||
protected FormIntegration $formIntegration
|
||||
) {
|
||||
$this->client = new Client();
|
||||
$this->client->setClientId(config('services.google.client_id'));
|
||||
$this->client->setClientSecret(config('services.google.client_secret'));
|
||||
$this->client->setAccessToken([
|
||||
'access_token' => $this->formIntegration->provider->access_token,
|
||||
'created' => $this->formIntegration->provider->updated_at->getTimestamp(),
|
||||
'expires_in' => 3600,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getClient(): Client
|
||||
{
|
||||
if($this->client->isAccessTokenExpired()) {
|
||||
$this->refreshToken();
|
||||
}
|
||||
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
public function refreshToken(): static
|
||||
{
|
||||
$this->client->refreshToken($this->formIntegration->provider->refresh_token);
|
||||
|
||||
$token = $this->client->getAccessToken();
|
||||
|
||||
$this->formIntegration->provider->update([
|
||||
'access_token' => $token['access_token'],
|
||||
'refresh_token' => $token['refresh_token'],
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sheets(): SpreadsheetManager
|
||||
{
|
||||
return new SpreadsheetManager($this, $this->formIntegration);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user