Separated laravel app to its own folder (#540)

This commit is contained in:
Julien Nahum
2024-08-26 18:24:56 +02:00
committed by GitHub
parent 39b8df5eed
commit 5bd1dda504
546 changed files with 124 additions and 143 deletions

View 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);
}
}

View File

@@ -0,0 +1,199 @@
<?php
namespace App\Integrations\Google\Sheets;
use App\Integrations\Data\SpreadsheetData;
use App\Integrations\Google\Google;
use App\Models\Forms\Form;
use App\Models\Integration\FormIntegration;
use App\Service\Forms\FormSubmissionFormatter;
use Google\Service\Sheets;
use Google\Service\Sheets\BatchUpdateValuesRequest;
use Google\Service\Sheets\Spreadsheet;
use Google\Service\Sheets\ValueRange;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class SpreadsheetManager
{
protected Sheets $driver;
protected SpreadsheetData $data;
public function __construct(
protected Google $google,
protected FormIntegration $integration
) {
$this->driver = new Sheets($google->getClient());
$this->data = empty($this->integration->data)
? new SpreadsheetData()
: new SpreadsheetData(
url: $this->integration->data->url,
spreadsheet_id: $this->integration->data->spreadsheet_id,
columns: array_map(
fn ($column) => (array)$column,
$this->integration->data->columns
)
);
}
public function get(string $id): Spreadsheet
{
$spreadsheet = $this->driver
->spreadsheets
->get($id);
return $spreadsheet;
}
public function create(Form $form): Spreadsheet
{
$body = new Spreadsheet([
'properties' => [
'title' => $form->title
]
]);
$spreadsheet = $this->driver->spreadsheets->create($body);
$this->data->url = $spreadsheet->spreadsheetUrl;
$this->data->spreadsheet_id = $spreadsheet->spreadsheetId;
$this->data->columns = [];
$this->updateHeaders($spreadsheet->spreadsheetId);
return $spreadsheet;
}
public function buildColumns(): array
{
collect($this->integration->form->properties)->each(function ($property) {
// Skip custom blocks
if (Str::of($property['type'])->startsWith('nf-')) {
return;
}
$key = Arr::first(
array_keys($this->data->columns),
fn (int $key) => $this->data->columns[$key]['id'] === $property['id']
);
$column = Arr::only($property, ['id', 'name']);
if (!is_null($key)) {
$this->data->columns[$key] = $column;
} else {
$this->data->columns[] = $column;
}
});
$this->integration->update([
'data' => $this->data,
]);
return $this->data->columns;
}
public function updateHeaders(string $id): static
{
$columns = $this->buildColumns();
$headers = array_map(
fn ($column) => $column['name'],
$columns
);
return $this->setHeaders($id, $headers);
}
protected function setHeaders(string $id, array $headers): static
{
$valueRange = new ValueRange([
'values' => [$headers],
]);
$valueRange->setRange(
$this->buildRange($headers)
);
$body = new BatchUpdateValuesRequest([
'valueInputOption' => 'RAW',
'data' => [$valueRange]
]);
$this->driver
->spreadsheets_values
->batchUpdate($id, $body);
return $this;
}
public function buildRow(array $submissionData): array
{
$formatter = (new FormSubmissionFormatter($this->integration->form, $submissionData))->useSignedUrlForFiles()->outputStringsOnly();
$fields = $formatter->getFieldsWithValue();
return collect($this->data->columns)
->map(function (array $column) use ($fields) {
$field = Arr::first($fields, fn ($field) => $field['id'] === $column['id']);
return $field ? $field['value'] : '';
})
->toArray();
}
public function submit(array $submissionData): static
{
$this->updateHeaders($this->data->spreadsheet_id);
$row = $this->buildRow($submissionData);
$this->addRow(
$this->data->spreadsheet_id,
$row
);
return $this;
}
public function addRow(string $id, array $values): static
{
$valueRange = new ValueRange([
'values' => [$values],
]);
$params = [
'valueInputOption' => 'RAW',
];
$this->driver
->spreadsheets_values
->append(
$id,
$this->buildRange($values),
$valueRange,
$params
);
return $this;
}
protected function buildRange(array $values): string
{
$columnsCount = count($values);
$endColumn = $this->getColumnLetter($columnsCount);
return "A1:{$endColumn}1";
}
protected function getColumnLetter(int $columnIndex): string
{
$columnLetter = '';
while ($columnIndex > 0) {
$columnIndex--;
$columnLetter = chr(65 + ($columnIndex % 26)) . $columnLetter;
$columnIndex = (int)($columnIndex / 26);
}
return $columnLetter;
}
}