feat: entity streaming
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -9,6 +9,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace KTXM\ProviderJmapc\Providers\Mail;
|
||||
|
||||
use Generator;
|
||||
use KTXF\Mail\Collection\CollectionBaseInterface;
|
||||
use KTXF\Mail\Collection\CollectionMutableInterface;
|
||||
use KTXF\Mail\Object\Address;
|
||||
@@ -32,11 +33,6 @@ use KTXM\ProviderJmapc\Providers\ServiceLocation;
|
||||
use KTXM\ProviderJmapc\Service\Remote\RemoteMailService;
|
||||
use KTXM\ProviderJmapc\Service\Remote\RemoteService;
|
||||
|
||||
/**
|
||||
* JMAP Service
|
||||
*
|
||||
* Represents a configured JMAP account
|
||||
*/
|
||||
class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceConfigurableInterface, ServiceCollectionMutableInterface
|
||||
{
|
||||
public const JSON_TYPE = ServiceBaseInterface::JSON_TYPE;
|
||||
@@ -105,16 +101,14 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
public function __construct(
|
||||
) {}
|
||||
|
||||
private function initialize(): void
|
||||
{
|
||||
private function initialize(): void {
|
||||
if (!isset($this->mailService)) {
|
||||
$client = RemoteService::freshClient($this);
|
||||
$this->mailService = RemoteService::mailService($client);
|
||||
}
|
||||
}
|
||||
|
||||
public function toStore(): array
|
||||
{
|
||||
public function toStore(): array {
|
||||
return array_filter([
|
||||
'tid' => $this->serviceTenantId,
|
||||
'uid' => $this->serviceUserId,
|
||||
@@ -130,8 +124,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
], fn($v) => $v !== null);
|
||||
}
|
||||
|
||||
public function fromStore(array $data): static
|
||||
{
|
||||
public function fromStore(array $data): static {
|
||||
$this->serviceTenantId = $data['tid'] ?? null;
|
||||
$this->serviceUserId = $data['uid'] ?? null;
|
||||
$this->serviceIdentifier = $data['sid'];
|
||||
@@ -160,8 +153,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
public function jsonSerialize(): array {
|
||||
return array_filter([
|
||||
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::JSON_PROPERTY_PROVIDER => self::PROVIDER_IDENTIFIER,
|
||||
@@ -177,8 +169,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
], fn($v) => $v !== null);
|
||||
}
|
||||
|
||||
public function jsonDeserialize(array|string $data): static
|
||||
{
|
||||
public function jsonDeserialize(array|string $data): static {
|
||||
if (is_string($data)) {
|
||||
$data = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
@@ -218,14 +209,9 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
return isset($this->serviceAbilities[$value]);
|
||||
}
|
||||
|
||||
public function capabilities(): array
|
||||
{
|
||||
$caps = [];
|
||||
foreach (array_keys($this->serviceAbilities) as $cap) {
|
||||
$caps[$cap] = true;
|
||||
}
|
||||
return $caps;
|
||||
}
|
||||
public function capabilities(): array {
|
||||
return $this->serviceAbilities;
|
||||
}
|
||||
|
||||
public function provider(): string
|
||||
{
|
||||
@@ -486,6 +472,22 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function entityListStream(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): Generator
|
||||
{
|
||||
$this->initialize();
|
||||
|
||||
$result = $this->mailService->entityList($collection, $filter, $sort, $range, $properties);
|
||||
|
||||
foreach ($result['list'] as $index => $entry) {
|
||||
if (is_array($entry) && isset($entry['id'])) {
|
||||
$object = new EntityResource(provider: $this->provider(), service: $this->identifier());
|
||||
$object->fromJmap($entry);
|
||||
yield $object;
|
||||
}
|
||||
unset($result['list'][$index]);
|
||||
}
|
||||
}
|
||||
|
||||
public function entityListFilter(): Filter
|
||||
{
|
||||
return new Filter($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_FILTER] ?? []);
|
||||
|
||||
Reference in New Issue
Block a user