Skip to content

Commit

Permalink
feat: Add TenantProvider::retrieveByResourceKey method and implementa…
Browse files Browse the repository at this point in the history
…tion (#73)
  • Loading branch information
ollieread authored Nov 25, 2024
1 parent 66098b6 commit f576b1f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
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();
}
}

0 comments on commit f576b1f

Please sign in to comment.