refactor: use resource identifiers
Some checks failed
Build Test / test (pull_request) Successful in 26s
JS Unit Tests / test (pull_request) Failing after 29s
PHP Unit Tests / test (pull_request) Successful in 56s

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-05-14 22:34:18 -04:00
parent 69d4b2f42c
commit c7ef2c5495
13 changed files with 425 additions and 621 deletions

View File

@@ -8,6 +8,7 @@ use InvalidArgumentException;
use KTXC\Resource\ProviderManager;
use KTXF\Mail\Collection\CollectionBaseInterface;
use KTXF\Mail\Collection\CollectionMutableInterface;
use KTXF\Mail\Collection\CollectionPropertiesMutableInterface;
use KTXF\Mail\Collection\ICollectionBase;
use KTXF\Mail\Entity\Address;
use KTXF\Mail\Entity\IMessageBase;
@@ -21,7 +22,9 @@ use KTXF\Mail\Queue\SendOptions;
use KTXF\Mail\Service\IServiceSend;
use KTXF\Mail\Service\ServiceBaseInterface;
use KTXF\Mail\Service\ServiceCollectionMutableInterface;
use KTXF\Mail\Service\ServiceConfigurableInterface;
use KTXF\Mail\Service\ServiceEntityMutableInterface;
use KTXF\Mail\Service\ServiceMutableInterface;
use KTXF\Resource\Filter\IFilter;
use KTXF\Resource\Identifier\CollectionIdentifier;
use KTXF\Resource\Identifier\EntityIdentifier;
@@ -266,6 +269,9 @@ class Manager {
if ($service === null) {
throw new InvalidArgumentException("Service '$serviceId' not found");
}
if ($service instanceof ServiceMutableInterface === false) {
throw new InvalidArgumentException("Service '$serviceId' is not mutable and cannot be updated");
}
// Update with new data
$service->jsonDeserialize($data, $delta);
@@ -416,7 +422,7 @@ class Manager {
throw new InvalidArgumentException('Service identity not valid');
}
/** @var ServiceMutableInterface $service */
/** @var ServiceConfigurableInterface|ServiceMutableInterface $service */
$service = $provider->serviceFresh();
if ($location instanceof ResourceServiceLocationInterface === false) {
$location = $service->freshLocation($location['type'], (array)$location);
@@ -572,18 +578,16 @@ class Manager {
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param string|int|null $collectionId collection identifier (parent collection)
* @param CollectionMutableInterface|array $object collection to create
* @param CollectionIdentifier $target target parent collection identifier
* @param CollectionPropertiesMutableInterface|array $properties properties for the new collection
* @param array $options additional options for creation
*
* @return CollectionBaseInterface
* @throws InvalidArgumentException
*/
public function collectionCreate(string $tenantId, string $userId, string $providerId, string|int $serviceId, string|int|null $collectionId, CollectionMutableInterface|array $object, array $options = []): CollectionBaseInterface {
public function collectionCreate(string $tenantId, string $userId, string $provider, string|int $service, CollectionIdentifier|null $target, CollectionPropertiesMutableInterface|array $properties, array $options = []): CollectionBaseInterface {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
$service = $this->serviceFetch($tenantId, $userId, $provider, $service);
// Check if service supports collection creation
if (!($service instanceof ServiceCollectionMutableInterface)) {
@@ -593,15 +597,14 @@ class Manager {
throw new InvalidArgumentException("Service is not capable of creating collections");
}
if (is_array($object)) {
$collection = $service->collectionFresh();
$collection->getProperties()->jsonDeserialize($object);
if (is_array($properties)) {
$collection = $service->collectionFresh()->getProperties()->jsonDeserialize($properties);
} else {
$collection = $object;
$collection = $properties;
}
// Create collection
return $service->collectionCreate($collectionId, $collection, $options);
return $service->collectionCreate($target, $collection, $options);
}
/**
@@ -609,17 +612,15 @@ class Manager {
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param string|int $collectionId collection identifier
* @param CollectionMutableInterface|array $object collection to modify
* @param CollectionIdentifier $target target collection identifier
* @param CollectionPropertiesMutableInterface|array $properties properties to modify
*
* @return CollectionBaseInterface
* @throws InvalidArgumentException
*/
public function collectionUpdate(string $tenantId, string $userId, string $providerId, string|int $serviceId, string|int $collectionId, CollectionMutableInterface|array $object): CollectionBaseInterface {
public function collectionUpdate(string $tenantId, string $userId, CollectionIdentifier $target, CollectionPropertiesMutableInterface|array $properties): CollectionBaseInterface {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
$service = $this->serviceFetch($tenantId, $userId, $target->provider(), $target->service());
// Check if service supports collection creation
if (!($service instanceof ServiceCollectionMutableInterface)) {
@@ -629,15 +630,14 @@ class Manager {
throw new InvalidArgumentException("Service is not capable of updating collections");
}
if (is_array($object)) {
$collection = $service->collectionFresh();
$collection->getProperties()->jsonDeserialize($object);
if (is_array($properties)) {
$mutation = $service->collectionFresh()->getProperties()->jsonDeserialize($properties);
} else {
$collection = $object;
$mutation = $properties;
}
// Update collection
return $service->collectionUpdate($collectionId, $collection);
return $service->collectionUpdate($target, $mutation);
}
/**
@@ -647,15 +647,14 @@ class Manager {
*
* @param string $tenantId Tenant identifier
* @param string|null $userId User identifier for context
* @param string $providerId Provider identifier
* @param string|int $serviceId Service identifier
* @param string|int $collectionId Collection identifier
* @param CollectionIdentifier $target Target collection identifier
* @param array $options Additional options for deletion (e.g., 'force' => true to force delete even if not empty)
*
* @return CollectionBaseInterface|null
*/
public function collectionDelete(string $tenantId, ?string $userId, string $providerId, string|int $serviceId, string|int $collectionId, array $options = []): CollectionBaseInterface | bool {
public function collectionDelete(string $tenantId, ?string $userId, CollectionIdentifier $target, array $options = []): CollectionBaseInterface | bool {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
$service = $this->serviceFetch($tenantId, $userId, $target->provider(), $target->service());
// Check if service supports collection deletion
if (!($service instanceof ServiceCollectionMutableInterface)) {
@@ -668,7 +667,7 @@ class Manager {
$force = $options['force'] ?? false;
// delete collection
return $service->collectionDelete($collectionId, $force);
return $service->collectionDelete($target, $force);
}
/**
@@ -704,7 +703,7 @@ class Manager {
}
// move collection
return $service->collectionMove($source->collection(), $target->collection());
return $service->collectionMove($target, $source);
}
// ==================== Message Operations ====================
@@ -1052,7 +1051,6 @@ class Manager {
}
// Temporarily disabled check until all methods are properly implemented from ServiceEntityMutableInterface
/*
if (!($service instanceof ServiceEntityMutableInterface)) {
foreach ($serviceSources as $identifier) {
$operationOutcome[(string)$identifier] = [
@@ -1072,7 +1070,6 @@ class Manager {
}
continue;
}
*/
return $service->entityDelete(...$serviceSources->all());
}
@@ -1170,12 +1167,12 @@ class Manager {
$targetService = $this->serviceFetch($tenantId, $userId, $target->provider(), $target->service());
// Check if service supports entity move
// Temporarily disabled check until all methods are properly implemented from ServiceEntityMutableInterface
/*
if ($targetService instanceof ServiceEntityMutableInterface === false) {
return [];
}
*/
if (!$targetService->capable(ServiceEntityMutableInterface::CAPABILITY_ENTITY_MOVE)) {
return [];
}
$operationOutcome = [];
@@ -1190,128 +1187,4 @@ class Manager {
return $operationOutcome;
}
/**
* Send a mail message
*
* Routes the message to the appropriate service based on the `from` address.
* By default, messages are queued; use SendOptions::immediate() for urgent messages.
*
* @since 2025.05.01
*
* @param string $tenantId Tenant identifier
* @param string|null $userId User identifier for context
* @param IMessageMutable $message Message to send
* @param SendOptions|null $options Delivery options (defaults to queued)
*
* @return string Job ID for queued messages, or Message ID for immediate sends
*
* @throws SendException On immediate send failure
* @throws InvalidArgumentException If no suitable service found
*/
public function entityTransmit(string $tenantId, ?string $userId, string $providerId, string|int $serviceId, array $data): string {
$options = $options ?? new SendOptions();
// Find the appropriate service
$from = $message->getFrom();
if ($from !== null) {
$service = $this->serviceFindByAddress($tenantId, $userId, $from->getAddress());
}
if ($service === null) {
throw new InvalidArgumentException('No mail service found for the message sender address');
}
// Verify service can send
if (!($service instanceof IServiceSend) || !$service->capable(IServiceSend::CAPABILITY_SEND)) {
throw new InvalidArgumentException('Selected mail service does not support sending');
}
// replace internal address for external 'from'
$message->setFrom((new Address())->setAddress('system@ktrix.local'));
// Immediate send bypasses queue
if ($options->immediate) {
$this->logger->debug('Sending mail immediately', [
'tenant' => $tenantId,
'provider' => $service->in(),
'service' => $service->id(),
'to' => array_map(fn($a) => $a->getAddress(), $message->getTo()),
]);
return $service->messageSend($message);
}
// Queue the message
$jobId = $this->queue->enqueue(
$tenantId,
$service->in(),
$service->id(),
$message,
$options
);
$this->logger->debug('Mail queued for delivery', [
'tenant' => $tenantId,
'jobId' => $jobId,
'provider' => $service->in(),
'service' => $service->id(),
'priority' => $options->priority,
]);
return $jobId;
}
/**
* Process queued mail for a tenant
*
* Called by the mail daemon to process pending messages.
*
* @since 2025.05.01
*
* @param string $tenantId Tenant identifier
* @param int $batchSize Maximum messages to process
*
* @return array{processed: int, failed: int}
*/
public function queueProcess(string $tenantId, int $batchSize = 50): array {
$processed = 0;
$failed = 0;
$jobs = $this->queue->dequeue($tenantId, $batchSize);
foreach ($jobs as $job) {
try {
$service = $this->serviceFetch($tenantId, null, $job->providerId, $job->serviceId);
if ($service === null || !($service instanceof IServiceSend)) {
throw new SendException("Service not found or cannot send: {$job->providerId}/{$job->serviceId}");
}
$messageId = $service->messageSend($job->message);
$this->queue->acknowledge($job->id, $messageId);
$processed++;
$this->logger->debug('Mail sent from queue', [
'tenant' => $tenantId,
'jobId' => $job->id,
'messageId' => $messageId,
]);
} catch (\Throwable $e) {
$isPermanent = $e instanceof SendException && $e->permanent;
$this->queue->reject($job->id, $e->getMessage(), !$isPermanent);
$failed++;
$this->logger->warning('Mail send failed', [
'tenant' => $tenantId,
'jobId' => $job->id,
'error' => $e->getMessage(),
'permanent' => $isPermanent,
]);
}
}
return ['processed' => $processed, 'failed' => $failed];
}
}