generated from Nodarx/template
852ead324f
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
79 lines
2.8 KiB
PHP
79 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXT\ProviderImap\Tests\Support;
|
|
|
|
use KTXM\ProviderImap\Providers\EntityResource;
|
|
use KTXM\ProviderImap\Stores\MessageStore;
|
|
|
|
/**
|
|
* In-memory MessageStore modelling addedSeq / modSeq / tombstones like the Mongo queries.
|
|
*/
|
|
final class FakeMessageStore extends MessageStore
|
|
{
|
|
/** @var array<int, array<int, array{flags: list<string>, addedSeq: int, modSeq: int, removedSeq: ?int}>> by UIDVALIDITY and UID */
|
|
private array $documents = [];
|
|
|
|
public function upsert(string $tenantId, string $serviceId, int $uidValidity, EntityResource $entity, int $seq): void
|
|
{
|
|
$meta = $entity->toCacheMeta();
|
|
$existing = $this->documents[$uidValidity][$meta['uid']] ?? null;
|
|
$this->documents[$uidValidity][$meta['uid']] = [
|
|
'flags' => $meta['flags'],
|
|
'addedSeq' => $existing['addedSeq'] ?? $seq,
|
|
'modSeq' => $seq,
|
|
'removedSeq' => null,
|
|
];
|
|
ksort($this->documents[$uidValidity]);
|
|
}
|
|
|
|
public function flags(string $serviceId, string $mailbox, int $uidValidity): array
|
|
{
|
|
$flags = [];
|
|
foreach ($this->documents[$uidValidity] ?? [] as $uid => $document) {
|
|
if ($document['removedSeq'] === null) {
|
|
$flags[$uid] = $document['flags'];
|
|
}
|
|
}
|
|
return $flags;
|
|
}
|
|
|
|
public function updateFlags(string $serviceId, string $mailbox, int $uidValidity, int $uid, array $flags, int $seq): void
|
|
{
|
|
$this->documents[$uidValidity][$uid]['flags'] = array_values($flags);
|
|
$this->documents[$uidValidity][$uid]['modSeq'] = $seq;
|
|
}
|
|
|
|
public function tombstone(string $serviceId, string $mailbox, int $uidValidity, int $seq, int ...$uids): void
|
|
{
|
|
foreach ($uids as $uid) {
|
|
$this->documents[$uidValidity][$uid]['removedSeq'] = $seq;
|
|
$this->documents[$uidValidity][$uid]['modSeq'] = $seq;
|
|
$this->documents[$uidValidity][$uid]['flags'] = [];
|
|
}
|
|
}
|
|
|
|
public function changes(string $serviceId, string $mailbox, int $uidValidity, int $since): array
|
|
{
|
|
$changes = ['additions' => [], 'modifications' => [], 'deletions' => []];
|
|
foreach ($this->documents[$uidValidity] ?? [] as $uid => $document) {
|
|
if ($document['removedSeq'] !== null) {
|
|
if ($document['addedSeq'] <= $since && $document['removedSeq'] > $since) {
|
|
$changes['deletions'][] = $uid;
|
|
}
|
|
} elseif ($document['addedSeq'] > $since) {
|
|
$changes['additions'][] = $uid;
|
|
} elseif ($document['modSeq'] > $since) {
|
|
$changes['modifications'][] = $uid;
|
|
}
|
|
}
|
|
return $changes;
|
|
}
|
|
|
|
public function deleteByMailbox(string $serviceId, string $mailbox): void
|
|
{
|
|
$this->documents = [];
|
|
}
|
|
}
|