generated from Nodarx/template
116 lines
3.5 KiB
PHP
116 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace KTXM\ProviderImapMail\Service\Cache;
|
|
|
|
use KTXM\ProviderImapMail\Providers\CollectionResource;
|
|
use KTXM\ProviderImapMail\Providers\EntityResource;
|
|
use KTXM\ProviderImapMail\Stores\MessageStore;
|
|
|
|
/**
|
|
* Cache Mail Service
|
|
*
|
|
* Provides read-only access to locally cached IMAP data stored in MongoDB by
|
|
* CacheService. Call CacheService::syncMailboxes() / syncMessages() to keep
|
|
* the cache up to date before reading from here.
|
|
*/
|
|
class CacheMailService
|
|
{
|
|
public function __construct(
|
|
private readonly MessageStore $messageStore,
|
|
private readonly string $provider,
|
|
private readonly string|int $service,
|
|
) {}
|
|
|
|
// ── Collection (mailbox) reads ────────────────────────────────────────────
|
|
|
|
/**
|
|
* Return all cached mailboxes for this service.
|
|
*
|
|
* @return CollectionResource[] keyed by mailbox name
|
|
*/
|
|
public function collectionList(): array
|
|
{
|
|
$docs = $this->messageStore->listMailboxes((string) $this->service);
|
|
$result = [];
|
|
foreach ($docs as $doc) {
|
|
$resource = new CollectionResource($this->provider, $this->service);
|
|
$resource->fromStore($doc);
|
|
$result[$resource->identifier()] = $resource;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Fetch a single cached mailbox by name.
|
|
*/
|
|
public function collectionFetch(string $name): ?CollectionResource
|
|
{
|
|
$doc = $this->messageStore->fetchMailbox((string) $this->service, $name);
|
|
if ($doc === null) {
|
|
return null;
|
|
}
|
|
$resource = new CollectionResource($this->provider, $this->service);
|
|
$resource->fromStore($doc);
|
|
return $resource;
|
|
}
|
|
|
|
// ── Entity (message) reads ────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Return all cached UIDs for a mailbox.
|
|
*
|
|
* @return int[]
|
|
*/
|
|
public function entityList(string $collection): array
|
|
{
|
|
return $this->messageStore->listUids((string) $this->service, $collection);
|
|
}
|
|
|
|
/**
|
|
* Fetch one or more cached messages by UID.
|
|
*
|
|
* @param int ...$uids
|
|
* @return EntityResource[] keyed by UID
|
|
*/
|
|
public function entityFetch(string $collection, int ...$uids): array
|
|
{
|
|
if (empty($uids)) {
|
|
return [];
|
|
}
|
|
|
|
$docs = $this->messageStore->fetchMessages(
|
|
(string) $this->service,
|
|
$collection,
|
|
array_values($uids),
|
|
);
|
|
$result = [];
|
|
foreach ($docs as $uid => $doc) {
|
|
$resource = new EntityResource($this->provider, $this->service);
|
|
$resource->fromStore($doc);
|
|
$result[$uid] = $resource;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Fetch a single cached message.
|
|
*/
|
|
public function entityFetchOne(string $collection, int $uid): ?EntityResource
|
|
{
|
|
$doc = $this->messageStore->fetchMessage((string) $this->service, $collection, $uid);
|
|
if ($doc === null) {
|
|
return null;
|
|
}
|
|
$resource = new EntityResource($this->provider, $this->service);
|
|
$resource->fromStore($doc);
|
|
return $resource;
|
|
}
|
|
}
|