feat: use resource identifier

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-09-05 20:10:08 -04:00
parent 41de70da75
commit 26ed0c8aad
2 changed files with 24 additions and 17 deletions
+18 -12
View File
@@ -25,6 +25,7 @@ use KTXF\Resource\Filter\IFilter;
use KTXF\Preview\PreviewSource;
use KTXF\Resource\BinaryResource;
use KTXF\Resource\Identifier\CollectionIdentifier;
use KTXF\Resource\Identifier\ServiceIdentifier;
use KTXF\Resource\Identifier\EntityIdentifier;
use KTXF\Resource\Identifier\ResourceIdentifiers;
use KTXF\Resource\Provider\ResourceServiceIdentityInterface;
@@ -507,18 +508,16 @@ class Manager {
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $provider provider identifier
* @param string|int $service service identifier
* @param CollectionIdentifier|null $target target parent collection identifier (null for root)
* @param ServiceIdentifier|CollectionIdentifier $target service root or parent collection
* @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 $provider, string|int $service, CollectionIdentifier|null $target, CollectionPropertiesMutableInterface|array $properties, array $options = []): CollectionBaseInterface {
public function collectionCreate(string $tenantId, string $userId, ServiceIdentifier|CollectionIdentifier $target, CollectionPropertiesMutableInterface|array $properties, array $options = []): CollectionBaseInterface {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $provider, $service);
$service = $this->serviceFetch($tenantId, $userId, $target->provider(), $target->service());
// Check if service supports collection creation
if ($service->getEnabled() === false) {
throw new InvalidArgumentException("Service '{$service->identifier()}' not found or is disabled");
@@ -534,7 +533,7 @@ class Manager {
$properties = $service->collectionFresh()->getProperties()->jsonDeserialize($properties);
}
// Create collection
return $service->collectionCreate($target, $properties, $options);
return $service->collectionCreate($target instanceof CollectionIdentifier ? $target : null, $properties, $options);
}
/**
@@ -812,21 +811,28 @@ class Manager {
* @return array<string,EntityBaseInterface>
*/
public function entityFetchBulk(string $tenantId, ?string $userId, EntityIdentifier ...$identifiers): array {
// group identifiers by provider/service
// Keep locations separate: providers may return maps keyed by local ID.
$groupedIdentifiers = [];
foreach ($identifiers as $identifier) {
$groupedIdentifiers[$identifier->provider()][$identifier->service()][] = $identifier;
$groupedIdentifiers[$identifier->provider()][$identifier->service()][$identifier->collection()][] = $identifier;
}
// retrieve each service and fetch entities
$list = [];
foreach ($groupedIdentifiers as $providerId => $services) {
foreach ($services as $serviceId => $entities) {
foreach ($services as $serviceId => $collections) {
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
if ($service->getEnabled() === false) {
throw new InvalidArgumentException("Service '{$providerId}:{$serviceId}' not found or is disabled");
}
// retrieve entities and merge into list
$list = array_merge($list, $service->entityFetchBulk(...$entities));
foreach ($collections as $entities) {
$fetched = $service->entityFetchBulk(...$entities);
foreach ($entities as $identifier) {
$entity = $fetched[(string) $identifier] ?? $fetched[$identifier->entity()] ?? null;
if ($entity !== null) {
$list[(string) $identifier] = $entity;
}
}
}
}
}
return $list;
@@ -1354,7 +1360,7 @@ class Manager {
*/
public function entityPreview(string $tenantId, string $userId, EntityIdentifier $target, string $variant = 'inline'): ?Preview {
$entities = $this->entityFetchBulk($tenantId, $userId, $target);
$entity = $entities[$target->entity()] ?? reset($entities);
$entity = $entities[(string) $target] ?? null;
if (!$entity instanceof EntityBaseInterface) {
return null;
}