75 lines
2.2 KiB
PHP
75 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace KTXC\Stores;
|
|
|
|
use KTXC\Db\DataStore;
|
|
use KTXC\Models\Tenant\TenantObject;
|
|
|
|
class TenantStore
|
|
{
|
|
|
|
protected const COLLECTION_NAME = 'tenants';
|
|
|
|
public function __construct(
|
|
protected readonly DataStore $dataStore
|
|
) { }
|
|
|
|
public function list(): array
|
|
{
|
|
$cursor = $this->dataStore->selectCollection(self::COLLECTION_NAME)->find();
|
|
$list = [];
|
|
foreach ($cursor as $entry) {
|
|
$entry = (new TenantObject())->jsonDeserialize((array)$entry);
|
|
$list[$entry->getId()] = $entry;
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
public function fetch(string $identifier): ?TenantObject
|
|
{
|
|
$entry = $this->dataStore->selectCollection(self::COLLECTION_NAME)->findOne(['identifier' => $identifier]);
|
|
if (!$entry) { return null; }
|
|
return (new TenantObject())->jsonDeserialize((array)$entry);
|
|
}
|
|
|
|
public function fetchByDomain(string $domain): ?TenantObject
|
|
{
|
|
$entry = $this->dataStore->selectCollection(self::COLLECTION_NAME)->findOne(['domains' => $domain]);
|
|
if (!$entry) { return null; }
|
|
$entity = new TenantObject();
|
|
$entity->jsonDeserialize((array)$entry);
|
|
return $entity;
|
|
}
|
|
|
|
public function deposit(TenantObject $entry): ?TenantObject
|
|
{
|
|
if ($entry->getId()) {
|
|
return $this->update($entry);
|
|
} else {
|
|
return $this->create($entry);
|
|
}
|
|
}
|
|
|
|
private function create(TenantObject $entry): ?TenantObject
|
|
{
|
|
$result = $this->dataStore->selectCollection(self::COLLECTION_NAME)->insertOne($entry->jsonSerialize());
|
|
$entry->setId((string)$result->getInsertedId());
|
|
return $entry;
|
|
}
|
|
|
|
private function update(TenantObject $entry): ?TenantObject
|
|
{
|
|
$id = $entry->getId();
|
|
if (!$id) { return null; }
|
|
$this->dataStore->selectCollection(self::COLLECTION_NAME)->updateOne(['_id' => $id], ['$set' => $entry->jsonSerialize()]);
|
|
return $entry;
|
|
}
|
|
|
|
public function destroy(TenantObject $entry): void
|
|
{
|
|
$id = $entry->getId();
|
|
if (!$id) { return; }
|
|
$this->dataStore->selectCollection(self::COLLECTION_NAME)->deleteOne([ '_id' => $id]);
|
|
}
|
|
|
|
} |