refactor: use resource identifiers
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -21,7 +21,10 @@ use KTXF\Resource\Identifier\CollectionIdentifier;
|
||||
use KTXF\Resource\Identifier\EntityIdentifier;
|
||||
use KTXF\Resource\Identifier\ResourceIdentifier;
|
||||
use KTXF\Resource\Identifier\ResourceIdentifiers;
|
||||
use KTXF\Resource\Identifier\ServiceIdentifier;
|
||||
use KTXF\Resource\Provider\ResourceServiceLocationInterface;
|
||||
use KTXF\Resource\Selector\CollectionSelector;
|
||||
use KTXF\Resource\Selector\ServiceSelector;
|
||||
use KTXF\Resource\Selector\SourceSelector;
|
||||
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
||||
use KTXM\MailManager\Manager;
|
||||
@@ -41,6 +44,7 @@ class DefaultController extends ControllerAbstract {
|
||||
private const ERR_MISSING_SERVICE = 'Missing parameter: service';
|
||||
private const ERR_MISSING_COLLECTION = 'Missing parameter: collection';
|
||||
private const ERR_MISSING_DATA = 'Missing parameter: data';
|
||||
private const ERR_MISSING_SOURCE = 'Missing parameter: source';
|
||||
private const ERR_MISSING_SOURCES = 'Missing parameter: sources';
|
||||
private const ERR_MISSING_TARGET = 'Missing parameter: target';
|
||||
private const ERR_MISSING_TARGETS = 'Missing parameter: targets';
|
||||
@@ -51,6 +55,7 @@ class DefaultController extends ControllerAbstract {
|
||||
private const ERR_INVALID_IDENTIFIER = 'Invalid parameter: identifier must be a string';
|
||||
private const ERR_INVALID_COLLECTION = 'Invalid parameter: collection must be a string or integer';
|
||||
private const ERR_INVALID_SOURCES = 'Invalid parameter: sources must be an array';
|
||||
private const ERR_INVALID_SOURCE = 'Invalid parameter: source must be a string';
|
||||
private const ERR_INVALID_TARGET = 'Invalid parameter: target must be an array';
|
||||
private const ERR_INVALID_TARGETS = 'Invalid parameter: targets must be an array';
|
||||
private const ERR_INVALID_IDENTIFIERS = 'Invalid parameter: identifiers must be an array';
|
||||
@@ -410,8 +415,14 @@ class DefaultController extends ControllerAbstract {
|
||||
private function collectionList(string $tenantId, string $userId, array $data): mixed {
|
||||
$sources = null;
|
||||
if (isset($data['sources']) && is_array($data['sources'])) {
|
||||
$sources = new SourceSelector();
|
||||
$sources->jsonDeserialize($data['sources']);
|
||||
// TODO: Refactor to use identifiers directly
|
||||
$sources = ResourceIdentifiers::fromArray($data['sources']);
|
||||
foreach ($sources as $source) {
|
||||
if (!$source instanceof CollectionIdentifier && !$source instanceof ServiceIdentifier) {
|
||||
throw new InvalidArgumentException('Invalid parameter: sources must contain provider:service, provider:service:collection, or provider:service:collection:entity identifiers');
|
||||
}
|
||||
}
|
||||
$sources = $this->createSourceSelectorFromIdentifiers($sources);
|
||||
}
|
||||
|
||||
$filter = $data['filter'] ?? null;
|
||||
@@ -421,44 +432,50 @@ class DefaultController extends ControllerAbstract {
|
||||
}
|
||||
|
||||
private function collectionFetch(string $tenantId, string $userId, array $data): mixed {
|
||||
if (!isset($data['provider'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||
if (!isset($data['targets'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_TARGETS);
|
||||
}
|
||||
if (!is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||
if (!is_array($data['targets'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_TARGETS);
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_SERVICE);
|
||||
|
||||
// TODO: Refactor to use identifiers directly
|
||||
$targetIdentifiers = ResourceIdentifiers::fromArray($data['targets']);
|
||||
foreach ($targetIdentifiers as $targetIdentifier) {
|
||||
if (!$targetIdentifier instanceof CollectionIdentifier) {
|
||||
throw new InvalidArgumentException('Invalid parameter: target must be provider:service:collection');
|
||||
}
|
||||
}
|
||||
if (!is_string($data['service'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||
|
||||
$list = [];
|
||||
foreach ($targetIdentifiers as $targetIdentifier) {
|
||||
$list[(string)$targetIdentifier] = $this->mailManager->collectionFetch(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$targetIdentifier->provider(),
|
||||
$targetIdentifier->service(),
|
||||
$targetIdentifier->collection()
|
||||
);
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||
}
|
||||
if (!is_string($data['identifier']) && !is_int($data['identifier'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||
}
|
||||
|
||||
return $this->mailManager->collectionFetch(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['identifier']
|
||||
);
|
||||
return $list;
|
||||
}
|
||||
|
||||
private function collectionExtant(string $tenantId, string $userId, array $data): mixed {
|
||||
if (!isset($data['sources'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_SOURCES);
|
||||
if (!isset($data['targets'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_TARGETS);
|
||||
}
|
||||
if (!is_array($data['sources'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||
if (!is_array($data['targets'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_TARGETS);
|
||||
}
|
||||
|
||||
$sources = new SourceSelector();
|
||||
$sources->jsonDeserialize($data['sources']);
|
||||
// TODO: Refactor to use identifiers directly
|
||||
$sources = ResourceIdentifiers::fromArray($data['targets']);
|
||||
foreach ($sources as $source) {
|
||||
if (!$source instanceof CollectionIdentifier) {
|
||||
throw new InvalidArgumentException('Invalid parameter: targets must contain provider:service, provider:service:collection, or provider:service:collection:entity identifiers');
|
||||
}
|
||||
}
|
||||
$sources = $this->createSourceSelectorFromIdentifiers($sources);
|
||||
|
||||
return $this->mailManager->collectionExtant($tenantId, $userId, $sources);
|
||||
}
|
||||
@@ -476,8 +493,8 @@ class DefaultController extends ControllerAbstract {
|
||||
if (!is_string($data['service'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||
}
|
||||
if (isset($data['collection']) && !is_string($data['collection']) && !is_int($data['collection'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||
if (isset($data['target']) && !is_string($data['target']) && !is_int($data['target'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_TARGET);
|
||||
}
|
||||
if (!isset($data['properties'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_DATA);
|
||||
@@ -485,35 +502,30 @@ class DefaultController extends ControllerAbstract {
|
||||
if (!is_array($data['properties'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
|
||||
}
|
||||
|
||||
if (isset($data['target'])) {
|
||||
$targetIdentifier = ResourceIdentifier::fromString($data['target']);
|
||||
if (!$targetIdentifier instanceof CollectionIdentifier) {
|
||||
throw new InvalidArgumentException('Invalid parameter: target must be provider:service:collection');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->mailManager->collectionCreate(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['collection'] ?? null,
|
||||
$targetIdentifier->collection() ?? null,
|
||||
$data['properties']
|
||||
);
|
||||
}
|
||||
|
||||
private function collectionUpdate(string $tenantId, string $userId, array $data): mixed {
|
||||
if (!isset($data['provider'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||
if (!isset($data['target'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_TARGET);
|
||||
}
|
||||
if (!is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_SERVICE);
|
||||
}
|
||||
if (!is_string($data['service'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||
}
|
||||
if (!is_string($data['identifier']) && !is_int($data['identifier'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||
if (!is_string($data['target'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_TARGET);
|
||||
}
|
||||
if (!isset($data['properties'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_DATA);
|
||||
@@ -521,45 +533,34 @@ class DefaultController extends ControllerAbstract {
|
||||
if (!is_array($data['properties'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
|
||||
}
|
||||
|
||||
$targetIdentifier = ResourceIdentifier::fromString($data['target']);
|
||||
if (!$targetIdentifier instanceof CollectionIdentifier) {
|
||||
throw new InvalidArgumentException('Invalid parameter: target must be provider:service:collection');
|
||||
}
|
||||
|
||||
return $this->mailManager->collectionUpdate(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['identifier'],
|
||||
$targetIdentifier,
|
||||
$data['properties']
|
||||
);
|
||||
}
|
||||
|
||||
private function collectionDelete(string $tenantId, string $userId, array $data): mixed {
|
||||
if (!isset($data['provider'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||
if (!isset($data['target'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_TARGET);
|
||||
}
|
||||
if (!is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||
if (!is_string($data['target'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_TARGET);
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_SERVICE);
|
||||
}
|
||||
if (!is_string($data['service'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||
}
|
||||
if (!is_string($data['identifier']) && !is_int($data['identifier'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER);
|
||||
|
||||
$targetIdentifier = ResourceIdentifier::fromString($data['target']);
|
||||
if (!$targetIdentifier instanceof CollectionIdentifier) {
|
||||
throw new InvalidArgumentException('Invalid parameter: target must be provider:service:collection');
|
||||
}
|
||||
|
||||
$result = $this->mailManager->collectionDelete(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['identifier'],
|
||||
$data['options'] ?? []
|
||||
);
|
||||
$result = $this->mailManager->collectionDelete($tenantId, $userId, $targetIdentifier, $data['options'] ?? [] );
|
||||
|
||||
if (is_bool($result)) {
|
||||
return [
|
||||
@@ -585,10 +586,10 @@ class DefaultController extends ControllerAbstract {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_TARGET);
|
||||
}
|
||||
if (!isset($data['source'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_SOURCE);
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_SOURCES);
|
||||
}
|
||||
if (!is_string($data['source'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_SOURCE);
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||
}
|
||||
|
||||
$target = ResourceIdentifier::fromString($data['target']);
|
||||
@@ -615,8 +616,14 @@ class DefaultController extends ControllerAbstract {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||
}
|
||||
|
||||
$sources = new SourceSelector();
|
||||
$sources->jsonDeserialize($data['sources']);
|
||||
$sources = ResourceIdentifiers::fromArray($data['sources']);
|
||||
foreach ($sources as $source) {
|
||||
if (!$source instanceof EntityIdentifier) {
|
||||
throw new InvalidArgumentException('Invalid parameter: sources must contain provider:service:collection:entity identifiers');
|
||||
}
|
||||
}
|
||||
|
||||
$sources = $this->createSourceSelectorFromIdentifiers($sources);
|
||||
|
||||
$filter = $data['filter'] ?? null;
|
||||
$sort = $data['sort'] ?? null;
|
||||
@@ -634,8 +641,14 @@ class DefaultController extends ControllerAbstract {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||
}
|
||||
|
||||
$sources = new SourceSelector();
|
||||
$sources->jsonDeserialize($data['sources']);
|
||||
$sources = ResourceIdentifiers::fromArray($data['sources']);
|
||||
foreach ($sources as $source) {
|
||||
if (!$source instanceof ServiceIdentifier && !$source instanceof CollectionIdentifier) {
|
||||
throw new InvalidArgumentException('Invalid parameter: sources must contain provider:service or provider:service:collection identifiers');
|
||||
}
|
||||
}
|
||||
|
||||
$sources = $this->createSourceSelectorFromIdentifiers($sources);
|
||||
|
||||
$filter = $data['filter'] ?? null;
|
||||
$sort = $data['sort'] ?? null;
|
||||
@@ -671,6 +684,50 @@ class DefaultController extends ControllerAbstract {
|
||||
return new StreamedNdJsonResponse($responseGenerator, 1, 200, ['Content-Type' => 'application/json']);
|
||||
}
|
||||
|
||||
private function createSourceSelectorFromIdentifiers(ResourceIdentifiers $identifiers): SourceSelector {
|
||||
$sources = new SourceSelector();
|
||||
|
||||
foreach ($identifiers as $identifier) {
|
||||
if (!$identifier instanceof ServiceIdentifier) {
|
||||
throw new InvalidArgumentException('Invalid parameter: sources must contain provider:service, provider:service:collection, or provider:service:collection:entity identifiers');
|
||||
}
|
||||
|
||||
$provider = $identifier->provider();
|
||||
$service = $identifier->service();
|
||||
|
||||
if (!isset($sources[$provider])) {
|
||||
$sources[$provider] = new ServiceSelector();
|
||||
}
|
||||
|
||||
$serviceSelector = $sources[$provider];
|
||||
if (!$serviceSelector instanceof ServiceSelector) {
|
||||
throw new InvalidArgumentException('Invalid parameter: sources must contain provider:service:collection selectors');
|
||||
}
|
||||
|
||||
if ($identifier instanceof ServiceIdentifier && !$identifier instanceof CollectionIdentifier) {
|
||||
$serviceSelector[$service] = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($serviceSelector[$service]) && $serviceSelector[$service] === true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($serviceSelector[$service])) {
|
||||
$serviceSelector[$service] = new CollectionSelector();
|
||||
}
|
||||
|
||||
$collectionSelector = $serviceSelector[$service];
|
||||
if (!$collectionSelector instanceof CollectionSelector) {
|
||||
throw new InvalidArgumentException('Invalid parameter: sources must contain provider:service:collection selectors');
|
||||
}
|
||||
|
||||
$collectionSelector[$identifier->collection()] = true;
|
||||
}
|
||||
|
||||
return $sources;
|
||||
}
|
||||
|
||||
private function entityFetch(string $tenantId, string $userId, array $data): mixed {
|
||||
if (!isset($data['provider'])) {
|
||||
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||
|
||||
191
lib/Manager.php
191
lib/Manager.php
@@ -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];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user