refactor: remove sources selector in backend

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-06-19 23:45:41 -04:00
parent af227f35f9
commit 9845262e62
2 changed files with 251 additions and 303 deletions
+80 -114
View File
@@ -24,9 +24,6 @@ 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;
use Psr\Log\LoggerInterface;
@@ -41,6 +38,7 @@ class DefaultController extends ControllerAbstract {
private const ERR_MISSING_SOURCES = 'Missing parameter: sources';
private const ERR_MISSING_TARGET = 'Missing parameter: target';
private const ERR_MISSING_TARGETS = 'Missing parameter: targets';
private const ERR_MISSING_SENDER = 'Missing parameter: sender';
private const ERR_INVALID_OPERATION = 'Invalid operation: ';
private const ERR_INVALID_PROVIDER = 'Invalid parameter: provider must be a string';
private const ERR_INVALID_SERVICE = 'Invalid parameter: service must be a string';
@@ -48,6 +46,7 @@ class DefaultController extends ControllerAbstract {
private const ERR_INVALID_SOURCES = 'Invalid parameter: sources must be an array';
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_SENDER = 'Invalid parameter: sender must be a string';
private const ERR_INVALID_DATA = 'Invalid parameter: data must be an array';
public function __construct(
@@ -172,40 +171,48 @@ class DefaultController extends ControllerAbstract {
private function providerList(string $tenantId, string $userId, array $data): mixed {
$sources = null;
if (isset($data['sources']) && is_array($data['sources'])) {
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
}
if (isset($data['targets'])) {
if (!is_array($data['targets'])) {
throw new InvalidArgumentException(self::ERR_INVALID_TARGETS);
}
foreach ($data['targets'] as $target) {
if (!is_string($target)) {
throw new InvalidArgumentException(self::ERR_INVALID_TARGETS);
}
}
}
return $this->manager->providerList($tenantId, $userId, $sources);
return $this->manager->providerList($tenantId, $userId, $data['targets'] ?? []);
}
private function providerFetch(string $tenantId, string $userId, array $data): mixed {
if (!isset($data['identifier'])) {
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
if (!isset($data['target'])) {
throw new InvalidArgumentException(self::ERR_MISSING_TARGET);
}
if (!is_string($data['identifier'])) {
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER);
if (!is_string($data['target'])) {
throw new InvalidArgumentException(self::ERR_INVALID_TARGET);
}
return $this->manager->providerFetch($tenantId, $userId, $data['identifier']);
return $this->manager->providerFetch($tenantId, $userId, $data['target']);
}
private function providerExtant(string $tenantId, string $userId, array $data): mixed {
if (!isset($data['sources'])) {
throw new InvalidArgumentException(self::ERR_MISSING_SOURCES);
}
if (!is_array($data['sources'])) {
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
}
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
if (!isset($data['targets'])) {
throw new InvalidArgumentException(self::ERR_MISSING_TARGETS);
}
foreach ($data['targets'] as $target) {
if (!is_string($target)) {
throw new InvalidArgumentException(self::ERR_INVALID_TARGETS);
}
}
return $this->manager->providerExtant($tenantId, $userId, $sources);
return $this->manager->providerExtant($tenantId, $userId, $data['targets']);
}
@@ -213,13 +220,17 @@ class DefaultController extends ControllerAbstract {
private function serviceList(string $tenantId, string $userId, array $data): mixed {
$sources = null;
if (isset($data['sources']) && is_array($data['sources'])) {
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
$targets = null;
if (isset($data['targets']) && is_array($data['targets'])) {
$targets = ResourceIdentifiers::fromArray($data['targets']);
foreach ($targets as $target) {
if (!$target instanceof CollectionIdentifier && !$target instanceof ServiceIdentifier) {
throw new InvalidArgumentException('Invalid parameter: targets must contain provider:service, provider:service:collection, or provider:service:collection:entity identifiers');
}
}
}
return $this->manager->serviceList($tenantId, $userId, $sources);
return $this->manager->serviceList($tenantId, $userId, $targets);
}
@@ -243,16 +254,20 @@ class DefaultController extends ControllerAbstract {
private function serviceExtant(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']);
return $this->manager->serviceExtant($tenantId, $userId, $sources);
$targets = ResourceIdentifiers::fromArray($data['targets']);
foreach ($targets as $target) {
if (!$target instanceof ServiceIdentifier) {
throw new InvalidArgumentException('Invalid parameter: targets must contain provider:service identifiers');
}
}
return $this->manager->serviceExtant($tenantId, $userId, $targets);
}
private function serviceCreate(string $tenantId, string $userId, array $data): mixed {
@@ -404,14 +419,12 @@ class DefaultController extends ControllerAbstract {
private function collectionList(string $tenantId, string $userId, array $data): mixed {
$sources = null;
if (isset($data['sources']) && is_array($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;
@@ -428,7 +441,6 @@ class DefaultController extends ControllerAbstract {
throw new InvalidArgumentException(self::ERR_INVALID_TARGETS);
}
// TODO: Refactor to use identifiers directly
$targetIdentifiers = ResourceIdentifiers::fromArray($data['targets']);
foreach ($targetIdentifiers as $targetIdentifier) {
if (!$targetIdentifier instanceof CollectionIdentifier) {
@@ -436,16 +448,11 @@ class DefaultController extends ControllerAbstract {
}
}
$list = [];
foreach ($targetIdentifiers as $targetIdentifier) {
$list[(string)$targetIdentifier] = $this->manager->collectionFetch(
$tenantId,
$userId,
$targetIdentifier->provider(),
$targetIdentifier->service(),
$targetIdentifier->collection()
);
}
$list = $this->manager->collectionFetch(
$tenantId,
$userId,
$targetIdentifier
);
return $list;
}
@@ -457,14 +464,12 @@ class DefaultController extends ControllerAbstract {
throw new InvalidArgumentException(self::ERR_INVALID_TARGETS);
}
// 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->manager->collectionExtant($tenantId, $userId, $sources);
}
@@ -611,8 +616,6 @@ class DefaultController extends ControllerAbstract {
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;
@@ -637,8 +640,6 @@ class DefaultController extends ControllerAbstract {
}
}
$sources = $this->createSourceSelectorFromIdentifiers($sources);
$filter = $data['filter'] ?? null;
$sort = $data['sort'] ?? null;
$range = $data['range'] ?? null;
@@ -696,31 +697,39 @@ class DefaultController extends ControllerAbstract {
}
private function entityExtant(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']);
return $this->manager->entityExtant($tenantId, $userId, $sources);
$targets = ResourceIdentifiers::fromArray($data['targets']);
foreach ($targets as $target) {
if (!$target instanceof CollectionIdentifier && !$target instanceof EntityIdentifier) {
throw new InvalidArgumentException('Invalid parameter: targets must contain provider:service:collection or provider:service:collection:entity identifiers');
}
}
return $this->manager->entityExtant($tenantId, $userId, $targets);
}
private function entityDelta(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']);
return $this->manager->entityDelta($tenantId, $userId, $sources);
$targets = ResourceIdentifiers::fromArray($data['targets']);
foreach ($targets as $target) {
if (!$target instanceof CollectionIdentifier && !$target instanceof EntityIdentifier) {
throw new InvalidArgumentException('Invalid parameter: targets must contain provider:service:collection or provider:service:collection:signature identifiers');
}
}
return $this->manager->entityDelta($tenantId, $userId, $targets);
}
private function entityPatch(string $tenantId, string $userId, array $data): mixed {
@@ -891,47 +900,4 @@ 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;
}
}