Skip to content

Commit

Permalink
Fix Expired Token
Browse files Browse the repository at this point in the history
  • Loading branch information
SupianIDz committed Apr 9, 2023
1 parent d4c904f commit 0ade9e2
Showing 1 changed file with 45 additions and 20 deletions.
65 changes: 45 additions & 20 deletions app/Commands/ListAlbumsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Exceptions\InvalidTokenException;
use App\GPhoto;
use Exception;
use Google\ApiCore\ApiException;
use Google\ApiCore\ValidationException;
use Google\Photos\Types\Album;
Expand All @@ -12,6 +13,11 @@

class ListAlbumsCommand extends Command
{
/**
* @var int
*/
protected int $retry = 3;

/**
* The signature of the command.
*
Expand Down Expand Up @@ -39,30 +45,49 @@ public function handle() : void

$gphoto = new GPhoto($this->option('auth'));

$response = $gphoto->client()->listAlbums([
'excludeNonAppCreatedData' => true,
]);
try {
$response = $gphoto->client()->listAlbums([
'excludeNonAppCreatedData' => true,
]);

$results = [];
foreach ($response->iterateAllElements() as $album) {
/**
* @var Album $album
*/
$results[] = [$album->getTitle(), $album->getMediaItemsCount(), $album->getProductUrl(),];
}
$results = [];
foreach ($response->iterateAllElements() as $album) {
/**
* @var Album $album
*/
$results[] = [$album->getTitle(), $album->getMediaItemsCount(), $album->getProductUrl(),];
}

// sort by title
usort($results, function ($a, $b) {
return $a[0] <=> $b[0];
});
// sort by title
usort($results, function ($a, $b) {
return $a[0] <=> $b[0];
});

$numb = 1;
$data = [];
foreach ($results as $item) {
$data[] = [$numb++, ...$item];
}
$numb = 1;
$data = [];
foreach ($results as $item) {
$data[] = [$numb++, ...$item];
}

$this->table(['#', 'TITLE', 'MEDIA', 'URL'], $data);
$this->table(['#', 'TITLE', 'MEDIA', 'URL'], $data);
} catch (Exception) {
echo PHP_EOL;

$this->components->warn(
'EXPIRED TOKEN, RETRYING'
);

$gphoto->revoke();

$this->call('auth:reload', [
'name' => $this->option('auth'),
]);

if ($this->retry > 0) {
$this->retry--;
$this->handle();
}
}
}

/**
Expand Down

0 comments on commit 0ade9e2

Please sign in to comment.