1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-06 14:54:03 +00:00

Change terminology to pending

This commit is contained in:
j.stein 2022-02-09 22:55:03 +01:00
parent c8eadeb363
commit 500b2538fc
20 changed files with 387 additions and 385 deletions

View file

@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Events\PullingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantPulled;
use Stancl\Tenancy\Events\CreatingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantCreated;
/**
* @property $pending_since
*
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withPending(bool $withPending = true)
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyPending()
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withoutPending()
*/
trait HasPending
{
/**
* Boot the has pending trait for a model.
*
* @return void
*/
public static function bootHasPending()
{
static::addGlobalScope(new PendingScope());
}
/**
* Initialize the has pending trait for an instance.
*
* @return void
*/
public function initializeHasPending()
{
$this->casts['pending_since'] = 'timestamp';
}
/**
* Determine if the model instance is in a pending state.
*
* @return bool
*/
public function pending()
{
return !is_null($this->pending_since);
}
public static function createPending($attributes = []): void
{
$tenant = static::create($attributes);
event(new CreatingPendingTenant($tenant));
// We add the pending value only after the model has then been created.
// this ensures the model is not marked as pending until the migrations, seeders, etc. are done
$tenant->update([
'pending_since' => now()->timestamp
]);
event(new PendingTenantCreated($tenant));
}
public static function pullPendingTenant(bool $firstOrCreate = false): ?Tenant
{
if (!static::onlyPending()->exists()) {
if (!$firstOrCreate) {
return null;
}
static::createPending();
}
// At this point we can guarantee a pending tenant is free and can be called.
$tenant = static::onlyPending()->first();
event(new PullingPendingTenant($tenant));
$tenant->update([
'pending_since' => null
]);
event(new PendingTenantPulled($tenant));
return $tenant;
}
}

View file

@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class ReadiedScope implements Scope
class PendingScope implements Scope
{
/**
@ -16,7 +16,7 @@ class ReadiedScope implements Scope
*
* @var string[]
*/
protected $extensions = ['WithReadied', 'WithoutReadied', 'OnlyReadied'];
protected $extensions = ['WithPending', 'WithoutPending', 'OnlyPending'];
/**
* Apply the scope to a given Eloquent query builder.
@ -27,8 +27,8 @@ class ReadiedScope implements Scope
*/
public function apply(Builder $builder, Model $model)
{
$builder->when(!config('tenancy.readied.include_in_queries'), function (Builder $builder){
$builder->whereNull('data->readied');
$builder->when(!config('tenancy.pending.include_in_queries'), function (Builder $builder){
$builder->whereNull('data->pending_since');
});
}
@ -45,16 +45,16 @@ class ReadiedScope implements Scope
}
}
/**
* Add the with-readied extension to the builder.
* Add the with-pending extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addWithReadied(Builder $builder)
protected function addWithPending(Builder $builder)
{
$builder->macro('withReadied', function (Builder $builder, $withReadied = true) {
if (! $withReadied) {
return $builder->withoutReadied();
$builder->macro('withPending', function (Builder $builder, $withPending = true) {
if (! $withPending) {
return $builder->withoutPending();
}
return $builder->withoutGlobalScope($this);
@ -62,32 +62,32 @@ class ReadiedScope implements Scope
}
/**
* Add the without-readied extension to the builder.
* Add the without-pending extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addWithoutReadied(Builder $builder)
protected function addWithoutPending(Builder $builder)
{
$builder->macro('withoutReadied', function (Builder $builder) {
$builder->macro('withoutPending', function (Builder $builder) {
$builder->withoutGlobalScope($this)->whereNull('data->readied');
$builder->withoutGlobalScope($this)->whereNull('data->pending_since');
return $builder;
});
}
/**
* Add the only-readied extension to the builder.
* Add the only-pending extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addOnlyReadied(Builder $builder)
protected function addOnlyPending(Builder $builder)
{
$builder->macro('onlyReadied', function (Builder $builder) {
$builder->macro('onlyPending', function (Builder $builder) {
$builder->withoutGlobalScope($this)->whereNotNull('data->readied');
$builder->withoutGlobalScope($this)->whereNotNull('data->pending_since');
return $builder;
});

View file

@ -1,90 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Events\PullingReadiedTenant;
use Stancl\Tenancy\Events\ReadiedTenantPulled;
use Stancl\Tenancy\Events\ReadyingTenant;
use Stancl\Tenancy\Events\TenantReadied;
/**
* @property $readied
*
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withReadied(bool $withReadied = true)
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyReadied()
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withoutReadied()
*/
trait WithReadied
{
/**
* Boot the readied trait for a model.
*
* @return void
*/
public static function bootWithReadied()
{
static::addGlobalScope(new ReadiedScope());
}
/**
* Initialize the readied trait for an instance.
*
* @return void
*/
public function initializeWithReadied()
{
$this->casts['readied'] = 'timestamp';
}
/**
* Determine if the model instance is in a readied state.
*
* @return bool
*/
public function readied()
{
return !is_null($this->readied);
}
public static function createReadied($attributes = []): void
{
$tenant = static::create($attributes);
event(new ReadyingTenant($tenant));
// We add the readied value only after the model has then been created.
// this ensures the model is not marked as readied until the migrations, seeders, etc. are done
$tenant->update([
'readied' => now()->timestamp
]);
event(new TenantReadied($tenant));
}
public static function pullReadiedTenant(bool $firstOrCreate = false): ?Tenant
{
if (!static::onlyReadied()->exists()) {
if (!$firstOrCreate) {
return null;
}
static::createReadied();
}
// At this point we can guarantee a readied tenant is free and can be called
$tenant = static::onlyReadied()->first();
event(new PullingReadiedTenant($tenant));
$tenant->update([
'readied' => null
]);
event(new ReadiedTenantPulled($tenant));
return $tenant;
}
}