|
4 | 4 |
|
5 | 5 | use App\Exceptions\InvalidTokenException; |
6 | 6 | use App\GPhoto; |
| 7 | +use Exception; |
7 | 8 | use Google\ApiCore\ApiException; |
8 | 9 | use Google\ApiCore\ValidationException; |
9 | 10 | use Google\Photos\Types\Album; |
|
12 | 13 |
|
13 | 14 | class ListAlbumsCommand extends Command |
14 | 15 | { |
| 16 | + /** |
| 17 | + * @var int |
| 18 | + */ |
| 19 | + protected int $retry = 3; |
| 20 | + |
15 | 21 | /** |
16 | 22 | * The signature of the command. |
17 | 23 | * |
@@ -39,30 +45,49 @@ public function handle() : void |
39 | 45 |
|
40 | 46 | $gphoto = new GPhoto($this->option('auth')); |
41 | 47 |
|
42 | | - $response = $gphoto->client()->listAlbums([ |
43 | | - 'excludeNonAppCreatedData' => true, |
44 | | - ]); |
| 48 | + try { |
| 49 | + $response = $gphoto->client()->listAlbums([ |
| 50 | + 'excludeNonAppCreatedData' => true, |
| 51 | + ]); |
45 | 52 |
|
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 | + } |
53 | 60 |
|
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 | + }); |
58 | 65 |
|
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 | + } |
64 | 71 |
|
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 | + } |
66 | 91 | } |
67 | 92 |
|
68 | 93 | /** |
|
0 commit comments