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

@@ -12,9 +12,6 @@ namespace KTXM\ProviderLocalChrono\Providers\Personal;
use KTXF\Chrono\Collection\CollectionContent; use KTXF\Chrono\Collection\CollectionContent;
use KTXF\Chrono\Collection\CollectionPropertiesMutableAbstract; use KTXF\Chrono\Collection\CollectionPropertiesMutableAbstract;
/**
* Personal Chrono Collection Properties Implementation
*/
class CollectionProperties extends CollectionPropertiesMutableAbstract { class CollectionProperties extends CollectionPropertiesMutableAbstract {
/** /**

View File

@@ -11,7 +11,7 @@ namespace KTXM\ProviderLocalChrono\Providers\Personal;
use KTXF\Chrono\Collection\CollectionMutableAbstract; use KTXF\Chrono\Collection\CollectionMutableAbstract;
class Collection extends CollectionMutableAbstract { class CollectionResource extends CollectionMutableAbstract {
private ?string $userId = null; private ?string $userId = null;
private ?string $collectionUuid = null; private ?string $collectionUuid = null;

View File

@@ -12,10 +12,7 @@ namespace KTXM\ProviderLocalChrono\Providers\Personal;
use KTXF\Chrono\Entity\EntityMutableAbstract; use KTXF\Chrono\Entity\EntityMutableAbstract;
use KTXF\Chrono\Event\EventObject; use KTXF\Chrono\Event\EventObject;
/** class EntityResource extends EntityMutableAbstract {
* Personal Chrono Entity Resource Implementation
*/
class Entity extends EntityMutableAbstract {
private ?string $tenantId = null; private ?string $tenantId = null;
private ?string $userId = null; private ?string $userId = null;

View File

@@ -156,8 +156,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
// Collection operations // Collection operations
public function collectionList(string|int|null $location, ?IFilter $filter = null, ?ISort $sort = null): array public function collectionList(string|int|null $location, ?IFilter $filter = null, ?ISort $sort = null): array {
{
$entries = $this->store->collectionList($this->serviceTenantId, $this->serviceUserId, $filter, $sort); $entries = $this->store->collectionList($this->serviceTenantId, $this->serviceUserId, $filter, $sort);
$this->serviceCollectionCache = $entries; $this->serviceCollectionCache = $entries;
return $entries ?? []; return $entries ?? [];
@@ -212,14 +211,14 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return null; return null;
} }
public function collectionFresh(): Collection { public function collectionFresh(): CollectionResource {
return new Collection(); return new CollectionResource();
} }
public function collectionCreate(string|int|null $location, CollectionMutableInterface $collection, array $options = []): CollectionBaseInterface { public function collectionCreate(string|int|null $location, CollectionMutableInterface $collection, array $options = []): CollectionBaseInterface {
// convert collection to a native type if needed // convert collection to a native type if needed
if (!($collection instanceof Collection)) { if (!($collection instanceof CollectionResource)) {
$nativeCollection = new Collection(); $nativeCollection = new CollectionResource();
$nativeCollection->jsonDeserialize($collection->jsonSerialize()); $nativeCollection->jsonDeserialize($collection->jsonSerialize());
} else { } else {
$nativeCollection = clone $collection; $nativeCollection = clone $collection;
@@ -244,16 +243,16 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
throw new InvalidParameterException("Invalid: Collection identifier '$id' does not exist or does not belong to user '{$this->serviceUserId}'"); throw new InvalidParameterException("Invalid: Collection identifier '$id' does not exist or does not belong to user '{$this->serviceUserId}'");
} }
// convert collection to a native type if needed // convert collection to a native type if needed
if (!($collection instanceof Collection)) { if (!($collection instanceof CollectionResource)) {
$nativeCollection = new Collection(); $nativeCollection = new CollectionResource();
$data = $collection->jsonSerialize(); $data = $collection->jsonSerialize();
$data[Collection::JSON_PROPERTY_IDENTIFIER] = $id; $data[CollectionResource::JSON_PROPERTY_IDENTIFIER] = $id;
$nativeCollection->jsonDeserialize($data); $nativeCollection->jsonDeserialize($data);
} else { } else {
$nativeCollection = clone $collection; $nativeCollection = clone $collection;
if ($nativeCollection->identifier() === null) { if ($nativeCollection->identifier() === null) {
$data = $nativeCollection->jsonSerialize(); $data = $nativeCollection->jsonSerialize();
$data[Collection::JSON_PROPERTY_IDENTIFIER] = $id; $data[CollectionResource::JSON_PROPERTY_IDENTIFIER] = $id;
$nativeCollection->jsonDeserialize($data); $nativeCollection->jsonDeserialize($data);
} }
} }
@@ -279,6 +278,8 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return false; return false;
} }
// Entity operations
public function entityList(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $options = null): array { public function entityList(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $options = null): array {
// validate id // validate id
if (!is_string($collection)) { if (!is_string($collection)) {
@@ -293,7 +294,6 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return $entries ?? []; return $entries ?? [];
} }
public function entityListFilter(): Filter { public function entityListFilter(): Filter {
return new Filter($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_FILTER] ?? []); return new Filter($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_FILTER] ?? []);
} }
@@ -360,11 +360,11 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return $entries ?? []; return $entries ?? [];
} }
public function entityFresh(): Entity { public function entityFresh(): EntityResource {
return new Entity(); return new EntityResource();
} }
public function entityCreate(string|int $collection, EntityMutableInterface $entity, array $options = []): Entity { public function entityCreate(string|int $collection, EntityMutableInterface $entity, array $options = []): EntityResource {
// validate collection identifier // validate collection identifier
if (!is_string($collection)) { if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid"); throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
@@ -374,7 +374,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'"); throw new InvalidParameterException("Invalid: Collection identifier '$collection' does not exist or does not belong to user '{$this->serviceUserId}'");
} }
// convert entity to a native type if needed // convert entity to a native type if needed
if (!($entity instanceof Entity)) { if (!($entity instanceof EntityResource)) {
$nativeEntity = $this->entityFresh(); $nativeEntity = $this->entityFresh();
$nativeEntity->jsonDeserialize($entity->jsonSerialize()); $nativeEntity->jsonDeserialize($entity->jsonSerialize());
} else { } else {
@@ -386,7 +386,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return $result; return $result;
} }
public function entityUpdate(string|int $collection, string|int $identifier, EntityMutableInterface $entity): Entity { public function entityUpdate(string|int $collection, string|int $identifier, EntityMutableInterface $entity): EntityResource {
// validate collection identifier // validate collection identifier
if (!is_string($collection)) { if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid"); throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");
@@ -406,7 +406,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
} }
// convert entity to a native type if needed // convert entity to a native type if needed
if (!($entity instanceof Entity)) { if (!($entity instanceof EntityResource)) {
$nativeEntity = $this->entityFresh(); $nativeEntity = $this->entityFresh();
$nativeEntity->jsonDeserialize($entity->jsonSerialize()); $nativeEntity->jsonDeserialize($entity->jsonSerialize());
} else { } else {
@@ -419,7 +419,7 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
return $result; return $result;
} }
public function entityDelete(string|int $collection, string|int $identifier): EntityMutableInterface { public function entityDelete(string|int $collection, string|int $identifier): EntityResource {
// validate collection identifier // validate collection identifier
if (!is_string($collection)) { if (!is_string($collection)) {
throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid"); throw new InvalidParameterException("Invalid: Collection identifier '$collection' is not valid");

View File

@@ -14,9 +14,6 @@ use KTXF\Chrono\Provider\ProviderBaseInterface;
use KTXF\Chrono\Service\ServiceBaseInterface; use KTXF\Chrono\Service\ServiceBaseInterface;
use KTXM\ProviderLocalChrono\Providers\Personal\PersonalService; use KTXM\ProviderLocalChrono\Providers\Personal\PersonalService;
/**
* Local Storage Provider for Chrono
*/
class Provider implements ProviderBaseInterface class Provider implements ProviderBaseInterface
{ {
@@ -55,7 +52,7 @@ class Provider implements ProviderBaseInterface
public function type(): string public function type(): string
{ {
return self::TYPE_MAIL; return self::TYPE_CHRONO;
} }
public function identifier(): string public function identifier(): string

View File

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