Skip to content

Commit 0ade9e2

Browse files
committed
Fix Expired Token
1 parent d4c904f commit 0ade9e2

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

app/Commands/ListAlbumsCommand.php

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Exceptions\InvalidTokenException;
66
use App\GPhoto;
7+
use Exception;
78
use Google\ApiCore\ApiException;
89
use Google\ApiCore\ValidationException;
910
use Google\Photos\Types\Album;
@@ -12,6 +13,11 @@
1213

1314
class ListAlbumsCommand extends Command
1415
{
16+
/**
17+
* @var int
18+
*/
19+
protected int $retry = 3;
20+
1521
/**
1622
* The signature of the command.
1723
*
@@ -39,30 +45,49 @@ public function handle() : void
3945

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

42-
$response = $gphoto->client()->listAlbums([
43-
'excludeNonAppCreatedData' => true,
44-
]);
48+
try {
49+
$response = $gphoto->client()->listAlbums([
50+
'excludeNonAppCreatedData' => true,
51+
]);
4552

46-
$results = [];
47-
foreach ($response->iterateAllElements() as $album) {
48-
/**
49-
* @var Album $album
50-
*/
51-
$results[] = [$album->getTitle(), $album->getMediaItemsCount(), $album->getProductUrl(),];
52-
}
53+
$results = [];
54+
foreach ($response->iterateAllElements() as $album) {
55+
/**
56+
* @var Album $album
57+
*/
58+
$results[] = [$album->getTitle(), $album->getMediaItemsCount(), $album->getProductUrl(),];
59+
}
5360

54-
// sort by title
55-
usort($results, function ($a, $b) {
56-
return $a[0] <=> $b[0];
57-
});
61+
// sort by title
62+
usort($results, function ($a, $b) {
63+
return $a[0] <=> $b[0];
64+
});
5865

59-
$numb = 1;
60-
$data = [];
61-
foreach ($results as $item) {
62-
$data[] = [$numb++, ...$item];
63-
}
66+
$numb = 1;
67+
$data = [];
68+
foreach ($results as $item) {
69+
$data[] = [$numb++, ...$item];
70+
}
6471

65-
$this->table(['#', 'TITLE', 'MEDIA', 'URL'], $data);
72+
$this->table(['#', 'TITLE', 'MEDIA', 'URL'], $data);
73+
} catch (Exception) {
74+
echo PHP_EOL;
75+
76+
$this->components->warn(
77+
'EXPIRED TOKEN, RETRYING'
78+
);
79+
80+
$gphoto->revoke();
81+
82+
$this->call('auth:reload', [
83+
'name' => $this->option('auth'),
84+
]);
85+
86+
if ($this->retry > 0) {
87+
$this->retry--;
88+
$this->handle();
89+
}
90+
}
6691
}
6792

6893
/**

0 commit comments

Comments
 (0)