diff --git a/lib/Controllers/DefaultController.php b/lib/Controllers/DefaultController.php index 0b4f076..bed3b4a 100644 --- a/lib/Controllers/DefaultController.php +++ b/lib/Controllers/DefaultController.php @@ -53,7 +53,7 @@ class DefaultController extends ControllerAbstract { public function __construct( private readonly SessionTenant $tenantIdentity, private readonly SessionIdentity $userIdentity, - private Manager $mailManager, + private Manager $manager, private readonly LoggerInterface $logger ) {} @@ -178,7 +178,7 @@ class DefaultController extends ControllerAbstract { $sources->jsonDeserialize($data['sources']); } - return $this->mailManager->providerList($tenantId, $userId, $sources); + return $this->manager->providerList($tenantId, $userId, $sources); } @@ -191,7 +191,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER); } - return $this->mailManager->providerFetch($tenantId, $userId, $data['identifier']); + return $this->manager->providerFetch($tenantId, $userId, $data['identifier']); } private function providerExtant(string $tenantId, string $userId, array $data): mixed { @@ -205,7 +205,7 @@ class DefaultController extends ControllerAbstract { $sources = new SourceSelector(); $sources->jsonDeserialize($data['sources']); - return $this->mailManager->providerExtant($tenantId, $userId, $sources); + return $this->manager->providerExtant($tenantId, $userId, $sources); } @@ -219,7 +219,7 @@ class DefaultController extends ControllerAbstract { $sources->jsonDeserialize($data['sources']); } - return $this->mailManager->serviceList($tenantId, $userId, $sources); + return $this->manager->serviceList($tenantId, $userId, $sources); } @@ -238,7 +238,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER); } - return $this->mailManager->serviceFetch($tenantId, $userId, $data['provider'], $data['identifier']); + return $this->manager->serviceFetch($tenantId, $userId, $data['provider'], $data['identifier']); } private function serviceExtant(string $tenantId, string $userId, array $data): mixed { @@ -252,7 +252,7 @@ class DefaultController extends ControllerAbstract { $sources = new SourceSelector(); $sources->jsonDeserialize($data['sources']); - return $this->mailManager->serviceExtant($tenantId, $userId, $sources); + return $this->manager->serviceExtant($tenantId, $userId, $sources); } private function serviceCreate(string $tenantId, string $userId, array $data): mixed { @@ -269,7 +269,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException(self::ERR_INVALID_DATA); } - return $this->mailManager->serviceCreate( + return $this->manager->serviceCreate( $tenantId, $userId, $data['provider'], @@ -300,7 +300,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException('Invalid parameter: delta must be a boolean'); } - return $this->mailManager->serviceUpdate( + return $this->manager->serviceUpdate( $tenantId, $userId, $data['provider'], @@ -324,7 +324,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER); } - return $this->mailManager->serviceDelete( + return $this->manager->serviceDelete( $tenantId, $userId, $data['provider'], @@ -345,7 +345,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException('Either a service identifier or location and identity must be provided for service test'); } - return $this->mailManager->serviceTest( + return $this->manager->serviceTest( $tenantId, $userId, $data['provider'], @@ -366,7 +366,7 @@ class DefaultController extends ControllerAbstract { $location = $data['location'] ?? null; $secret = $data['secret'] ?? null; - $discoverGenerator = $this->mailManager->serviceDiscover($tenantId, $userId, $provider, $identity, $location, $secret); + $discoverGenerator = $this->manager->serviceDiscover($tenantId, $userId, $provider, $identity, $location, $secret); $logger = $this->logger; $response = (function () use ($discoverGenerator, $version, $transaction, $logger): \Generator { @@ -417,7 +417,7 @@ class DefaultController extends ControllerAbstract { $filter = $data['filter'] ?? null; $sort = $data['sort'] ?? null; - return $this->mailManager->collectionList($tenantId, $userId, $sources, $filter, $sort); + return $this->manager->collectionList($tenantId, $userId, $sources, $filter, $sort); } private function collectionFetch(string $tenantId, string $userId, array $data): mixed { @@ -438,7 +438,7 @@ class DefaultController extends ControllerAbstract { $list = []; foreach ($targetIdentifiers as $targetIdentifier) { - $list[(string)$targetIdentifier] = $this->mailManager->collectionFetch( + $list[(string)$targetIdentifier] = $this->manager->collectionFetch( $tenantId, $userId, $targetIdentifier->provider(), @@ -466,7 +466,7 @@ class DefaultController extends ControllerAbstract { } $sources = $this->createSourceSelectorFromIdentifiers($sources); - return $this->mailManager->collectionExtant($tenantId, $userId, $sources); + return $this->manager->collectionExtant($tenantId, $userId, $sources); } private function collectionCreate(string $tenantId, string $userId, array $data): mixed { @@ -499,7 +499,7 @@ class DefaultController extends ControllerAbstract { } } - return $this->mailManager->collectionCreate( + return $this->manager->collectionCreate( $tenantId, $userId, $data['provider'], @@ -528,7 +528,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException('Invalid parameter: target must be provider:service:collection'); } - return $this->mailManager->collectionUpdate( + return $this->manager->collectionUpdate( $tenantId, $userId, $targetIdentifier, @@ -549,7 +549,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException('Invalid parameter: target must be provider:service:collection'); } - $result = $this->mailManager->collectionDelete($tenantId, $userId, $targetIdentifier, $data['options'] ?? [] ); + $result = $this->manager->collectionDelete($tenantId, $userId, $targetIdentifier, $data['options'] ?? [] ); if (is_bool($result)) { return [ @@ -592,7 +592,7 @@ class DefaultController extends ControllerAbstract { } - return $this->mailManager->collectionMove($tenantId, $userId, $target, $source); + return $this->manager->collectionMove($tenantId, $userId, $target, $source); } // ==================== Entity Operations ==================== @@ -618,7 +618,7 @@ class DefaultController extends ControllerAbstract { $sort = $data['sort'] ?? null; $range = $data['range'] ?? null; - return $this->mailManager->entityListBulk($tenantId, $userId, $sources, $filter, $sort, $range); + return $this->manager->entityListBulk($tenantId, $userId, $sources, $filter, $sort, $range); } @@ -643,7 +643,7 @@ class DefaultController extends ControllerAbstract { $sort = $data['sort'] ?? null; $range = $data['range'] ?? null; - $entityGenerator = $this->mailManager->entityListStream($tenantId, $userId, $sources, $filter, $sort, $range); + $entityGenerator = $this->manager->entityListStream($tenantId, $userId, $sources, $filter, $sort, $range); $logger = $this->logger; $responseGenerator = (function () use ($entityGenerator, $version, $transaction, $logger): \Generator { @@ -673,50 +673,6 @@ 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['targets'])) { throw new InvalidArgumentException(self::ERR_MISSING_TARGETS); @@ -732,7 +688,7 @@ class DefaultController extends ControllerAbstract { } } - return $this->mailManager->entityFetchBulk( + return $this->manager->entityFetchBulk( $tenantId, $userId, ...$targets->all() @@ -750,7 +706,7 @@ class DefaultController extends ControllerAbstract { $sources = new SourceSelector(); $sources->jsonDeserialize($data['sources']); - return $this->mailManager->entityExtant($tenantId, $userId, $sources); + return $this->manager->entityExtant($tenantId, $userId, $sources); } private function entityDelta(string $tenantId, string $userId, array $data): mixed { @@ -764,25 +720,7 @@ class DefaultController extends ControllerAbstract { $sources = new SourceSelector(); $sources->jsonDeserialize($data['sources']); - return $this->mailManager->entityDelta($tenantId, $userId, $sources); - } - - private function entityDelete(string $tenantId, string $userId, array $data): mixed { - if (!isset($data['targets'])) { - throw new InvalidArgumentException(self::ERR_MISSING_TARGETS); - } - if (!is_array($data['targets'])) { - throw new InvalidArgumentException(self::ERR_INVALID_TARGETS); - } - - $targets = ResourceIdentifiers::fromArray($data['targets']); - foreach ($targets as $target) { - if (!$target instanceof EntityIdentifier) { - throw new InvalidArgumentException('Invalid parameter: targets must contain provider:service:collection:entity identifiers'); - } - } - - return $this->mailManager->entityDelete($tenantId, $userId, ...$targets->all()); + return $this->manager->entityDelta($tenantId, $userId, $sources); } private function entityPatch(string $tenantId, string $userId, array $data): mixed { @@ -806,7 +744,25 @@ class DefaultController extends ControllerAbstract { } } - return $this->mailManager->entityPatch($tenantId, $userId, $data['properties'], ...$targets->all()); + return $this->manager->entityPatch($tenantId, $userId, $data['properties'], ...$targets->all()); + } + + private function entityDelete(string $tenantId, string $userId, array $data): mixed { + if (!isset($data['targets'])) { + throw new InvalidArgumentException(self::ERR_MISSING_TARGETS); + } + if (!is_array($data['targets'])) { + throw new InvalidArgumentException(self::ERR_INVALID_TARGETS); + } + + $targets = ResourceIdentifiers::fromArray($data['targets']); + foreach ($targets as $target) { + if (!$target instanceof EntityIdentifier) { + throw new InvalidArgumentException('Invalid parameter: targets must contain provider:service:collection:entity identifiers'); + } + } + + return $this->manager->entityDelete($tenantId, $userId, ...$targets->all()); } private function entityMove(string $tenantId, string $userId, array $data): mixed { @@ -835,7 +791,7 @@ class DefaultController extends ControllerAbstract { } } - return $this->mailManager->entityMove($tenantId, $userId, $target, ...$sources->all()); + return $this->manager->entityMove($tenantId, $userId, $target, ...$sources->all()); } private function entitySubmit(string $tenantId, string $userId, array $data): mixed { @@ -846,7 +802,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException(self::ERR_INVALID_SENDER); } - return $this->mailManager->entitySubmit( + return $this->manager->entitySubmit( $tenantId, $userId, $data['sender'], @@ -869,7 +825,7 @@ class DefaultController extends ControllerAbstract { $target = ResourceIdentifier::fromString($data['target']); $logger = $this->logger; - $result = $this->mailManager->entityDownload($tenantId, $userId, $target, $part); + $result = $this->manager->entityDownload($tenantId, $userId, $target, $part); $filename = $result->filename(); $asciiFilename = preg_replace('/[^\x20-\x7E]|[\\\\"]/', '_', $filename); @@ -916,7 +872,7 @@ class DefaultController extends ControllerAbstract { throw new InvalidArgumentException('Invalid part selector'); } - $resource = $this->mailManager->entityDownload($tenantId, $userId, $target, $part); + $resource = $this->manager->entityDownload($tenantId, $userId, $target, $part); $bytes = ''; foreach ($resource->stream() as $chunk) { @@ -935,4 +891,47 @@ class DefaultController extends ControllerAbstract { return $results; } + 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; + } }