refactor: standardize design

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-02-25 15:33:59 -05:00
parent 149c342d34
commit 7e743542d0
6 changed files with 56 additions and 77 deletions

View File

@@ -17,15 +17,13 @@ use KTXF\Resource\Range\Range;
use KTXF\Resource\Range\RangeType;
use KTXF\Resource\Sort\Sort;
use KTXF\Utile\UUID;
use KTXM\ProviderLocalChrono\Providers\Personal\Collection;
use KTXM\ProviderLocalChrono\Providers\Personal\Entity;
use KTXM\ProviderLocalChrono\Providers\Personal\CollectionResource;
use KTXM\ProviderLocalChrono\Providers\Personal\EntityResource;
class Store {
protected string $_CollectionTable = 'provider_local_chrono_collection';
protected string $_CollectionClass = 'KTXM\ProviderLocalChrono\Providers\Personal\Collection';
protected string $_EntityTable = 'provider_local_chrono_entity';
protected string $_EntityClass = 'KTXM\ProviderLocalChrono\Providers\Personal\Entity';
protected string $_ChronicleTable = 'provider_local_chrono_chronicle';
protected array $_CollectionFilterAttributeMap = [
@@ -145,7 +143,7 @@ class Store {
$cursor = $this->_store->selectCollection($this->_CollectionTable)->find($query, $options);
$list = [];
foreach ($cursor as $entry) {
$entry = (new Collection())->fromStore($entry);
$entry = $this->collectionFresh()->fromStore($entry);
$identifier = $entry->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entry;
@@ -180,9 +178,9 @@ class Store {
* @param string $userId user identifier
* @param string $identifier collection identifier
*
* @return Collection
* @return CollectionResource|null
*/
public function collectionFetch(string $tenantId, string $userId, string $identifier): ?Collection {
public function collectionFetch(string $tenantId, string $userId, string $identifier): ?CollectionResource {
$cursor = $this->_store->selectCollection($this->_CollectionTable)->findOne([
'tid' => $tenantId,
'uid' => $userId,
@@ -191,7 +189,7 @@ class Store {
if ($cursor === null) {
return null;
}
$entry = (new Collection())->fromStore($cursor);
$entry = $this->collectionFresh()->fromStore($cursor);
return $entry;
}
@@ -200,10 +198,10 @@ class Store {
*
* @since Release 1.0.0
*
* @return Collection
* @return CollectionResource
*/
public function collectionFresh(): Collection {
return new $this->_CollectionClass;
public function collectionFresh(): CollectionResource {
return new CollectionResource();
}
/**
@@ -212,11 +210,11 @@ class Store {
* @since Release 1.0.0
*
* @param string $userId user identifier
* @param Collection $entity
* @param CollectionResource $entity
*
* @return Collection
* @return CollectionResource
*/
public function collectionCreate(string $tenantId, string $userId, Collection $entity): Collection {
public function collectionCreate(string $tenantId, string $userId, CollectionResource $entity): CollectionResource {
// convert entity to store format
$data = $entity->toStore();
// prepare data for creation
@@ -229,8 +227,7 @@ class Store {
// create entry
$result = $this->_store->selectCollection($this->_CollectionTable)->insertOne($data);
if ($result->getInsertedCount() === 1) {
$entity = new Collection();
$entity->fromStore($data);
$entity = $this->collectionFresh()->fromStore($data);
}
return $entity;
}
@@ -241,11 +238,11 @@ class Store {
* @since Release 1.0.0
*
* @param string $userId user identifier
* @param Collection $entity
* @param CollectionResource $entity
*
* @return Collection
* @return CollectionResource
*/
public function collectionModify(string $tenantId, string $userId, Collection $entity): Collection {
public function collectionModify(string $tenantId, string $userId, CollectionResource $entity): CollectionResource {
// convert entity to store format
$data = $entity->toStore();
// prepare data for modification
@@ -268,11 +265,11 @@ class Store {
*
* @since Release 1.0.0
*
* @param Collection $entity
* @param CollectionResource $entity
*
* @return Collection
* @return CollectionResource
*/
public function collectionDestroy(string $tenantId, string $userId, Collection $entity): Collection {
public function collectionDestroy(string $tenantId, string $userId, CollectionResource $entity): CollectionResource {
$identifier = $entity->identifier();
if ($identifier === null) {
return $entity;
@@ -351,7 +348,7 @@ class Store {
$cursor = $this->_store->selectCollection($this->_EntityTable)->find($query, $findOptions);
$list = [];
foreach ($cursor as $entry) {
$entity = (new Entity())->fromStore($entry);
$entity = $this->entityFresh()->fromStore($entry);
$identifier = $entity->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entity;
@@ -405,7 +402,7 @@ class Store {
* @param string $collection collection identifier
* @param string ...$identifiers entity identifiers (eid UUID strings)
*
* @return array<Entity>
* @return array<EntityResource>
*/
public function entityFetch(string $tenantId, string $userId, string $collectionId, string ...$identifiers): array {
// Query for entities using eid field
@@ -418,7 +415,7 @@ class Store {
$list = [];
foreach ($cursor as $entry) {
$entity = (new Entity())->fromStore($entry);
$entity = $this->entityFresh()->fromStore($entry);
$identifier = $entity->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entity;
@@ -433,21 +430,12 @@ class Store {
*
* @since Release 1.0.0
*
* @return Entity
* @return EntityResource
*/
public function entityFresh(): Entity {
return new Entity();
public function entityFresh(): EntityResource {
return new EntityResource();
}
/**
* create a entity entry in the data store
*
* @since Release 1.0.0
*
* @param Entity $entity entity to create
*
* @return Entity
*/
/**
* create a entity entry in the data store
*
@@ -455,11 +443,11 @@ class Store {
*
* @param string $userId user identifier
* @param string $collection collection identifier
* @param Entity $entity entity to create
* @param EntityResource $entity entity to create
*
* @return Entity
* @return EntityResource
*/
public function entityCreate(string $tenantId, string $userId, string $collectionId, Entity $entity): Entity {
public function entityCreate(string $tenantId, string $userId, string $collectionId, EntityResource $entity): EntityResource {
// convert entity to store format
$data = $entity->toStore();
// assign identifiers and timestamps
@@ -476,7 +464,7 @@ class Store {
if ($result->getInsertedCount() === 1) {
$eid = $data['eid'];
$entity->fromStore($data);
$entity = $this->entityFresh()->fromStore($data);
// Chronicle the creation (operation 1)
$this->chronicleDocument($tenantId, $collectionId, $eid, 1);
}
@@ -492,11 +480,11 @@ class Store {
* @param string $userId user identifier
* @param string $collection collection identifier
* @param string $identifier entity identifier
* @param Entity $entity entity to modify
* @param EntityResource $entity entity to modify
*
* @return Entity
* @return EntityResource
*/
public function entityModify(string $tenantId, string $userId, string $collectionId, string $identifier, Entity $entity): Entity {
public function entityModify(string $tenantId, string $userId, string $collectionId, string $identifier, EntityResource $entity): EntityResource {
// convert entity to store format
$data = $entity->toStore();
$data['modifiedOn'] = date('c');
@@ -524,11 +512,11 @@ class Store {
*
* @param string $userId user identifier
* @param string $collection collection identifier
* @param Entity $entity entity to delete
* @param EntityResource $entity entity to delete
*
* @return Entity
* @return EntityResource
*/
public function entityDestroy(string $tenantId, string $userId, string $collectionId, Entity $entity): Entity {
public function entityDestroy(string $tenantId, string $userId, string $collectionId, EntityResource $entity): EntityResource {
$identifier = $entity->identifier();
if ($identifier === null) {
return $entity;