Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Contracts/TokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Dacastro4\LaravelGmail\Contracts;

interface TokenRepository
{
public function tokenExists(string $fileName): bool;

public function getToken(string $fileName, bool $allowJsonEncrypt): array;

Check failure on line 11 in src/Contracts/TokenRepository.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Dacastro4\LaravelGmail\Contracts\TokenRepository::getToken() return type has no value type specified in iterable type array.

public function storeToken(string $fileName, array $config, bool $allowJsonEncrypt): void;

Check failure on line 13 in src/Contracts/TokenRepository.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Dacastro4\LaravelGmail\Contracts\TokenRepository::storeToken() has parameter $config with no value type specified in iterable type array.

public function deleteToken(string $fileName, bool $allowJsonEncrypt): void;
}
73 changes: 16 additions & 57 deletions src/GmailConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Dacastro4\LaravelGmail;

use Dacastro4\LaravelGmail\Contracts\TokenRepository;
use Dacastro4\LaravelGmail\Traits\Configurable;
use Dacastro4\LaravelGmail\Traits\HasLabels;
use Google_Client;
use Google_Service_Gmail;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class GmailConnection extends Google_Client
{
Expand All @@ -29,14 +29,18 @@

protected mixed $token = null;

private mixed $configuration;

Check failure on line 32 in src/GmailConnection.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Dacastro4\LaravelGmail\GmailConnection::$configuration is never read, only written.

public ?string $userId = null;

public function __construct(mixed $config = null, ?string $userId = null)
protected TokenRepository $tokenRepository;

public function __construct(TokenRepository $tokenRepository, mixed $config = null, ?string $userId = null)

Check failure on line 38 in src/GmailConnection.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Dacastro4\LaravelGmail\GmailConnection::__construct() has parameter $config with no value type specified in iterable type array.
{
$this->app = Container::getInstance();

$this->tokenRepository = $tokenRepository;

$this->userId = $userId;

$this->configConstruct($config);
Expand All @@ -53,29 +57,29 @@

}

public function getTokenRepository(): TokenRepository
{
return $this->tokenRepository;
}

public function getUserId(): ?string
{
return $this->userId;
}

/**
* Check and return true if the user has previously logged in without checking if the token needs to refresh
*
* @return bool
*/
public function checkPreviouslyLoggedIn(): bool
{
$fileName = $this->getFileName();
$file = "gmail/tokens/$fileName.json";
$disk = Storage::disk('local');
$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt'];

if (! $disk->exists($file)) {
if (! $this->tokenRepository->tokenExists($fileName)) {
return false;
}

$contents = $disk->get($file);
$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt'];
$savedConfigToken = json_decode($allowJsonEncrypt ? decrypt($contents) : $contents, true);
$savedConfigToken = $this->tokenRepository->getToken($fileName, $allowJsonEncrypt);

return ! empty($savedConfigToken['access_token']);
}
Expand Down Expand Up @@ -136,58 +140,33 @@
/**
* @param array|string $token
*/
public function setAccessToken($token)

Check failure on line 143 in src/GmailConnection.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Dacastro4\LaravelGmail\GmailConnection::setAccessToken() has parameter $token with no value type specified in iterable type array.

Check failure on line 143 in src/GmailConnection.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Dacastro4\LaravelGmail\GmailConnection::setAccessToken() has no return type specified.
{
parent::setAccessToken($token);
}

public function setBothAccessToken(array|string $token): void

Check failure on line 148 in src/GmailConnection.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Dacastro4\LaravelGmail\GmailConnection::setBothAccessToken() has parameter $token with no value type specified in iterable type array.
{
$this->setAccessToken($token);
$this->saveAccessToken($token);

Check failure on line 151 in src/GmailConnection.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $config of method Dacastro4\LaravelGmail\GmailConnection::saveAccessToken() expects array, array|string given.
}

/**
* Save the credentials in a file
*/
public function saveAccessToken(array $config): void

Check failure on line 157 in src/GmailConnection.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Dacastro4\LaravelGmail\GmailConnection::saveAccessToken() has parameter $config with no value type specified in iterable type array.
{
$disk = Storage::disk('local');
$fileName = $this->getFileName();
$file = "gmail/tokens/$fileName.json";
$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt'];
$config['email'] = $this->emailAddress;

if ($disk->exists($file)) {

if (empty($config['email'])) {
if ($allowJsonEncrypt) {
$savedConfigToken = json_decode(decrypt($disk->get($file)), true);
} else {
$savedConfigToken = json_decode($disk->get($file), true);
}
if (isset($savedConfigToken['email'])) {
$config['email'] = $savedConfigToken['email'];
}
}

$disk->delete($file);
}

if ($allowJsonEncrypt) {
$disk->put($file, encrypt(json_encode($config)));
} else {
$disk->put($file, json_encode($config));
}

$this->tokenRepository->storeToken($fileName, $config, $allowJsonEncrypt);
}

/**
* @return array|string
*
* @throws \Exception
*/
public function makeToken(Request $request): array|string

Check failure on line 169 in src/GmailConnection.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Dacastro4\LaravelGmail\GmailConnection::makeToken() return type has no value type specified in iterable type array.
{
if (! $this->check()) {
$code = (string) $request->input('code', null);
Expand All @@ -213,8 +192,6 @@

/**
* Check
*
* @return bool
*/
public function check(): bool
{
Expand All @@ -223,8 +200,6 @@

/**
* Gets user profile from Gmail
*
* @return \Google_Service_Gmail_Profile
*/
public function getProfile(): \Google_Service_Gmail_Profile
{
Expand All @@ -246,22 +221,10 @@
*/
public function deleteAccessToken(): void
{
$disk = Storage::disk('local');
$fileName = $this->getFileName();
$file = "gmail/tokens/$fileName.json";

$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt'];

if ($disk->exists($file)) {
$disk->delete($file);
}

if ($allowJsonEncrypt) {
$disk->put($file, encrypt(json_encode([])));
} else {
$disk->put($file, json_encode([]));
}

$this->tokenRepository->deleteToken($fileName, $allowJsonEncrypt);
}

private function haveReadScope(): bool
Expand All @@ -275,8 +238,6 @@
* users.stop receiving push notifications for the given user mailbox.
*
* @param string $userEmail Email address
* @param array $optParams
* @return \Google_Service_Gmail_Stop
*/
public function stopWatch(string $userEmail, array $optParams = []): \Google_Service_Gmail_Stop
{
Expand All @@ -299,8 +260,6 @@

/**
* Lists the history of all changes to the given mailbox. History results are returned in chronological order (increasing historyId).
*
* @return \Google\Service\Gmail\ListHistoryResponse
*/
public function historyList(string $userEmail, array $params): \Google\Service\Gmail\ListHistoryResponse
{
Expand Down
5 changes: 3 additions & 2 deletions src/LaravelGmailClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@

namespace Dacastro4\LaravelGmail;

use Dacastro4\LaravelGmail\Contracts\TokenRepository;
use Dacastro4\LaravelGmail\Exceptions\AuthException;
use Dacastro4\LaravelGmail\Services\Message;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Redirect;

class LaravelGmailClass extends GmailConnection
{
public function __construct(mixed $config, ?string $userId = null)
public function __construct(mixed $config, TokenRepository $tokenRepository, ?string $userId = null)
{
if (class_basename($config) === 'Application') {
$config = $config['config'];
}

parent::__construct($config, $userId);
parent::__construct($tokenRepository, $config, $userId);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/LaravelGmailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Dacastro4\LaravelGmail;

use Dacastro4\LaravelGmail\Contracts\TokenRepository;
use Dacastro4\LaravelGmail\Repositories\StorageTokenRepository;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;

Expand All @@ -18,9 +20,11 @@ public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/config/gmail.php', 'gmail');

$this->app->bind(TokenRepository::class, fn (): TokenRepository => new StorageTokenRepository);

// Main Service
$this->app->bind('laravelgmail', function ($app): LaravelGmailClass {
return new LaravelGmailClass($app['config']);
return new LaravelGmailClass($app['config'], $app->make(TokenRepository::class));
});
}
}
68 changes: 68 additions & 0 deletions src/Repositories/StorageTokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Dacastro4\LaravelGmail\Repositories;

use Dacastro4\LaravelGmail\Contracts\TokenRepository;
use Illuminate\Support\Facades\Storage;

class StorageTokenRepository implements TokenRepository
{
public function tokenExists(string $fileName): bool
{
$file = "gmail/tokens/$fileName.json";

return Storage::disk('local')->exists($file);
}

public function getToken(string $fileName, bool $allowJsonEncrypt): array
{
$file = "gmail/tokens/$fileName.json";
$disk = Storage::disk('local');

if (! $disk->exists($file)) {
return [];
}

$contents = $disk->get($file);

return json_decode($allowJsonEncrypt ? decrypt($contents) : $contents, true) ?? [];
}

public function storeToken(string $fileName, array $config, bool $allowJsonEncrypt): void
{
$disk = Storage::disk('local');
$file = "gmail/tokens/$fileName.json";

if ($disk->exists($file)) {
$savedConfigToken = $this->getToken($fileName, $allowJsonEncrypt);
if (empty($config['email']) && isset($savedConfigToken['email'])) {
$config['email'] = $savedConfigToken['email'];
}
$disk->delete($file);
}

if ($allowJsonEncrypt) {
$disk->put($file, encrypt(json_encode($config)));
} else {
$disk->put($file, json_encode($config));
}
}

public function deleteToken(string $fileName, bool $allowJsonEncrypt): void
{
$disk = Storage::disk('local');
$file = "gmail/tokens/$fileName.json";

if ($disk->exists($file)) {
$disk->delete($file);
}

if ($allowJsonEncrypt) {
$disk->put($file, encrypt(json_encode([])));
} else {
$disk->put($file, json_encode([]));
}
}
}
7 changes: 3 additions & 4 deletions src/Services/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Dacastro4\LaravelGmail\Traits\Filterable;
use Dacastro4\LaravelGmail\Traits\SendsParameters;
use Google_Service_Gmail;
use Google_Service_Gmail_ListMessagesResponse;
use Google_Service_Gmail_Message;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -71,7 +70,7 @@ public function all(?string $pageToken = null): Collection

if (! $this->preload) {
foreach ($messages as $message) {
$mails[] = new Mail($message, $this->preload, $this->client->userId);
$mails[] = new Mail($this->client->getTokenRepository(), $message, $this->preload, $this->client->userId);
}
} else {
$mails = count($messages) > 0 ? $this->batchRequest($messages) : [];
Expand Down Expand Up @@ -107,7 +106,7 @@ public function get(string $id): Mail
{
$message = $this->getRequest($id);

return new Mail($message, false, $this->client->userId);
return new Mail($this->client->getTokenRepository(), $message, false, $this->client->userId);
}

/**
Expand All @@ -130,7 +129,7 @@ public function batchRequest(array $allMessages): array
$messages = [];

foreach ($messagesBatch as $message) {
$messages[] = new Mail($message, false, $this->client->userId);
$messages[] = new Mail($this->client->getTokenRepository(), $message, false, $this->client->userId);
}

return $messages;
Expand Down
Loading