From 9978c544314d342129f90998561e06748081937e Mon Sep 17 00:00:00 2001 From: Sebastian Krupinski Date: Sat, 15 Aug 2026 21:16:12 -0400 Subject: [PATCH] feat: implement entity create and modify Signed-off-by: Sebastian Krupinski --- lib/Controllers/DefaultController.php | 53 +++++++++++++++++++++++- lib/Manager.php | 58 +++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/lib/Controllers/DefaultController.php b/lib/Controllers/DefaultController.php index 63548e8..4d2ed61 100644 --- a/lib/Controllers/DefaultController.php +++ b/lib/Controllers/DefaultController.php @@ -172,8 +172,8 @@ class DefaultController extends ControllerAbstract { 'entity.fetch' => $this->entityFetch($tenantId, $userId, $data), 'entity.extant' => $this->entityExtant($tenantId, $userId, $data), 'entity.delta' => $this->entityDelta($tenantId, $userId, $data), - 'entity.create' => throw new InvalidArgumentException('Operation not implemented: ' . $operation), - 'entity.update' => throw new InvalidArgumentException('Operation not implemented: ' . $operation), + 'entity.create' => $this->entityCreate($tenantId, $userId, $data), + 'entity.update' => $this->entityUpdate($tenantId, $userId, $data), 'entity.delete' => $this->entityDelete($tenantId, $userId, $data), 'entity.patch' => $this->entityPatch($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); } + 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 { if (!isset($data['targets'])) { throw new InvalidArgumentException(self::ERR_MISSING_TARGETS); diff --git a/lib/Manager.php b/lib/Manager.php index bf1a3f2..63664d3 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -9,6 +9,7 @@ use KTXC\Resource\ProviderManager; use KTXF\Mail\Collection\CollectionBaseInterface; use KTXF\Mail\Collection\CollectionPropertiesMutableInterface; use KTXF\Mail\Collection\ICollectionBase; +use KTXF\Mail\Entity\EntityBaseInterface; use KTXF\Mail\Entity\IMessageBase; use KTXF\Mail\Object\Address; use KTXF\Mail\Object\AddressInterface; @@ -1129,6 +1130,63 @@ class Manager { 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 *