Files
opnform-host-nginx/app/Integrations/Handlers/GoogleSheetsIntegration.php
Boris Lepikhin 24d33a9ebb 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>
2024-06-05 15:35:46 +02:00

73 lines
1.7 KiB
PHP

<?php
namespace App\Integrations\Handlers;
use App\Events\Forms\FormSubmitted;
use App\Integrations\Google\Google;
use App\Models\Integration\FormIntegration;
use Exception;
use Illuminate\Support\Facades\Log;
class GoogleSheetsIntegration extends AbstractIntegrationHandler
{
protected Google $client;
public function __construct(
protected FormSubmitted $event,
protected FormIntegration $formIntegration,
protected array $integration
) {
parent::__construct($event, $formIntegration, $integration);
$this->client = new Google($formIntegration);
}
public static function getValidationRules(): array
{
return [
];
}
public static function isOAuthRequired(): bool
{
return true;
}
public static function getValidationAttributes(): array
{
return [
'oauth_id' => 'Google Account',
];
}
public function handle(): void
{
if (!$this->shouldRun()) {
return;
}
Log::debug('Creating Google Spreadsheet record', [
'spreadsheet_id' => $this->getSpreadsheetId(),
'form_id' => $this->form->id,
'form_slug' => $this->form->slug,
]);
$this->client->sheets()->submit($this->submissionData);
}
protected function getSpreadsheetId(): string
{
if(!isset($this->integrationData->spreadsheet_id)) {
throw new Exception('The spreadsheed is not instantiated');
}
return $this->integrationData->spreadsheet_id;
}
protected function shouldRun(): bool
{
return parent::shouldRun() && $this->formIntegration->oauth_id && $this->getSpreadsheetId();
}
}