1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 09:44:03 +00:00

misc improvements - stronger types, exception refactor

This commit is contained in:
Samuel Štancl 2022-08-26 21:35:17 +02:00
parent ddc7cf49c3
commit 55d0a9ab87
34 changed files with 179 additions and 209 deletions

View file

@ -6,26 +6,20 @@ namespace Stancl\Tenancy\Database\Concerns;
trait HasInternalKeys
{
/**
* Get the internal prefix.
*/
/** Get the internal prefix. */
public static function internalPrefix(): string
{
return 'tenancy_';
}
/**
* Get an internal key.
*/
public function getInternal(string $key)
/** Get an internal key. */
public function getInternal(string $key): mixed
{
return $this->getAttribute(static::internalPrefix() . $key);
}
/**
* Set internal key.
*/
public function setInternal(string $key, $value)
/** Set internal key. */
public function setInternal(string $key, mixed $value): static
{
$this->setAttribute(static::internalPrefix() . $key, $value);

View file

@ -4,15 +4,17 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Closure;
use Stancl\Tenancy\Contracts\Tenant;
trait TenantRun
{
/**
* Run a callback in this tenant's context.
* Atomic, safely reverts to previous context.
*
* This method is atomic and safely reverts to the previous context.
*/
public function run(callable $callback)
public function run(Closure $callback): mixed
{
/** @var Tenant $this */
$originalTenant = tenant();

View file

@ -14,6 +14,8 @@ use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
use Stancl\Tenancy\Exceptions\TenantDatabaseUserAlreadyExistsException;
// todo move to Database namespace
/**
* @internal Class is subject to breaking changes in minor and patch versions.
*/

View file

@ -35,10 +35,8 @@ class ImpersonationToken extends Model
'created_at',
];
public static function boot()
public static function booted()
{
parent::boot();
static::creating(function ($model) {
$model->created_at = $model->created_at ?? $model->freshTimestamp();
$model->token = $model->token ?? Str::random(128);

View file

@ -39,7 +39,7 @@ class Tenant extends Model implements Contracts\Tenant
return 'id';
}
public function getTenantKey()
public function getTenantKey(): int|string
{
return $this->getAttribute($this->getTenantKeyName());
}

View file

@ -9,10 +9,8 @@ use Stancl\Tenancy\Contracts\Syncable;
class TenantPivot extends Pivot
{
public static function boot()
public static function booted()
{
parent::boot();
static::saved(function (self $pivot) {
$parent = $pivot->pivotParent;

View file

@ -10,7 +10,7 @@ use Illuminate\Database\Eloquent\Scope;
class ParentModelScope implements Scope
{
public function apply(Builder $builder, Model $model)
public function apply(Builder $builder, Model $model): void
{
if (! tenancy()->initialized) {
return;

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database;
use Closure;
use Illuminate\Database\Eloquent\Collection;
use Stancl\Tenancy\Contracts\Tenant;
@ -16,7 +17,7 @@ use Stancl\Tenancy\Contracts\Tenant;
*/
class TenantCollection extends Collection
{
public function runForEach(callable $callable): self
public function runForEach(Closure $callable): self
{
tenancy()->runForMultiple($this->items, $callable);