Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Aug 20, 2024
1 parent cf56bae commit e58baee
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 40 deletions.
4 changes: 3 additions & 1 deletion src/Actions/ManagesBackupDestinations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace VanguardBackup\Vanguard\Actions;

use Exception;
use VanguardBackup\Vanguard\Resources\BackupDestination;

trait ManagesBackupDestinations
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Actions/ManagesBackupTaskLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ trait ManagesBackupTaskLogs
* Get the collection of backup task logs.
*
* @return BackupTaskLog[]
* @throws \Exception
*/
public function backupTaskLogs(array $query = []): array
{
Expand Down
4 changes: 3 additions & 1 deletion src/Actions/ManagesBackupTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace VanguardBackup\Vanguard\Actions;

use Exception;
use VanguardBackup\Vanguard\Resources\BackupTask;

trait ManagesBackupTasks
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Actions/ManagesNotificationStreams.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace VanguardBackup\Vanguard\Actions;

use Exception;
use VanguardBackup\Vanguard\Resources\NotificationStream;

trait ManagesNotificationStreams
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Actions/ManagesRemoteServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace VanguardBackup\Vanguard\Actions;

use Exception;
use VanguardBackup\Vanguard\Resources\RemoteServer;

trait ManagesRemoteServers
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Actions/ManagesTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace VanguardBackup\Vanguard\Actions;

use Exception;
use VanguardBackup\Vanguard\Resources\Tag;

trait ManagesTags
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/Exceptions/MaintenanceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace VanguardBackup\Vanguard\Exceptions;

use Exception;

class MaintenanceException extends Exception
{
/**
* Create a new exception instance.
*/
public function __construct(string $message = 'The application is reporting as being in maintenance mode.')
{
parent::__construct($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Exception;

class RateLimitExceededException extends Exception
class TooManyRequestsException extends Exception
{
/**
* The timestamp when the rate limit will be reset.
Expand Down
47 changes: 27 additions & 20 deletions src/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,51 @@
namespace VanguardBackup\Vanguard;

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use VanguardBackup\Vanguard\Exceptions\MaintenanceException;
use VanguardBackup\Vanguard\Exceptions\NotFoundException;
use VanguardBackup\Vanguard\Exceptions\RateLimitExceededException;
use VanguardBackup\Vanguard\Exceptions\TooManyRequestsException;
use VanguardBackup\Vanguard\Exceptions\ValidationException;

trait MakesHttpRequests
{
/**
* Send a GET request to VanguardBackup API and return the response.
*
* @param string $uri
* @param string $uri
*
* @throws Exception
* @return mixed
* @throws GuzzleException
*/
public function get($uri): mixed
public function get(string $uri): mixed
{
return $this->request('GET', $uri);
}

/**
* 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);
}

/**
* 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);
}
Expand All @@ -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
{
Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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),
};
}
Expand Down
20 changes: 10 additions & 10 deletions src/VanguardClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/VanguardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
6 changes: 3 additions & 3 deletions tests/VanguardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,7 +97,7 @@

try {
$vanguard->tags();
} catch (RateLimitExceededException $e) {
} catch (TooManyRequestsException $e) {
expect($e->getRateLimitResetsAt())->toBe($timestamp);
}
})->with('vanguard');
Expand All @@ -109,7 +109,7 @@

try {
$vanguard->tags();
} catch (RateLimitExceededException $e) {
} catch (TooManyRequestsException $e) {
expect($e->getRateLimitResetsAt())->toBeNull();
}
})->with('vanguard');
Expand Down

0 comments on commit e58baee

Please sign in to comment.