feat: implement entity create and modify

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-08-15 21:16:12 -04:00
parent ae2f5c26ad
commit 9978c54431
2 changed files with 109 additions and 2 deletions
+51 -2
View File
@@ -172,8 +172,8 @@ class DefaultController extends ControllerAbstract {
'entity.fetch' => $this->entityFetch($tenantId, $userId, $data), 'entity.fetch' => $this->entityFetch($tenantId, $userId, $data),
'entity.extant' => $this->entityExtant($tenantId, $userId, $data), 'entity.extant' => $this->entityExtant($tenantId, $userId, $data),
'entity.delta' => $this->entityDelta($tenantId, $userId, $data), 'entity.delta' => $this->entityDelta($tenantId, $userId, $data),
'entity.create' => throw new InvalidArgumentException('Operation not implemented: ' . $operation), 'entity.create' => $this->entityCreate($tenantId, $userId, $data),
'entity.update' => throw new InvalidArgumentException('Operation not implemented: ' . $operation), 'entity.update' => $this->entityUpdate($tenantId, $userId, $data),
'entity.delete' => $this->entityDelete($tenantId, $userId, $data), 'entity.delete' => $this->entityDelete($tenantId, $userId, $data),
'entity.patch' => $this->entityPatch($tenantId, $userId, $data), 'entity.patch' => $this->entityPatch($tenantId, $userId, $data),
'entity.move' => $this->entityMove($tenantId, $userId, $data), 'entity.move' => $this->entityMove($tenantId, $userId, $data),
@@ -751,6 +751,55 @@ class DefaultController extends ControllerAbstract {
return $this->manager->entityDelta($tenantId, $userId, $targets); return $this->manager->entityDelta($tenantId, $userId, $targets);
} }
private function entityCreate(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['target'])) {
throw new InvalidArgumentException(self::ERR_MISSING_TARGET);
}
if (!is_string($data['target'])) {
throw new InvalidArgumentException(self::ERR_INVALID_TARGET);
}
if (!isset($data['properties'])) {
throw new InvalidArgumentException(self::ERR_MISSING_DATA);
}
if (!is_array($data['properties'])) {
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
}
$target = ResourceIdentifier::fromString($data['target']);
if (!$target instanceof CollectionIdentifier) {
throw new InvalidArgumentException('Invalid parameter: target must be provider:service:collection');
}
$options = $data['options'] ?? [];
if (!is_array($options)) {
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
}
return $this->manager->entityCreate($tenantId, $userId, $target, $data['properties'], $options);
}
private function entityUpdate(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['target'])) {
throw new InvalidArgumentException(self::ERR_MISSING_TARGET);
}
if (!is_string($data['target'])) {
throw new InvalidArgumentException(self::ERR_INVALID_TARGET);
}
if (!isset($data['properties'])) {
throw new InvalidArgumentException(self::ERR_MISSING_DATA);
}
if (!is_array($data['properties'])) {
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
}
$target = ResourceIdentifier::fromString($data['target']);
if (!$target instanceof EntityIdentifier) {
throw new InvalidArgumentException('Invalid parameter: target must be provider:service:collection:entity');
}
return $this->manager->entityModify($tenantId, $userId, $target, $data['properties']);
}
private function entityPatch(string $tenantId, string $userId, array $data): mixed { private function entityPatch(string $tenantId, string $userId, array $data): mixed {
if (!isset($data['targets'])) { if (!isset($data['targets'])) {
throw new InvalidArgumentException(self::ERR_MISSING_TARGETS); throw new InvalidArgumentException(self::ERR_MISSING_TARGETS);
+58
View File
@@ -9,6 +9,7 @@ use KTXC\Resource\ProviderManager;
use KTXF\Mail\Collection\CollectionBaseInterface; use KTXF\Mail\Collection\CollectionBaseInterface;
use KTXF\Mail\Collection\CollectionPropertiesMutableInterface; use KTXF\Mail\Collection\CollectionPropertiesMutableInterface;
use KTXF\Mail\Collection\ICollectionBase; use KTXF\Mail\Collection\ICollectionBase;
use KTXF\Mail\Entity\EntityBaseInterface;
use KTXF\Mail\Entity\IMessageBase; use KTXF\Mail\Entity\IMessageBase;
use KTXF\Mail\Object\Address; use KTXF\Mail\Object\Address;
use KTXF\Mail\Object\AddressInterface; use KTXF\Mail\Object\AddressInterface;
@@ -1129,6 +1130,63 @@ class Manager {
return $operationOutcome; return $operationOutcome;
} }
/**
* Create a new entity in a collection.
*
* @param MessagePropertiesMutableInterface|array $properties Entity properties
*
* @throws InvalidArgumentException
*/
public function entityCreate(string $tenantId, string $userId, CollectionIdentifier $target, MessagePropertiesMutableInterface|array $properties, array $options = []): EntityBaseInterface
{
$service = $this->serviceFetch($tenantId, $userId, $target->provider(), $target->service());
if ($service->getEnabled() === false) {
throw new InvalidArgumentException("Service '{$service->identifier()}' not found or is disabled");
}
if ($service instanceof ServiceEntityMutableInterface === false) {
throw new InvalidArgumentException("Service '{$service->identifier()}' does not support entity mutations");
}
if (!$service->capable(ServiceEntityMutableInterface::CAPABILITY_ENTITY_CREATE)) {
throw new InvalidArgumentException("Service '{$service->identifier()}' is not capable of creating entities");
}
if ($properties instanceof MessagePropertiesMutableInterface === false) {
$properties = $service->entityFresh()->getProperties()->jsonDeserialize($properties);
}
return $service->entityCreate($target, $properties, $options);
}
/**
* Modify an existing entity.
*
* The returned entity identifier is authoritative and may differ from the
* target identifier for providers that use replacement semantics.
*
* @param MessagePropertiesMutableInterface|array $properties Entity properties
*
* @throws InvalidArgumentException
*/
public function entityModify(string $tenantId, string $userId, EntityIdentifier $target, MessagePropertiesMutableInterface|array $properties): EntityBaseInterface
{
$service = $this->serviceFetch($tenantId, $userId, $target->provider(), $target->service());
if ($service->getEnabled() === false) {
throw new InvalidArgumentException("Service '{$service->identifier()}' not found or is disabled");
}
if ($service instanceof ServiceEntityMutableInterface === false) {
throw new InvalidArgumentException("Service '{$service->identifier()}' does not support entity mutations");
}
if (!$service->capable(ServiceEntityMutableInterface::CAPABILITY_ENTITY_MODIFY)) {
throw new InvalidArgumentException("Service '{$service->identifier()}' is not capable of modifying entities");
}
if ($properties instanceof MessagePropertiesMutableInterface === false) {
$properties = $service->entityFresh()->getProperties()->jsonDeserialize($properties);
}
return $service->entityModify($target, $properties);
}
/** /**
* Deletes entities * Deletes entities
* *