Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add TenantProvider::retrieveByResourceKey method and implementation #73

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Contracts/TenantProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,25 @@ public function retrieveByIdentifier(string $identifier): ?Tenant;
* @phpstan-return TenantClass|null
*/
public function retrieveByKey(int|string $key): ?Tenant;

/**
* Retrieve a tenant by its resource key
*
* Gets an instance of the tenant implementation the provider represents,
* using a resource key.
* The tenant class must implement the {@see \Sprout\Contracts\TenantHasResources}
* interface for this method to work.
*
* @param string $resourceKey
*
* @return (\Sprout\Contracts\Tenant&\Sprout\Contracts\TenantHasResources)|null
*
* @throws \Sprout\Exceptions\MisconfigurationException
*
* @phpstan-return (TenantClass&\Sprout\Contracts\TenantHasResources)|null
*
* @see \Sprout\Contracts\Tenant::getTenantKeyName()
* @see \Sprout\Contracts\Tenant::getTenantKey()
*/
public function retrieveByResourceKey(string $resourceKey): (Tenant&TenantHasResources)|null;
}
45 changes: 42 additions & 3 deletions src/Providers/DatabaseTenantProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

use Illuminate\Database\ConnectionInterface;
use Sprout\Contracts\Tenant;
use Sprout\Contracts\TenantProvider;
use Sprout\Contracts\TenantHasResources;
use Sprout\Exceptions\MisconfigurationException;
use Sprout\Support\BaseTenantProvider;
use Sprout\Support\GenericTenant;

Expand Down Expand Up @@ -131,7 +132,7 @@ private function makeEntity(array|object $attributes): object
* @see \Sprout\Contracts\Tenant::getTenantIdentifier()
* @see \Sprout\Contracts\Tenant::getTenantIdentifierName()
*
* @phpstan-return Tenant|null
* @phpstan-return EntityClass|null
*/
public function retrieveByIdentifier(string $identifier): ?Tenant
{
Expand Down Expand Up @@ -160,7 +161,7 @@ public function retrieveByIdentifier(string $identifier): ?Tenant
* @see \Sprout\Contracts\Tenant::getTenantKey()
* @see \Sprout\Contracts\Tenant::getTenantKeyName()
*
* @phpstan-return Tenant|null
* @phpstan-return EntityClass|null
*/
public function retrieveByKey(int|string $key): ?Tenant
{
Expand All @@ -175,4 +176,42 @@ public function retrieveByKey(int|string $key): ?Tenant

return null;
}

/**
* Retrieve a tenant by its resource key
*
* Gets an instance of the tenant implementation the provider represents,
* using a resource key.
* The tenant class must implement the {@see \Sprout\Contracts\TenantHasResources}
* interface for this method to work.
*
* @param string $resourceKey
*
* @return (\Sprout\Contracts\Tenant&\Sprout\Contracts\TenantHasResources)|null
*
* @throws \Sprout\Exceptions\MisconfigurationException
*
* @phpstan-return (EntityClass&\Sprout\Contracts\TenantHasResources)|null
*
* @see \Sprout\Contracts\TenantHasResources::getTenantResourceKeyName()
* @see \Sprout\Contracts\TenantHasResources::getTenantResourceKey()
*/
public function retrieveByResourceKey(string $resourceKey): (Tenant&TenantHasResources)|null
{
$entity = $this->getEntity();

if (! ($entity instanceof TenantHasResources)) {
throw MisconfigurationException::misconfigured('tenant', $entity::class, 'resources');
}

$attributes = $this->connection->table($this->table)
->where($entity->getTenantResourceKeyName(), '=', $resourceKey)
->first();

if ($attributes !== null) {
return $this->makeEntity($attributes); // @phpstan-ignore-line
}

return null;
}
}
35 changes: 34 additions & 1 deletion src/Providers/EloquentTenantProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

use Illuminate\Database\Eloquent\Model;
use Sprout\Contracts\Tenant;
use Sprout\Contracts\TenantProvider;
use Sprout\Contracts\TenantHasResources;
use Sprout\Exceptions\MisconfigurationException;
use Sprout\Support\BaseTenantProvider;

/**
Expand Down Expand Up @@ -133,4 +134,36 @@ public function retrieveByKey(int|string $key): ?Tenant
->where($model->getTenantKeyName(), $key)
->first();
}

/**
* Retrieve a tenant by its resource key
*
* Gets an instance of the tenant implementation the provider represents,
* using a resource key.
* The tenant class must implement the {@see \Sprout\Contracts\TenantHasResources}
* interface for this method to work.
*
* @param string $resourceKey
*
* @return (\Sprout\Contracts\Tenant&\Sprout\Contracts\TenantHasResources)|null
*
* @throws \Sprout\Exceptions\MisconfigurationException
*
* @phpstan-return (TenantModel&\Sprout\Contracts\TenantHasResources)|null
*
* @see \Sprout\Contracts\TenantHasResources::getTenantResourceKeyName()
* @see \Sprout\Contracts\TenantHasResources::getTenantResourceKey()
*/
public function retrieveByResourceKey(string $resourceKey): (Tenant&TenantHasResources)|null
{
$model = $this->getModel();

if (! ($model instanceof TenantHasResources)) {
throw MisconfigurationException::misconfigured('tenant', $model::class, 'resources');
}

return $model->newModelQuery()
->where($model->getTenantResourceKeyName(), $resourceKey)
->first();
}
}
Loading