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

Allows tenants to be initalized with their IDs

This commit is contained in:
Jørgen Solli 2020-08-04 18:02:24 +02:00
parent 99dec30512
commit e9c9c08b8d
2 changed files with 45 additions and 2 deletions

View file

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Exceptions;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;
use Facade\IgnitionContracts\Solution;
use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException;
class TenantCountNotBeIdentifiedById extends TenantCouldNotBeIdentifiedException implements ProvidesSolution
{
public function __construct($tenant_id)
{
parent::__construct("Tenant could not be identified with tenant_id: $tenant_id");
}
public function getSolution(): Solution
{
return BaseSolution::create('Tenant could not be identified with that ID')
->setSolutionDescription('Are you sure the ID is correct and the tenant exists?')
->setDocumentationLinks([
'Initializing Tenants' => 'https://tenancyforlaravel.com/docs/v3/tenants',
]);
}
}

View file

@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Macroable;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Exceptions\TenantCountNotBeIdentifiedById;
class Tenancy
{
@ -23,6 +23,11 @@ class Tenancy
/** @var bool */
public $initialized = false;
/**
* Initializes the tenant
* @param Tenant|int|string $tenant
* @return void
*/
public function initialize(Tenant $tenant): void
{
if ($this->initialized && $this->tenant->getTenantKey() === $tenant->getTenantKey()) {
@ -33,7 +38,18 @@ class Tenancy
$this->end();
}
if (is_object($tenant)) {
$this->tenant = $tenant;
} else {
$tenantId = $tenant;
$tenant = $this->find($tenantId);
if (!$tenant) {
throw new TenantCountNotBeIdentifiedById($tenantId);
}
$this->tenant = $tenant;
}
event(new Events\InitializingTenancy($this));