Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer committed Nov 13, 2024
1 parent 015a923 commit 0d2ba7c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 41 deletions.
2 changes: 1 addition & 1 deletion config/excel-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* The configuration for the exporters.
*/
'exporters' => [
'google_sheet' => [
'google-sheet' => [
/*
* Path to the client secret json file. Take a look at the README of this package
* to learn how to get this file. You can also pass the credentials as an array
Expand Down
2 changes: 0 additions & 2 deletions src/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Enflow\LaravelExcelExporter;

use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\ExportableToGoogleSheet;
use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\GoogleSheetPusher;
use Maatwebsite\Excel\Concerns\WithTitle;

interface Exportable extends WithTitle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Enflow\LaravelExcelExporter\Exporters\GoogleSheet;
namespace Enflow\LaravelExcelExporter\Exporters\GoogleSheet\Exceptions;

use Exception;

class GoogleSheetInvalidConfiguration extends Exception
class InvalidConfiguration extends Exception
{
public static function credentialsJsonDoesNotExist(string $path): static
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

namespace Enflow\LaravelExcelExporter\Exporters\GoogleSheet;

use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\Exceptions\InvalidConfiguration;
use Google\Service\Sheets;
use Google_Service_Sheets_BatchUpdateSpreadsheetRequest;
use Google_Service_Sheets_ValueRange;

class GoogleSheetService
class GoogleSheet
{
public function __construct(
protected Sheets $service,
) {

}

public function clearSheet(string $spreadsheetId, string $sheetName): void
public function clear(string $spreadsheetId, string $sheetName): void
{
$spreadsheet = $this->service->spreadsheets->get($spreadsheetId);

$sheet = collect($spreadsheet->getSheets())->firstWhere(fn (Sheets\Sheet $sheet) => $sheet->getProperties()->getTitle() === $sheetName);
throw_unless($sheet, GoogleSheetInvalidConfiguration::sheetDoesntExist($sheetName));
throw_unless($sheet, InvalidConfiguration::sheetDoesntExist($sheetName));

$sheetId = $sheet->getProperties()->getSheetId();

Expand Down
32 changes: 19 additions & 13 deletions src/Exporters/GoogleSheet/GoogleSheetPusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,32 @@
namespace Enflow\LaravelExcelExporter\Exporters\GoogleSheet;

use Enflow\LaravelExcelExporter\Exportable;
use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\ExportableToGoogleSheet;
use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\GoogleSheetService;
use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\Exceptions\InvalidConfiguration;
use Enflow\LaravelExcelExporter\Pusher;
use Google\Exception as GoogleException;
use Illuminate\Support\LazyCollection;
use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Writer;

class GoogleSheetPusher implements Pusher
{
public function __construct(
protected Exportable $export,
protected Exportable $export,
)
{
$config = config('excel-to-google-sheet');
$this->guardAgainstInvalidConfiguration($config);

$this->service = app(GoogleSheetService::class, [

]);
}

public function clear(): void
{
$this->service->clearSheet($this->export->googleSpreadsheetId(), $this->export->title());
dd('would clear sheet');
$this->sheet()->clear($this->export->googleSpreadsheetId(), $this->export->title());
}

public function insert(LazyCollection $collection): void
{
dd('would insert data');
try {
$collection->each(function (LazyCollection $chunk) {
// Send the data to the Google Sheet.
$this->service->insert(
$this->sheet()->insert(
spreadsheetId: $this->export->googleSpreadsheetId(),
range: $this->export->title(),
values: $chunk->values()->all(),
Expand All @@ -49,4 +42,17 @@ public function insert(LazyCollection $collection): void
throw $e;
}
}

protected function sheet(): GoogleSheet
{
$config = config('excel-exporter.exporters.google-sheet');

if (! is_array($config['service_account_credentials_json']) && ! file_exists($config['service_account_credentials_json'])) {
throw InvalidConfiguration::credentialsJsonDoesNotExist($config['service_account_credentials_json']);
}

return app(GoogleSheet::class, [
'service' => GoogleSheetServiceFactory::createForConfig($config),
]);
}
}
4 changes: 2 additions & 2 deletions src/Exporters/GoogleSheet/GoogleSheetServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

class GoogleSheetServiceFactory
{
public static function createForConfig(array $config): GoogleSheetService
public static function createForConfig(array $config): GoogleSheet
{
$googleClient = new Client();
$googleClient->setApplicationName(config('app.name'));
$googleClient->setScopes([GoogleSheets::SPREADSHEETS]);
$googleClient->setAccessType('offline');
$googleClient->setAuthConfig($config['service_account_credentials_json']);

return new GoogleSheetService(new GoogleSheets($googleClient));
return new GoogleSheet(new GoogleSheets($googleClient));
}
}
23 changes: 5 additions & 18 deletions src/PushHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Enflow\LaravelExcelExporter\Exceptions\MustImplementExportable;
use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\ExportableToGoogleSheet;
use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\GoogleSheetPusher;
use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\GoogleSheetService;
use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\GoogleSheet;
use Exception;
use Google\Exception as GoogleException;
use Illuminate\Support\LazyCollection;
Expand All @@ -16,19 +16,16 @@ class PushHandler
{
public function __invoke(Exportable $export): void
{
$pusher = $this->pusher($export);

if (method_exists($export, 'prepare')) {
$export->prepare();
}
$pusher = PusherFactory::make($export);

$writer = app(Writer::class)->export($export, Excel::CSV);
$temporaryFilePath = $writer->getLocalPath();

$handle = fopen($temporaryFilePath, 'r');

// Ensure the export destination is empty.
$pusher->clear();

$handle = fopen($temporaryFilePath, 'r');

try {
LazyCollection::make(function () use ($handle) {
while ($line = fgetcsv($handle)) {
Expand All @@ -48,14 +45,4 @@ public function __invoke(Exportable $export): void
}
}
}

private function pusher(Exportable $export): Pusher
{
$class = match (true) {
$export instanceof ExportableToGoogleSheet => GoogleSheetPusher::class,
default => throw new Exception('Must implement specific pusher for exportable'),
};

return app($class);
}
}
20 changes: 20 additions & 0 deletions src/PusherFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Enflow\LaravelExcelExporter;

use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\ExportableToGoogleSheet;
use Enflow\LaravelExcelExporter\Exporters\GoogleSheet\GoogleSheetPusher;
use Exception;

class PusherFactory
{
public static function make(Exportable $export): Pusher
{
$class = match (true) {
$export instanceof ExportableToGoogleSheet => GoogleSheetPusher::class,
default => throw new Exception('Must implement specific pusher for exportable'),
};

return app($class, ['export' => $export]);
}
}

0 comments on commit 0d2ba7c

Please sign in to comment.