chore: standardize chrono provider

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-02-17 03:11:54 -05:00
parent f3d2e2690b
commit 5c6d508cac
7 changed files with 451 additions and 529 deletions

View File

@@ -146,7 +146,10 @@ class Store {
$list = [];
foreach ($cursor as $entry) {
$entry = (new Collection())->fromStore($entry);
$list[$entry->id()] = $entry;
$identifier = $entry->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entry;
}
}
return $list;
}
@@ -220,6 +223,7 @@ class Store {
$data['tid'] = $tenantId;
$data['uid'] = $userId;
$data['cid'] = UUID::v4();
$data['signature'] = isset($data['signature']) && is_numeric($data['signature']) ? (int)$data['signature'] : 0;
$data['createdOn'] = date('c');
$data['modifiedOn'] = $data['createdOn'];
// create entry
@@ -245,7 +249,10 @@ class Store {
// convert entity to store format
$data = $entity->toStore();
// prepare data for modification
$cid = $entity->id();
$cid = $entity->identifier();
if ($cid === null) {
throw new \InvalidArgumentException('Collection identifier is required for modification');
}
$data['modifiedOn'] = date('c');
unset($data['_id'], $data['tid'], $data['uid'], $data['cid']);
// modify entry
@@ -266,7 +273,11 @@ class Store {
* @return Collection
*/
public function collectionDestroy(string $tenantId, string $userId, Collection $entity): Collection {
return $this->collectionDestroyById($tenantId, $userId, $entity->id()) ? $entity : $entity;
$identifier = $entity->identifier();
if ($identifier === null) {
return $entity;
}
return $this->collectionDestroyById($tenantId, $userId, (string) $identifier) ? $entity : $entity;
}
/**
@@ -341,7 +352,10 @@ class Store {
$list = [];
foreach ($cursor as $entry) {
$entity = (new Entity())->fromStore($entry);
$list[$entity->id()] = $entity;
$identifier = $entity->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entity;
}
}
return $list;
}
@@ -405,7 +419,10 @@ class Store {
$list = [];
foreach ($cursor as $entry) {
$entity = (new Entity())->fromStore($entry);
$list[$entity->id()] = $entity;
$identifier = $entity->identifier();
if ($identifier !== null) {
$list[(string) $identifier] = $entity;
}
}
return $list;
@@ -459,7 +476,7 @@ class Store {
if ($result->getInsertedCount() === 1) {
$eid = $data['eid'];
$entity->fromStore(['eid' => $eid, 'tid' => $tenantId, 'uid' => $userId, 'cid' => $collectionId]);
$entity->fromStore($data);
// Chronicle the creation (operation 1)
$this->chronicleDocument($tenantId, $collectionId, $eid, 1);
}
@@ -512,18 +529,21 @@ class Store {
* @return Entity
*/
public function entityDestroy(string $tenantId, string $userId, string $collectionId, Entity $entity): Entity {
$identifier = $entity->id();
$identifier = $entity->identifier();
if ($identifier === null) {
return $entity;
}
$result = $this->_store->selectCollection($this->_EntityTable)->deleteOne([
'tid' => $tenantId,
'uid' => $userId,
'cid' => $collectionId,
'eid' => $identifier
'eid' => (string) $identifier
]);
if ($result->getDeletedCount() === 1) {
// Chronicle the deletion (operation 3)
$this->chronicleDocument($tenantId, $collectionId, $identifier, 3);
$this->chronicleDocument($tenantId, $collectionId, (string) $identifier, 3);
}
return $entity;
@@ -570,12 +590,19 @@ class Store {
private function chronicleDocument(string $tid, string $cid, string $eid, int $operation): void {
// retrieve current token from collection
$collection = $this->_store->selectCollection($this->_CollectionTable)->findOne([
'tid' => $tid,
'cid' => $cid
], [
'projection' => ['signature' => 1, '_id' => 0]
]);
$signature = $collection['signature'] ?? 0;
$signatureRaw = $collection['signature'] ?? 0;
if (is_numeric($signatureRaw)) {
$signature = (int)$signatureRaw;
} else {
$decoded = is_string($signatureRaw) ? base64_decode($signatureRaw, true) : false;
$signature = (is_string($decoded) && is_numeric($decoded)) ? (int)$decoded : 0;
}
// document operation in chronicle
$this->_store->selectCollection($this->_ChronicleTable)->insertOne([
@@ -587,10 +614,10 @@ class Store {
'mutatedOn' => time(),
]);
// increment token atomically
// update signature as normalized numeric value
$this->_store->selectCollection($this->_CollectionTable)->updateOne(
['cid' => $cid],
['$inc' => ['signature' => 1]]
['tid' => $tid, 'cid' => $cid],
['$set' => ['signature' => $signature + 1]]
);
}