generated from Nodarx/template
refactor: use new mail interface desing
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -12,8 +12,7 @@ namespace KTXM\ProviderImap\Providers;
|
||||
use Generator;
|
||||
use KTXF\Mail\Collection\CollectionBaseInterface;
|
||||
use KTXF\Mail\Collection\CollectionMutableInterface;
|
||||
use KTXF\Mail\Entity\EntityBaseInterface;
|
||||
use KTXF\Mail\Entity\EntityMutableInterface;
|
||||
use KTXF\Mail\Collection\CollectionPropertiesBaseInterface;
|
||||
use KTXF\Mail\Object\Address;
|
||||
use KTXF\Mail\Object\AddressInterface;
|
||||
use KTXF\Mail\Service\ServiceBaseInterface;
|
||||
@@ -41,6 +40,7 @@ use KTXM\ProviderImap\Service\Remote\RemoteService;
|
||||
use KTXM\ProviderImap\Providers\CollectionResource;
|
||||
use KTXF\Mail\Collection\CollectionRoles;
|
||||
use KTXF\Mail\Object\MessagePropertiesMutableInterface;
|
||||
use KTXF\Mail\Service\ServiceEntityMutableInterface;
|
||||
use KTXM\ProviderImap\Providers\EntityResource;
|
||||
|
||||
/**
|
||||
@@ -48,8 +48,6 @@ use KTXM\ProviderImap\Providers\EntityResource;
|
||||
*/
|
||||
class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceConfigurableInterface, ServiceCollectionMutableInterface, ServiceEntityMutableInterface
|
||||
{
|
||||
public const JSON_TYPE = ServiceBaseInterface::JSON_TYPE;
|
||||
|
||||
private const PROVIDER_IDENTIFIER = 'imap';
|
||||
|
||||
private ?string $serviceTenantId = null;
|
||||
@@ -92,6 +90,11 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
self::CAPABILITY_ENTITY_LIST_RANGE => ['tally' => ['absolute', 'relative']],
|
||||
self::CAPABILITY_ENTITY_EXTANT => true,
|
||||
self::CAPABILITY_ENTITY_FETCH => true,
|
||||
self::CAPABILITY_ENTITY_CREATE => false,
|
||||
self::CAPABILITY_ENTITY_MODIFY => false,
|
||||
self::CAPABILITY_ENTITY_DELETE => true,
|
||||
self::CAPABILITY_ENTITY_MOVE => true,
|
||||
self::CAPABILITY_ENTITY_COPY => false,
|
||||
];
|
||||
|
||||
private RemoteMailService $mailService;
|
||||
@@ -359,7 +362,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
$this->initialize();
|
||||
|
||||
$list = [];
|
||||
|
||||
|
||||
foreach ($this->mailService->collectionList($location, $filter, $sort) as $mailbox) {
|
||||
$resource = $this->collectionFresh();
|
||||
$resource->fromImap($mailbox);
|
||||
@@ -383,12 +386,16 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
{
|
||||
$this->initialize();
|
||||
|
||||
$mailboxes = $this->collectionList(null);
|
||||
$extant = [];
|
||||
foreach ($identifiers as $id) {
|
||||
$extant[(string) $id] = isset($mailboxes[(string) $id]);
|
||||
$list = [];
|
||||
|
||||
foreach ($identifiers as $identifier) {
|
||||
$key = (string) $identifier;
|
||||
$result = $this->mailService->collectionFetch($key);
|
||||
|
||||
$list[$key] = $result !== false;
|
||||
}
|
||||
return $extant;
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function collectionFetch(string|int $identifier): ?CollectionBaseInterface
|
||||
@@ -411,17 +418,23 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
return new CollectionResource($this->provider(), $this->identifier());
|
||||
}
|
||||
|
||||
public function collectionCreate(string|int|null $location, CollectionMutableInterface $collection, array $options = []): CollectionBaseInterface
|
||||
public function collectionCreate(CollectionIdentifier|null $target, CollectionPropertiesBaseInterface $properties, array $options = []): CollectionBaseInterface
|
||||
{
|
||||
$this->initialize();
|
||||
|
||||
if (!$properties->getLabel()) {
|
||||
throw new \InvalidArgumentException('Collection label is required property');
|
||||
}
|
||||
$label = $properties->getLabel();
|
||||
|
||||
// Resolve the full name: if a parent location is given, prepend it
|
||||
$label = $collection->getProperties()->getLabel() ?? '';
|
||||
if ($location !== null && $location !== '') {
|
||||
if ($target !== null) {
|
||||
$path = $target->collection();
|
||||
// Determine the hierarchy delimiter from an existing mailbox, default to '/'
|
||||
$mailboxes = iterator_to_array($this->mailService->collectionList(null, null, null, ''));
|
||||
$delimiter = $mailboxes ? reset($mailboxes)->delimiter() ?? '/' : '/';
|
||||
$label = rtrim((string) $location, $delimiter) . $delimiter . ltrim($label, $delimiter);
|
||||
$rootMailbox = $mailboxes === [] ? null : reset($mailboxes);
|
||||
$delimiter = $rootMailbox === false ? '/' : ($rootMailbox?->delimiter() ?? '/');
|
||||
$label = rtrim((string) $path, $delimiter) . $delimiter . ltrim($label, $delimiter);
|
||||
}
|
||||
|
||||
$mailbox = $this->mailService->collectionCreate($label);
|
||||
@@ -432,20 +445,26 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function collectionUpdate(string|int $identifier, CollectionMutableInterface $collection): CollectionBaseInterface
|
||||
public function collectionUpdate(CollectionIdentifier $target, CollectionPropertiesBaseInterface $properties): CollectionBaseInterface
|
||||
{
|
||||
$this->initialize();
|
||||
|
||||
if (!$properties->getLabel()) {
|
||||
throw new \InvalidArgumentException('Collection label is a required property');
|
||||
}
|
||||
$label = $properties->getLabel();
|
||||
|
||||
// In IMAP, "update" = rename to the new label
|
||||
$newName = $collection->getProperties()->getLabel() ?? (string) $identifier;
|
||||
$mailbox = $this->mailService->collectionRename((string) $identifier, $newName);
|
||||
$oldPath = (string) $target->collection();
|
||||
$newName = $properties->getLabel();
|
||||
$mailbox = $this->mailService->collectionRename($oldPath, $newName);
|
||||
|
||||
$collection = $this->collectionFresh();
|
||||
$collection->fromImap($mailbox);
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function collectionDelete(string|int $identifier, bool $force = false): CollectionBaseInterface | true
|
||||
public function collectionDelete(CollectionIdentifier $target, bool $force = false): CollectionBaseInterface | true
|
||||
{
|
||||
$this->initialize();
|
||||
|
||||
@@ -458,7 +477,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
|
||||
// Move to target collection (e.g. Trash) instead of deleting
|
||||
if ($deleteMode === 'soft' && $deleteTarget !== null) {
|
||||
return $this->collectionMove((string) $identifier, (string) $deleteTarget);
|
||||
return $this->collectionMove($target, new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget));
|
||||
}
|
||||
|
||||
if ($deleteMode === 'soft' && $deleteTarget === null) {
|
||||
@@ -474,24 +493,24 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
}
|
||||
|
||||
// we need to determine if the folder being deleted is already in the trash
|
||||
if (str_starts_with((string) $identifier, (string) $deleteTarget)) {
|
||||
if (str_starts_with((string) $target->collection(), (string) $deleteTarget)) {
|
||||
// if so, we should hard delete instead of moving to avoid duplicates in the trash
|
||||
$deleteMode = 'hard';
|
||||
}
|
||||
|
||||
$result = match ($deleteMode) {
|
||||
'soft' => $this->collectionMove((string) $identifier, (string) $deleteTarget),
|
||||
'hard' => $this->mailService->collectionDestroy((string) $identifier)
|
||||
'soft' => $this->collectionMove($target, new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget)),
|
||||
'hard' => $this->mailService->collectionDestroy((string) $target->collection()),
|
||||
};
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function collectionMove(string|int $identifier, string|int|null $target): CollectionBaseInterface
|
||||
public function collectionMove(CollectionIdentifier $target, CollectionIdentifier $source): CollectionBaseInterface
|
||||
{
|
||||
$this->initialize();
|
||||
|
||||
$sourceMailbox = $this->mailService->collectionFetch((string) $identifier);
|
||||
$targetMailbox = $this->mailService->collectionFetch((string) $target);
|
||||
$sourceMailbox = $this->mailService->collectionFetch((string) $source->collection());
|
||||
$targetMailbox = $this->mailService->collectionFetch((string) $target->collection());
|
||||
if ($sourceMailbox === null) {
|
||||
throw new \RuntimeException('Source collection not found for move operation');
|
||||
}
|
||||
@@ -514,7 +533,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
|
||||
public function entityList(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): array
|
||||
{
|
||||
return itterator_to_array($this->entityList((string) $collection, $filter, $sort, $range), true);
|
||||
return iterator_to_array($this->entityList((string) $collection, $filter, $sort, $range), true);
|
||||
}
|
||||
|
||||
public function entityListStream(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): Generator
|
||||
@@ -579,12 +598,12 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
|
||||
public function entityCreate(CollectionIdentifier $target, MessagePropertiesMutableInterface $properties, array $options = []): EntityResource
|
||||
{
|
||||
return $this->entityFresh();
|
||||
throw new \RuntimeException('Entity creation is not supported in this service');
|
||||
}
|
||||
|
||||
public function entityModify(EntityIdentifier $target, MessagePropertiesMutableInterface $properties): EntityResource
|
||||
{
|
||||
return $this->entityFresh();
|
||||
throw new \RuntimeException('Entity modification is not supported in this service');
|
||||
}
|
||||
|
||||
public function entityDelete(EntityIdentifier ...$targets): array
|
||||
@@ -613,7 +632,12 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
throw new \RuntimeException('No Trash collection configured or found for deletion');
|
||||
}
|
||||
|
||||
$deleteTargetNative = reset($mailboxes)->name();
|
||||
$rootMailbox = reset($mailboxes);
|
||||
if ($rootMailbox === false) {
|
||||
throw new \RuntimeException('No Trash collection configured or found for deletion');
|
||||
}
|
||||
|
||||
$deleteTargetNative = $rootMailbox->name();
|
||||
$deleteTargetIdentifier = new CollectionIdentifier($this->provider(), (string) $this->identifier(), $deleteTargetNative);
|
||||
} else {
|
||||
$deleteTargetNative = $deleteTarget;
|
||||
@@ -636,7 +660,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
|
||||
foreach ($uids as $uid) {
|
||||
$mutatedUid = $mutations[$uid] ?? null;
|
||||
$results[(string)$sourceEntities[$uid]] = [
|
||||
$list[(string)$sourceEntities[$uid]] = [
|
||||
'disposition' => $deleteMode === 'soft' ? 'moved' : 'deleted',
|
||||
'destination' => $deleteMode === 'soft' ? $deleteTargetIdentifier : null,
|
||||
'mutation' => $mutatedUid !== null ? new EntityIdentifier($this->provider(), $this->identifier(), $deleteTargetIdentifier->collection(), $mutatedUid) : null,
|
||||
@@ -644,7 +668,12 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function entityPatch(MessagePropertiesMutableInterface $properties, EntityIdentifier ...$targets): array
|
||||
{
|
||||
throw new \RuntimeException('Entity patching is not supported in this service');
|
||||
}
|
||||
|
||||
public function entityPatch(MessagePropertiesMutableInterface $properties, EntityIdentifier ...$targets): array
|
||||
@@ -736,6 +765,11 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function entityCopy(CollectionIdentifier $target, EntityIdentifier ...$sources): array
|
||||
{
|
||||
throw new \RuntimeException('Entity copying is not supported in this service');
|
||||
}
|
||||
|
||||
private function groupEntitiesByCollection(EntityIdentifier ...$identifiers): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
Reference in New Issue
Block a user