Skip to content

Commit

Permalink
chore: Adding docblocks to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 30, 2024
1 parent 7c15d42 commit 1b031b9
Show file tree
Hide file tree
Showing 15 changed files with 562 additions and 135 deletions.
12 changes: 12 additions & 0 deletions app/Actions/BackupDestinations/CheckS3Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@
use Exception;
use Illuminate\Support\Facades\Log;

/**
* Checks the connection to an S3 backup destination.
*
* This action verifies the reachability of an S3 backup destination
* and updates its status accordingly.
*/
class CheckS3Connection
{
/**
* Attempt to connect to the S3 backup destination.
*
* @param BackupDestination $backupDestination The backup destination to check
* @return bool True if connection is successful, false otherwise
*/
public function handle(BackupDestination $backupDestination): bool
{
if (! $backupDestination->isS3Connection()) {
Expand Down
8 changes: 8 additions & 0 deletions app/Facades/Greeting.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
use App\Services\GreetingService;
use Illuminate\Support\Facades\Facade;

/**
* Facade for the GreetingService.
*
* Provides a convenient static interface to the GreetingService functionality.
*/
class Greeting extends Facade
{
/**
* Get the registered name of the component.
*/
protected static function getFacadeAccessor(): string
{
return GreetingService::class;
Expand Down
91 changes: 61 additions & 30 deletions app/Models/BackupDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@
use Illuminate\Support\Facades\Log;
use RuntimeException;

/**
* Represents a backup destination in the system.
*
* This model handles various types of backup destinations including S3, custom S3, and local storage.
* It provides methods for managing the connection status and interacting with S3 services.
*/
class BackupDestination extends Model
{
/** @use HasFactory<BackupDestinationFactory> */
use HasFactory;

public const string TYPE_CUSTOM_S3 = 'custom_s3';

public const string TYPE_S3 = 's3';

public const string TYPE_LOCAL = 'local';

public const string STATUS_REACHABLE = 'reachable';

public const string STATUS_UNREACHABLE = 'unreachable';

public const string STATUS_UNKNOWN = 'unknown';

public const string STATUS_CHECKING = 'checking';

protected $guarded = [];
Expand All @@ -56,28 +57,52 @@ public function backupTasks(): HasMany
return $this->hasMany(BackupTask::class);
}

/**
* Get the human-readable type of the backup destination.
*/
public function type(): ?string
{
return match ($this->type) {
self::TYPE_S3 => 'S3',
self::TYPE_CUSTOM_S3 => (string) trans('Custom S3'),
self::TYPE_LOCAL => (string) trans('Local'),
default => null,
};
}

/**
* Check if the backup destination is an S3 connection.
*/
public function isS3Connection(): bool
{
return $this->type === self::TYPE_S3 || $this->type === self::TYPE_CUSTOM_S3;
return in_array($this->type, [self::TYPE_S3, self::TYPE_CUSTOM_S3], true);
}

/**
* Check if the backup destination is a local connection.
*/
public function isLocalConnection(): bool
{
return $this->type === self::TYPE_LOCAL;
}

/**
* Determine the S3 region for the backup destination.
*/
public function determineS3Region(): string
{
if ($this->type === BackupDestination::TYPE_CUSTOM_S3 && $this->custom_s3_region === null) {
return 'us-east-1'; // Dummy region to satisfy the S3Client constructor
if ($this->type === self::TYPE_CUSTOM_S3 && $this->custom_s3_region === null) {
return 'us-east-1'; // Default region for custom S3
}

/** @var string $customS3Region */
$customS3Region = $this->custom_s3_region;

return $customS3Region;
return (string) $this->custom_s3_region;
}

/**
* Get an S3 client instance for the backup destination.
*
* @throws RuntimeException If the destination is not an S3 connection or client creation fails.
*/
public function getS3Client(): S3Client
{
if (! $this->isS3Connection()) {
Expand Down Expand Up @@ -107,25 +132,8 @@ public function getS3Client(): S3Client
}

/**
* Get the type of the backup destination.
* Run the connection check for the backup destination.
*/
public function type(): ?string
{
if ($this->type === self::TYPE_S3) {
return 'S3';
}

if ($this->type === self::TYPE_CUSTOM_S3) {
return __('Custom S3');
}

if ($this->type === self::TYPE_LOCAL) {
return __('Local');
}

return null;
}

public function run(): void
{
if ($this->isS3Connection()) {
Expand All @@ -134,42 +142,65 @@ public function run(): void
}
}

/**
* Check if the backup destination is reachable.
*/
public function isReachable(): bool
{
return $this->status === self::STATUS_REACHABLE;
}

/**
* Check if the backup destination is unreachable.
*/
public function isUnreachable(): bool
{
return $this->status === self::STATUS_UNREACHABLE;
}

/**
* Check if the backup destination status is unknown.
*/
public function isUnknown(): bool
{
return $this->status === self::STATUS_UNKNOWN;
}

/**
* Check if the backup destination is currently being checked.
*/
public function isChecking(): bool
{
return $this->status === self::STATUS_CHECKING;
}

/**
* Mark the backup destination as being checked.
*/
public function markAsChecking(): void
{
$this->update(['status' => self::STATUS_CHECKING]);
}

/**
* Mark the backup destination as reachable.
*/
public function markAsReachable(): void
{
$this->update(['status' => self::STATUS_REACHABLE]);
}

/**
* Mark the backup destination as unreachable.
*/
public function markAsUnreachable(): void
{
$this->update(['status' => self::STATUS_UNREACHABLE]);
}

/**
* Define the casts for the model's attributes.
*
* @return string[]
*/
protected function casts(): array
Expand Down
Loading

0 comments on commit 1b031b9

Please sign in to comment.