1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-18 05:04:05 +00:00
tenancy/src/Resolvers/PathTenantResolver.php
Abrar Ahmad 99dd862b20
[4.x] [WIP] Add phpstan to CI (#928)
* add phpstan

* resolve phpstan issue from CI

Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
2022-11-08 13:47:24 +01:00

43 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Resolvers;
use Illuminate\Routing\Route;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
class PathTenantResolver extends Contracts\CachedTenantResolver
{
public function resolveWithoutCache(mixed ...$args): Tenant
{
/** @var Route $route */
$route = $args[0];
/** @var string $id */
$id = $route->parameter(static::tenantParameterName());
if ($id) {
$route->forgetParameter(static::tenantParameterName());
if ($tenant = tenancy()->find($id)) {
return $tenant;
}
}
throw new TenantCouldNotBeIdentifiedByPathException($id);
}
public function getArgsForTenant(Tenant $tenant): array
{
return [
[$tenant->getTenantKey()],
];
}
public static function tenantParameterName(): string
{
return config('tenancy.identification.resolvers.' . static::class . '.tenant_parameter_name') ?? 'tenant';
}
}