Skip to content

Commit

Permalink
fix: error handling & reporting, bucket naming & refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
secrethash committed Dec 11, 2024
1 parent bc3ade9 commit 689956a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 48 deletions.
52 changes: 33 additions & 19 deletions src/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class Bucket
/**
* @var string|null Name of the Created Bucket
*/
protected string|null $createdBucketName;
protected string|null $bucketName;

/**
* @var \Aws\Exception\AwsException|null Exception Error Bag
*/
protected AwsException|null $e;
protected AwsException|null $e = null;

public function __construct(
protected Tenant $tenant
Expand All @@ -66,7 +66,6 @@ private function setupCredentials()
/**
* Create Tenant Specific Bucket
*
* @access public
* @return self $this
*/
public function createTenantBucket(): self
Expand All @@ -79,7 +78,6 @@ public function createTenantBucket(): self
/**
* Delete Tenant Specific Bucket
*
* @access public
* @return self $this
*/
public function deleteTenantBucket(): self
Expand All @@ -94,7 +92,6 @@ public function deleteTenantBucket(): self
*
* @param string $name Name of the S3 Bucket
* @param \Aws\Credentials\Credentials $credentials AWS Credentials Object
* @access public
* @return self $this
*/
public function createBucket(string $name, Credentials $credentials): self
Expand All @@ -108,21 +105,22 @@ public function createBucket(string $name, Credentials $credentials): self
"version" => $this->version,
"use_path_style_endpoint" => $this->pathStyle,
];
$this->bucketName = $this->formatBucketName($name);

$client = new S3Client($params);

try {
$exec = $client->createBucket([
$client->createBucket([
'Bucket' => $name,
]);
$this->createdBucketName = $name;

// Update Tenant
$this->tenant->tenant_bucket = $name;
$this->tenant->save();
} catch (AwsException $e) {
$this->e = $e;
Log::error($this->getErrorMessage());
throw_if(config('app.debug', false), $e);
Log::critical($this->getErrorMessage());
}

event(new CreatedBucket($this->tenant));
Expand All @@ -135,12 +133,11 @@ public function createBucket(string $name, Credentials $credentials): self
*
* @param string $name Name of the S3 Bucket
* @param \Aws\Credentials\Credentials $credentials AWS Credentials Object
* @access public
* @return self $this
*/
public function deleteBucket(string $name, Credentials $credentials): self
{
event(new DeletingBucket($this->tenant));
event(new DeletingBucket(tenant: $this->tenant));

$params = [
"credentials" => $credentials,
Expand All @@ -153,15 +150,13 @@ public function deleteBucket(string $name, Credentials $credentials): self
$client = new S3Client($params);

try {
$exec = $client->deleteBucket([
$client->deleteBucket([
'Bucket' => $name,
]);
} catch (AwsException $e) {
$this->e = $e;
if (config('tenant-buckets.errors.throw', true)) {
throw $e;
}
Log::error($this->getErrorMessage());
throw_if(config('app.debug'), $e);
Log::critical($this->getErrorMessage());
}

event(new DeletedBucket($this->tenant));
Expand All @@ -176,7 +171,17 @@ public function deleteBucket(string $name, Credentials $credentials): self
*/
public function getBucketName(): string|null
{
return $this->createdBucketName;
return $this->bucketName;
}

/**
* Format Bucket Name according to AWS S3 Bucket Naming Rules
* @see https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
* @param string $name
*/
protected function formatBucketName(string $name): string
{
return str(preg_replace('/[^a-zA-Z0-9]/', '', $name))->lower()->toString();
}

/**
Expand All @@ -186,9 +191,18 @@ public function getBucketName(): string|null
*/
public function getErrorMessage(): string|null
{
return ($this->e) ?
"Error: " . $this->e->getAwsErrorMessage() :
null;
if ($this->e) {
$data = [
'tenant' => $this->tenant->id,
'bucket' => $this->bucketName,
'error_code' => $this->e?->getAwsErrorCode(),
'error_type' => $this->e?->getAwsErrorType(),
'error_message' => $this->e?->getAwsErrorMessage(),
'response' => $this->e?->getResponse(),
];
return "[tenant-buckets] Error: (Tenant ID: {$this->tenant->id}) {$this->e->getAwsErrorMessage()} ". json_encode($data);
}
return null;
}

/**
Expand Down
18 changes: 2 additions & 16 deletions src/Jobs/CreateTenantBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ class CreateTenantBucket implements ShouldQueue
use Queueable;
use SerializesModels;

/**
* Current Tenant
* @access protected
*/
protected $tenant;

/**
* The number of times the job may be attempted.
*
Expand All @@ -42,11 +36,7 @@ class CreateTenantBucket implements ShouldQueue
*
* @return void
*/
public function __construct(Tenant $tenant)
{
//
$this->tenant = $tenant;
}
public function __construct(protected Tenant $tenant) {}

/**
* Execute the job.
Expand All @@ -55,11 +45,7 @@ public function __construct(Tenant $tenant)
*/
public function handle()
{
$bucket = new Bucket($this->tenant);
$create = $bucket->createTenantBucket();

// $this->tenant->tenant_bucket = $create->getBucketName();
// $this->tenant->save();
(new Bucket($this->tenant))->createTenantBucket();
}

/**
Expand Down
15 changes: 2 additions & 13 deletions src/Jobs/DeleteTenantBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ class DeleteTenantBucket implements ShouldQueue
use Queueable;
use SerializesModels;

/**
* Current Tenant
* @access protected
*/
protected $tenant;

/**
* The number of times the job may be attempted.
*
Expand All @@ -42,11 +36,7 @@ class DeleteTenantBucket implements ShouldQueue
*
* @return void
*/
public function __construct(Tenant $tenant)
{
//
$this->tenant = $tenant;
}
public function __construct(protected Tenant $tenant) {}

/**
* Execute the job.
Expand All @@ -55,8 +45,7 @@ public function __construct(Tenant $tenant)
*/
public function handle()
{
$bucket = new Bucket($this->tenant);
$delete = $bucket->deleteTenantBucket();
(new Bucket($this->tenant))->deleteTenantBucket();
}

/**
Expand Down

0 comments on commit 689956a

Please # to comment.