Skip to content

Commit

Permalink
Misc updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomshaw committed Oct 17, 2023
1 parent 94567b7 commit 564c118
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 23 deletions.
1 change: 0 additions & 1 deletion config/service.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
],
],
'calendar' => [
'calendar_id' => env('GOOGLE_CALENDAR_ID'),
'owner' => [
'name' => 'Your Name',
'email' => '[email protected]',
Expand Down
10 changes: 5 additions & 5 deletions src/Api/GoogleCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/Api/GoogleMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions src/Contracts/GoogleClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
59 changes: 50 additions & 9 deletions src/GoogleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

class GoogleClient implements GoogleClientInterface
{
protected Client $client;

public static $rules = [
'access_token' => 'required|string',
'refresh_token' => 'required|string',
Expand All @@ -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'));

Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/Providers/GoogleApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TomShaw\GoogleApi\Providers;

use Google\Client;
use Illuminate\Support\ServiceProvider;
use TomShaw\GoogleApi\Contracts\GoogleClientInterface;
use TomShaw\GoogleApi\GoogleClient;
Expand All @@ -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()));
}
}

0 comments on commit 564c118

Please sign in to comment.