1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 16:14:02 +00:00

ID generation

This commit is contained in:
Samuel Štancl 2019-09-11 19:25:30 +02:00
parent 958651099a
commit 445b2650cf
3 changed files with 24 additions and 13 deletions

View file

@ -9,9 +9,9 @@ interface UniqueIdentifierGenerator
/** /**
* Generate a unique identifier. * Generate a unique identifier.
* *
* @param string $domain * @param string[] $domains
* @param array $data * @param array $data
* @return string * @return string
*/ */
public static function handle(string $domain, array $data = []): string; public static function handle(array $domains, array $data = []): string;
} }

View file

@ -6,6 +6,7 @@ namespace Stancl\Tenancy;
use ArrayAccess; use ArrayAccess;
use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
/** /**
* @internal Class is subject to breaking changes in minor and patch versions. * @internal Class is subject to breaking changes in minor and patch versions.
@ -28,11 +29,14 @@ class Tenant implements ArrayAccess
*/ */
public $domains = []; public $domains = [];
/** @var StorageDriver */
protected $storage;
/** @var TenantManager */ /** @var TenantManager */
protected $manager; protected $manager;
/** @var StorageDriver */ /** @var UniqueIdentifierGenerator */
protected $storage; protected $idGenerator;
/** /**
* Does this tenant exist in the storage. * Does this tenant exist in the storage.
@ -41,17 +45,16 @@ class Tenant implements ArrayAccess
*/ */
protected $persisted = false; protected $persisted = false;
public function __construct(TenantManager $tenantManager, StorageDriver $storage) public function __construct(StorageDriver $storage, TenantManager $tenantManager, UniqueIdentifierGenerator $idGenerator)
{ {
$this->manager = $tenantManager;
$this->storage = $storage; $this->storage = $storage;
$this->manager = $tenantManager;
$this->idGenerator = $idGenerator;
} }
public static function new(): self public static function new(): self
{ {
return app(static::class)->withData([ return app(static::class);
'id' => static::generateId(), // todo
]);
} }
public static function fromStorage(array $data): self public static function fromStorage(array $data): self
@ -66,7 +69,6 @@ class Tenant implements ArrayAccess
return $this; return $this;
} }
// todo move this to TenantFactory?
public function withDomains($domains): self public function withDomains($domains): self
{ {
$domains = (array) $domains; $domains = (array) $domains;
@ -83,8 +85,17 @@ class Tenant implements ArrayAccess
return $this; return $this;
} }
public function generateId()
{
$this->id = $this->idGenerator->handle($this->domains, $this->data);
}
public function save(): self public function save(): self
{ {
if (! $this->id) {
$this->generateId();
}
if ($this->persisted) { if ($this->persisted) {
$this->manager->createTenant($this); $this->manager->createTenant($this);
} else { } else {

View file

@ -4,12 +4,12 @@ declare(strict_types=1);
namespace Stancl\Tenancy; namespace Stancl\Tenancy;
use Stancl\Tenancy\Interfaces\UniqueIdentifierGenerator; use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
class UUIDGenerator implements UniqueIdentifierGenerator class UUIDGenerator implements UniqueIdentifierGenerator
{ {
public static function handle(string $domain, array $data = []): string public static function handle(array $domains, array $data = []): string
{ {
return (string) \Webpatser\Uuid\Uuid::generate(1, $domain); return (string) \Webpatser\Uuid\Uuid::generate(1, $domains[0] ?? '');
} }
} }