1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 18:34:04 +00:00

Add readied events

This commit is contained in:
j.stein 2022-01-18 18:19:40 +01:00
parent f487f92f0d
commit 30f0a2b134
7 changed files with 80 additions and 1 deletions

View file

@ -49,6 +49,12 @@ class TenancyServiceProvider extends ServiceProvider
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
],
// Readied events
Events\ReadyingTenant::class => [],
Events\TenantReadied::class => [],
Events\PullingReadiedTenant::class => [],
Events\ReadiedTenantPulled::class => [],
// Domain events
Events\CreatingDomain::class => [],
Events\DomainCreated::class => [],

View file

@ -4,8 +4,11 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Carbon\Carbon;
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 null|Carbon $readied
@ -51,11 +54,15 @@ trait WithReadied
{
$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
@ -70,10 +77,14 @@ trait WithReadied
// 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;
}
}

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class PullingReadiedTenant extends Contracts\TenantEvent
{
}

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class ReadiedTenantPulled extends Contracts\TenantEvent
{
}

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class ReadyingTenant extends Contracts\TenantEvent
{
}

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class TenantReadied extends Contracts\TenantEvent
{
}

View file

@ -5,8 +5,13 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Commands\ClearReadiedTenants;
use Stancl\Tenancy\Commands\CreateReadiedTenants;
use Stancl\Tenancy\Events\PullingReadiedTenant;
use Stancl\Tenancy\Events\ReadiedTenantPulled;
use Stancl\Tenancy\Events\ReadyingTenant;
use Stancl\Tenancy\Events\TenantReadied;
use Stancl\Tenancy\Tests\Etc\Tenant;
class ReadiedTenantsTest extends TestCase
@ -127,4 +132,25 @@ class ReadiedTenantsTest extends TestCase
$this->assertCount(1, Tenant::all());
Tenant::all();
}
/** @test */
public function readied_events_are_triggerred()
{
Event::fake([
ReadyingTenant::class,
TenantReadied::class,
PullingReadiedTenant::class,
ReadiedTenantPulled::class,
]);
Tenant::createReadied();
Event::assertDispatched(ReadyingTenant::class);
Event::assertDispatched(TenantReadied::class);
Tenant::pullReadiedTenant();
Event::assertDispatched(PullingReadiedTenant::class);
Event::assertDispatched(ReadiedTenantPulled::class);
}
}