Skip to content

Commit

Permalink
add: support for Tenancy v4 & refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
secrethash committed Dec 8, 2024
1 parent 6e3734e commit a895028
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require": {
"php": "^8.0",
"aws/aws-sdk-php": "~3.0",
"stancl/tenancy": "^3.4"
"stancl/tenancy": "dev-master"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/Bootstrappers/TenantBucketBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public function __construct(Application $app)
$this->orignalBucket = $this->app['config']['filesystems.disks.s3.bucket'];
}

public function bootstrap(Tenant $tenant)
public function bootstrap(Tenant $tenant): void
{
// Select Bucket Name
$bucket = 'tenant'.$tenant->getTenantKey();
$bucket = $tenant->tenant_bucket ?? $bucket;
$this->app['config']['filesystems.disks.s3.bucket'] = $bucket;
}

public function revert()
public function revert(): void
{
//
$this->app['config']['filesystems.disks.s3.bucket'] = $this->orignalBucket;
Expand Down
76 changes: 40 additions & 36 deletions src/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,54 @@

class Bucket
{

/**
* @var \AWS\Credentials\Credentials Credentials Object
*/
protected $credentials;

/**
* @var string AWS/Minio Endpoint
*/
protected $endpoint;

/**
* @var string AWS/Minio Region
*/
protected $region;

protected string $version = "2006-03-01";

/**
* @var bool Use Path style endpoint (used for minio)
*/
protected bool $pathStyle = false;

/**
* @access public
* @var Stancl\Tenancy\Contracts\TenantWithDatabase Tenant
* @var AWS Credentials Object
* @var AWS/Minio Endpoint
* @var AWS/Minio Region
* @var Use Path style endpoint (used for minio)
* @access protected
* @var string|null Name of the Created Bucket
* @var Aws\Exception\AwsException|null Exception Error Bag
*/
public $tenant;
public $credentials;
public $endpoint;
public $region;
public string $version = "2006-03-01";
public bool $pathStyle = false;
*/
protected string|null $createdBucketName;
protected AwsException|null $e;

/**
* Setup the Bucket Object
*
* @access public
* @param Stancl\Tenancy\Contracts\TenantWithDatabase $tenant Current Teanant
* @param Aws\Credentials\Credentials $credentials Aws Credentials Object
* @param string $endpoint Aws/Minio Endpoint
* @param bool $pathStyle Use Path Style Endpoint (set `true` for minio, default: false)
* @return void
*/
* @var \Aws\Exception\AwsException|null Exception Error Bag
*/
protected AwsException|null $e;

public function __construct(
TenantWithDatabase $tenant,
?Credentials $credentials = null,
?string $region = null,
?string $endpoint = null,
?bool $pathStyle = null
protected TenantWithDatabase $tenant
) {
$this->tenant = $tenant;
$this->credentials = $credentials ?? new Credentials(
$this->setupCredentials();
}

private function setupCredentials()
{
$this->credentials = new Credentials(
config('filesystems.disks.s3.key'),
config('filesystems.disks.s3.secret')
);
$this->region = $region ?? config('filesystems.disks.s3.region');
$this->endpoint = $endpoint ?? config('filesystems.disks.s3.endpoint');
$pathStyle = $pathStyle ?? config('filesystems.disks.s3.use_path_style_endpoint');
$this->pathStyle = $pathStyle ?? $this->pathStyle;
$this->region = config('filesystems.disks.s3.region');
$this->endpoint = config('filesystems.disks.s3.endpoint');
$this->pathStyle = config('filesystems.disks.s3.use_path_style_endpoint', false);
}

/**
Expand Down Expand Up @@ -158,6 +160,8 @@ public function deleteBucket(string $name, Credentials $credentials): self
]);
} catch (AwsException $e) {
$this->e = $e;
if (config('tenant-buckets.errors.throw', true))
throw $e;
Log::error($this->getErrorMessage());
}

Expand Down

0 comments on commit a895028

Please sign in to comment.