1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 01:44:04 +00:00
tenancy/src/Resolvers/RequestDataTenantResolver.php
lukinovec f308e2f84d
[4.x] Resolve testing todos (#1361)
* Test encrypted cookie identification

* Add Fortify bootstrapper custom query param passing test

* Correct Fortify route bootstrapper test (todo  refactor, convoluted)

* Clarify Fortify bootstrapper test

* Fix encrypted cookie identification test

* Move encrypted cookie assertion to "cookie identification works"

* Cover configured tenant model columns in cached resolver tests

* Refactor testing resolver with default vs custom tenant model name config

* Delete resolved todo

* Make code more concise

* Keep initial formatting (minimize diff noise)

* Make dataset/helper method parameter clearer

* Clarify fortify test

* Clarify assertions, improve comments

* Delete excessive comments, make existing comments consistent and clearer

* Make cached resolver test file clearer, update outdated comments

* Use the tenant model column term consistently

* FIx inconsistencies

* Provide more info in comment

* make comment more clear

* static property reset

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
2025-08-03 23:21:03 +02:00

72 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Resolvers;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestDataException;
class RequestDataTenantResolver extends Contracts\CachedTenantResolver
{
public static bool $shouldCache = false;
public static int $cacheTTL = 3600; // seconds
public static string|null $cacheStore = null; // default
public function resolveWithoutCache(mixed ...$args): Tenant
{
$payload = (string) $args[0];
$column = static::tenantModelColumn();
if ($payload && $tenant = tenancy()->find($payload, $column, withRelations: true)) {
return $tenant;
}
throw new TenantCouldNotBeIdentifiedByRequestDataException($payload);
}
public function getPossibleCacheKeys(Tenant&Model $tenant): array
{
return [
$this->formatCacheKey(static::payloadValue($tenant)),
];
}
public static function payloadValue(Tenant $tenant): string
{
return $tenant->getAttribute(static::tenantModelColumn());
}
public static function tenantModelColumn(): string
{
return config('tenancy.identification.resolvers.' . static::class . '.tenant_model_column') ?? tenancy()->model()->getTenantKeyName();
}
/**
* Returns the name of the header used for identification, or null if header identification is disabled.
*/
public static function headerName(): string|null
{
return config('tenancy.identification.resolvers.' . static::class . '.header');
}
/**
* Returns the name of the query parameter used for identification, or null if query parameter identification is disabled.
*/
public static function queryParameterName(): string|null
{
return config('tenancy.identification.resolvers.' . static::class . '.query_parameter');
}
/**
* Returns the name of the cookie used for identification, or null if cookie identification is disabled.
*/
public static function cookieName(): string|null
{
return config('tenancy.identification.resolvers.' . static::class . '.cookie');
}
}