From 564c118e429df97d73947bead56eb4a8fd5da1bd Mon Sep 17 00:00:00 2001 From: Tom Shaw Date: Tue, 17 Oct 2023 07:46:51 -0500 Subject: [PATCH] Misc updates. --- config/service.php | 1 - src/Api/GoogleCalendar.php | 10 ++-- src/Api/GoogleMail.php | 6 +-- src/Contracts/GoogleClientInterface.php | 16 +++++- src/GoogleClient.php | 59 ++++++++++++++++++---- src/Providers/GoogleApiServiceProvider.php | 7 +-- 6 files changed, 76 insertions(+), 23 deletions(-) diff --git a/config/service.php b/config/service.php index 85d8e47..c02184c 100644 --- a/config/service.php +++ b/config/service.php @@ -8,7 +8,6 @@ ], ], 'calendar' => [ - 'calendar_id' => env('GOOGLE_CALENDAR_ID'), 'owner' => [ 'name' => 'Your Name', 'email' => 'name@example.com', diff --git a/src/Api/GoogleCalendar.php b/src/Api/GoogleCalendar.php index 5a9821b..cb9c5d6 100644 --- a/src/Api/GoogleCalendar.php +++ b/src/Api/GoogleCalendar.php @@ -20,12 +20,12 @@ public function __construct(GoogleClient $client) $client->createAuthUrl(); } - $this->service = new Calendar($client->getClient()); + $this->service = new Calendar($client->client); } public function listEvents(int $maxResults = 10, string $orderBy = 'startTime', bool $singleEvents = true) { - $calendarId = config('google-api.service.calendar.owner.email'); + $calendarId = config('google-api-service.calendar.owner.email'); $options = [ 'maxResults' => $maxResults, @@ -54,7 +54,7 @@ public function addEvent($summary, $location, $from, $to, $description = '') $end->setDateTime($this->date3339($to)); $event->setEnd($end); - $calendarId = config('google-api.service.calendar.owner.email'); + $calendarId = config('google-api-service.calendar.owner.email'); $insert = $this->service->events->insert($calendarId, $event); @@ -63,7 +63,7 @@ public function addEvent($summary, $location, $from, $to, $description = '') public function updateEvent($eventId, $summary, $location, $from, $to, $description = '') { - $calendarId = config('google-api.service.calendar.owner.email'); + $calendarId = config('google-api-service.calendar.owner.email'); $event = new Event($this->service->events->get($calendarId, $eventId)); $event->setSummary($summary); @@ -85,7 +85,7 @@ public function updateEvent($eventId, $summary, $location, $from, $to, $descript public function deleteEvent($eventId) { - $calendarId = config('google-api.service.calendar.owner.email'); + $calendarId = config('google-api-service.calendar.owner.email'); $this->service->events->delete($calendarId, $eventId); diff --git a/src/Api/GoogleMail.php b/src/Api/GoogleMail.php index db54b9b..7f8ff57 100644 --- a/src/Api/GoogleMail.php +++ b/src/Api/GoogleMail.php @@ -33,10 +33,10 @@ public function __construct(GoogleClient $client) $client->createAuthUrl(); } - $this->service = new Gmail($client->getClient()); + $this->service = new Gmail($client->client); - $this->setFromName(config('google-api.service.gmail.sender.name')); - $this->setFromEmail(config('google-api.service.gmail.sender.email')); + $this->setFromName(config('google-api-service.gmail.sender.name')); + $this->setFromEmail(config('google-api-service.gmail.sender.email')); } public function setToName($toName): GoogleMail diff --git a/src/Contracts/GoogleClientInterface.php b/src/Contracts/GoogleClientInterface.php index a1f809b..09d1c29 100644 --- a/src/Contracts/GoogleClientInterface.php +++ b/src/Contracts/GoogleClientInterface.php @@ -2,13 +2,25 @@ namespace TomShaw\GoogleApi\Contracts; -use Google\Client; use Illuminate\Support\Collection; +use TomShaw\GoogleApi\GoogleClient; use TomShaw\GoogleApi\Models\GoogleToken; interface GoogleClientInterface { - public function getClient(): Client; + public function setAuthConfig(string $authConfig): GoogleClient; + + public function setApplicationName(string $applicationName): GoogleClient; + + public function addScope(array $scopes): GoogleClient; + + public function setAccessType(string $accessType = 'offline'): GoogleClient; + + public function setPrompt(string $prompt = 'none'): GoogleClient; + + public function setApprovalPrompt(string $approvalPrompt = 'offline'): GoogleClient; + + public function setIncludeGrantedScopes(bool $includeGrantedScopes = true): GoogleClient; public function createAuthUrl(): void; diff --git a/src/GoogleClient.php b/src/GoogleClient.php index ff19615..4cc4167 100644 --- a/src/GoogleClient.php +++ b/src/GoogleClient.php @@ -12,8 +12,6 @@ class GoogleClient implements GoogleClientInterface { - protected Client $client; - public static $rules = [ 'access_token' => 'required|string', 'refresh_token' => 'required|string', @@ -23,17 +21,16 @@ class GoogleClient implements GoogleClientInterface 'created' => 'required|numeric', ]; - public function __construct() - { + public function __construct( + public Client $client + ) { if (! file_exists(config('google-api.auth_config'))) { throw new GoogleClientException('Unable to load client secrets.'); } - $this->client = new Client(); - $this->client->setAuthConfig(config('google-api.auth_config')); - $this->client->addScope(config('google-api.scopes')); + $this->client->addScope(config('google-api-scopes')); $this->client->setApplicationName(config('google-api.application_name')); @@ -64,9 +61,53 @@ public function __construct() } } - public function getClient(): Client + public function setAuthConfig(string $authConfig): GoogleClient + { + $this->client->setAuthConfig($authConfig); + + return $this; + } + + public function setApplicationName(string $applicationName): GoogleClient + { + $this->client->setApplicationName($applicationName); + + return $this; + } + + public function addScope(array $scopes): GoogleClient + { + $this->client->addScope($scopes); + + return $this; + } + + public function setAccessType(string $accessType = 'offline'): GoogleClient + { + $this->client->setAccessType($accessType); + + return $this; + } + + public function setPrompt(string $prompt = 'none'): GoogleClient + { + $this->client->setPrompt($prompt); + + return $this; + } + + public function setApprovalPrompt(string $approvalPrompt = 'offline'): GoogleClient { - return $this->client; + $this->client->setApprovalPrompt($approvalPrompt); + + return $this; + } + + public function setIncludeGrantedScopes(bool $includeGrantedScopes = true): GoogleClient + { + $this->client->setPrompt($includeGrantedScopes); + + return $this; } public function createAuthUrl(): void diff --git a/src/Providers/GoogleApiServiceProvider.php b/src/Providers/GoogleApiServiceProvider.php index 7ec5a5d..0dcea91 100644 --- a/src/Providers/GoogleApiServiceProvider.php +++ b/src/Providers/GoogleApiServiceProvider.php @@ -2,6 +2,7 @@ namespace TomShaw\GoogleApi\Providers; +use Google\Client; use Illuminate\Support\ServiceProvider; use TomShaw\GoogleApi\Contracts\GoogleClientInterface; use TomShaw\GoogleApi\GoogleClient; @@ -22,9 +23,9 @@ public function boot(): void public function register() { $this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'google-api'); - $this->mergeConfigFrom(__DIR__.'/../../config/scopes.php', 'google-api.scopes'); - $this->mergeConfigFrom(__DIR__.'/../../config/service.php', 'google-api.service'); + $this->mergeConfigFrom(__DIR__.'/../../config/scopes.php', 'google-api-scopes'); + $this->mergeConfigFrom(__DIR__.'/../../config/service.php', 'google-api-service'); - $this->app->bind(GoogleClientInterface::class, GoogleClient::class); + $this->app->bind(GoogleClientInterface::class, fn () => new GoogleClient(new Client())); } }