From e58baeecf29593be4408e3b29854d5c4d4a09f15 Mon Sep 17 00:00:00 2001 From: Lewis Larsen Date: Tue, 20 Aug 2024 09:05:36 +0100 Subject: [PATCH] improvements --- src/Actions/ManagesBackupDestinations.php | 4 +- src/Actions/ManagesBackupTaskLogs.php | 1 + src/Actions/ManagesBackupTasks.php | 4 +- src/Actions/ManagesNotificationStreams.php | 4 +- src/Actions/ManagesRemoteServers.php | 4 +- src/Actions/ManagesTags.php | 4 +- src/Exceptions/MaintenanceException.php | 18 +++++++ ...ption.php => TooManyRequestsException.php} | 2 +- src/MakesHttpRequests.php | 47 +++++++++++-------- src/VanguardClient.php | 20 ++++---- src/VanguardServiceProvider.php | 2 +- tests/VanguardTest.php | 6 +-- 12 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 src/Exceptions/MaintenanceException.php rename src/Exceptions/{RateLimitExceededException.php => TooManyRequestsException.php} (92%) diff --git a/src/Actions/ManagesBackupDestinations.php b/src/Actions/ManagesBackupDestinations.php index 2d43a18..fcf0567 100644 --- a/src/Actions/ManagesBackupDestinations.php +++ b/src/Actions/ManagesBackupDestinations.php @@ -4,6 +4,7 @@ namespace VanguardBackup\Vanguard\Actions; +use Exception; use VanguardBackup\Vanguard\Resources\BackupDestination; trait ManagesBackupDestinations @@ -12,8 +13,9 @@ trait ManagesBackupDestinations * Get the collection of backup destinations. * * @return BackupDestination[] + * @throws Exception */ - public function backupDestinations() + public function backupDestinations(): array { return $this->transformCollection( $this->get('backup-destinations')['data'], BackupDestination::class diff --git a/src/Actions/ManagesBackupTaskLogs.php b/src/Actions/ManagesBackupTaskLogs.php index 7a43f80..7e53e46 100644 --- a/src/Actions/ManagesBackupTaskLogs.php +++ b/src/Actions/ManagesBackupTaskLogs.php @@ -12,6 +12,7 @@ trait ManagesBackupTaskLogs * Get the collection of backup task logs. * * @return BackupTaskLog[] + * @throws \Exception */ public function backupTaskLogs(array $query = []): array { diff --git a/src/Actions/ManagesBackupTasks.php b/src/Actions/ManagesBackupTasks.php index b9c2429..3ef8ef4 100644 --- a/src/Actions/ManagesBackupTasks.php +++ b/src/Actions/ManagesBackupTasks.php @@ -4,6 +4,7 @@ namespace VanguardBackup\Vanguard\Actions; +use Exception; use VanguardBackup\Vanguard\Resources\BackupTask; trait ManagesBackupTasks @@ -12,8 +13,9 @@ trait ManagesBackupTasks * Get the collection of backup tasks. * * @return BackupTask[] + * @throws Exception */ - public function backupTasks() + public function backupTasks(): array { return $this->transformCollection( $this->get('backup-tasks')['data'], BackupTask::class diff --git a/src/Actions/ManagesNotificationStreams.php b/src/Actions/ManagesNotificationStreams.php index ff35cbe..bff0137 100644 --- a/src/Actions/ManagesNotificationStreams.php +++ b/src/Actions/ManagesNotificationStreams.php @@ -4,6 +4,7 @@ namespace VanguardBackup\Vanguard\Actions; +use Exception; use VanguardBackup\Vanguard\Resources\NotificationStream; trait ManagesNotificationStreams @@ -12,8 +13,9 @@ trait ManagesNotificationStreams * Get the collection of notification streams. * * @return NotificationStream[] + * @throws Exception */ - public function notificationStreams() + public function notificationStreams(): array { return $this->transformCollection( $this->get('notification-streams')['data'], NotificationStream::class diff --git a/src/Actions/ManagesRemoteServers.php b/src/Actions/ManagesRemoteServers.php index 2d647a2..0ce6af4 100644 --- a/src/Actions/ManagesRemoteServers.php +++ b/src/Actions/ManagesRemoteServers.php @@ -4,6 +4,7 @@ namespace VanguardBackup\Vanguard\Actions; +use Exception; use VanguardBackup\Vanguard\Resources\RemoteServer; trait ManagesRemoteServers @@ -12,8 +13,9 @@ trait ManagesRemoteServers * Get the collection of remote servers. * * @return RemoteServer[] + * @throws Exception */ - public function remoteServers() + public function remoteServers(): array { return $this->transformCollection( $this->get('remote-servers')['data'], RemoteServer::class diff --git a/src/Actions/ManagesTags.php b/src/Actions/ManagesTags.php index 807a3f4..f0e1fa9 100644 --- a/src/Actions/ManagesTags.php +++ b/src/Actions/ManagesTags.php @@ -4,6 +4,7 @@ namespace VanguardBackup\Vanguard\Actions; +use Exception; use VanguardBackup\Vanguard\Resources\Tag; trait ManagesTags @@ -12,8 +13,9 @@ trait ManagesTags * Get the collection of tags. * * @return Tag[] + * @throws Exception */ - public function tags() + public function tags(): array { return $this->transformCollection( $this->get('tags')['tags'], Tag::class diff --git a/src/Exceptions/MaintenanceException.php b/src/Exceptions/MaintenanceException.php new file mode 100644 index 0000000..9cdd1e3 --- /dev/null +++ b/src/Exceptions/MaintenanceException.php @@ -0,0 +1,18 @@ +request('GET', $uri); } @@ -28,11 +31,12 @@ public function get($uri): mixed /** * Send a POST request to VanguardBackup API and return the response. * - * @param string $uri - * - * @throws Exception + * @param string $uri + * @param array $payload + * @return mixed + * @throws GuzzleException */ - public function post($uri, array $payload = []): mixed + public function post(string $uri, array $payload = []): mixed { return $this->request('POST', $uri, $payload); } @@ -40,11 +44,12 @@ public function post($uri, array $payload = []): mixed /** * Send a PUT request to VanguardBackup API and return the response. * - * @param string $uri - * - * @throws Exception + * @param string $uri + * @param array $payload + * @return mixed + * @throws GuzzleException */ - public function put($uri, array $payload = []): mixed + public function put(string $uri, array $payload = []): mixed { return $this->request('PUT', $uri, $payload); } @@ -54,7 +59,7 @@ public function put($uri, array $payload = []): mixed * * @param string $uri * - * @throws Exception + * @throws Exception|GuzzleException */ public function delete($uri, array $payload = []): mixed { @@ -64,12 +69,13 @@ public function delete($uri, array $payload = []): mixed /** * Send a request to VanguardBackup API and return the response. * - * @param string $method - * @param string $uri - * - * @throws Exception + * @param string $method + * @param string $uri + * @param array $payload + * @return mixed + * @throws GuzzleException */ - protected function request($method, $uri, array $payload = []): mixed + protected function request(string $method, string $uri, array $payload = []): mixed { $options = $this->prepareRequestPayload($payload); @@ -113,7 +119,7 @@ protected function handleResponse(ResponseInterface $response): mixed * * @throws NotFoundException * @throws ValidationException - * @throws RateLimitExceededException + * @throws TooManyRequestsException * @throws Exception */ protected function handleRequestError(ResponseInterface $response): void @@ -124,11 +130,12 @@ protected function handleRequestError(ResponseInterface $response): void throw match ($statusCode) { 422 => new ValidationException(json_decode($body, true, 512, JSON_THROW_ON_ERROR)), 404 => new NotFoundException, - 429 => new RateLimitExceededException( + 429 => new TooManyRequestsException( $response->hasHeader('x-ratelimit-reset') ? (int) $response->getHeader('x-ratelimit-reset')[0] : null ), + 503 => new MaintenanceException, default => new RuntimeException($body), }; } diff --git a/src/VanguardClient.php b/src/VanguardClient.php index 8ff4ecf..35ee0af 100644 --- a/src/VanguardClient.php +++ b/src/VanguardClient.php @@ -36,11 +36,11 @@ class VanguardClient /** * Create a new VanguardClient instance. * - * @param string|null $apiKey - * @param string|null $baseUrl + * @param string|null $apiKey + * @param string|null $baseUrl * @return void */ - public function __construct($apiKey = null, $baseUrl = null, ?HttpClient $httpClient = null) + public function __construct(string $apiKey = null, string $baseUrl = null, ?HttpClient $httpClient = null) { if (! is_null($baseUrl)) { $this->setBaseUrl($baseUrl); @@ -58,12 +58,12 @@ public function __construct($apiKey = null, $baseUrl = null, ?HttpClient $httpCl /** * Transform the items of the collection to the given class. * - * @param array $collection - * @param string $class - * @param array $extraData + * @param array $collection + * @param string $class + * @param array $extraData * @return array */ - protected function transformCollection($collection, $class, $extraData = []) + protected function transformCollection(array $collection, string $class, array $extraData = []): array { return array_map(function ($data) use ($class, $extraData) { return new $class($data + $extraData, $this); @@ -73,11 +73,11 @@ protected function transformCollection($collection, $class, $extraData = []) /** * Set the API key and set up the HTTP client. * - * @param string $apiKey - * @param \GuzzleHttp\Client|null $httpClient + * @param string $apiKey + * @param HttpClient|null $httpClient * @return $this */ - public function setApiKey($apiKey, $httpClient = null): static + public function setApiKey(string $apiKey, HttpClient $httpClient = null): static { $this->apiKey = $apiKey; diff --git a/src/VanguardServiceProvider.php b/src/VanguardServiceProvider.php index 820de35..10f2ab7 100644 --- a/src/VanguardServiceProvider.php +++ b/src/VanguardServiceProvider.php @@ -12,7 +12,7 @@ class VanguardServiceProvider extends ServiceProvider /** * Register any application services. */ - public function register() + public function register(): void { $this->app->singleton(VanguardManager::class, function ($app) { return new VanguardManager($app['config']->get('services.vanguard.token')); diff --git a/tests/VanguardTest.php b/tests/VanguardTest.php index ddadc84..3203f00 100644 --- a/tests/VanguardTest.php +++ b/tests/VanguardTest.php @@ -5,7 +5,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response; use VanguardBackup\Vanguard\Exceptions\NotFoundException; -use VanguardBackup\Vanguard\Exceptions\RateLimitExceededException; +use VanguardBackup\Vanguard\Exceptions\TooManyRequestsException; use VanguardBackup\Vanguard\Exceptions\ValidationException; use VanguardBackup\Vanguard\Resources\Tag; use VanguardBackup\Vanguard\VanguardClient; @@ -97,7 +97,7 @@ try { $vanguard->tags(); - } catch (RateLimitExceededException $e) { + } catch (TooManyRequestsException $e) { expect($e->getRateLimitResetsAt())->toBe($timestamp); } })->with('vanguard'); @@ -109,7 +109,7 @@ try { $vanguard->tags(); - } catch (RateLimitExceededException $e) { + } catch (TooManyRequestsException $e) { expect($e->getRateLimitResetsAt())->toBeNull(); } })->with('vanguard');