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

Rewrite old tests

This commit is contained in:
Samuel Štancl 2020-05-12 23:22:40 +02:00
parent 64383b4c56
commit 89936187ce
71 changed files with 698 additions and 3203 deletions

View file

@ -33,6 +33,10 @@ class Tenancy
public function end(): void
{
if (! $this->initialized) {
return;
}
$this->initialized = false;
event(new Events\TenancyEnded($this));
@ -68,4 +72,38 @@ class Tenancy
{
return $this->model()->find($id);
}
/**
* Run a callback for multiple tenants.
* More performant than running $tenant->run() one by one.
*
* @param Tenant[]|\Illuminate\Support\Collection|string[]|null $tenants
* @param callable $callback
* @return void
*/
public function runForMultiple($tenants, callable $callback)
{
// Wrap string in array
$tenants = is_string($tenants) ? [$tenants] : $tenants;
// Use all tenants if $tenants is falsy
$tenants = $tenants ?: $this->model()->cursor();
$originalTenant = $this->tenant;
foreach ($tenants as $tenant) {
if (is_string($tenant)) {
$tenant = $this->find($tenant);
}
$this->initialize($tenant);
$callback($tenant);
}
if ($originalTenant) {
$this->initialize($originalTenant);
} else {
$this->end();
}
}
}