generated from Nodarx/template
refactor: split service into ServiceBase and LiveService
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,404 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderImap\Providers;
|
||||
|
||||
use Generator;
|
||||
use KTXF\Mail\Collection\CollectionBaseInterface;
|
||||
use KTXF\Mail\Collection\CollectionPropertiesBaseInterface;
|
||||
use KTXF\Mail\Object\Address;
|
||||
use KTXF\Mail\Object\AddressInterface;
|
||||
use KTXF\Mail\Service\ServiceBaseInterface;
|
||||
use KTXF\Mail\Service\ServiceCollectionMutableInterface;
|
||||
use KTXF\Mail\Service\ServiceEntityMutableInterface;
|
||||
use KTXF\Mail\Service\ServiceEntitySubmitInterface;
|
||||
use KTXF\Mail\Service\ServiceConfigurableInterface;
|
||||
use KTXF\Mail\Service\ServiceMutableInterface;
|
||||
use KTXF\Mail\Submission\EntitySubmitResult;
|
||||
use KTXF\Resource\BinaryResource;
|
||||
use KTXF\Resource\Provider\ResourceServiceIdentityInterface;
|
||||
use KTXF\Resource\Provider\ResourceServiceLocationInterface;
|
||||
use KTXF\Resource\Delta\Delta;
|
||||
use KTXF\Resource\Filter\Filter;
|
||||
use KTXF\Resource\Filter\IFilter;
|
||||
use KTXF\Resource\Identifier\CollectionIdentifier;
|
||||
use KTXF\Resource\Identifier\EntityIdentifier;
|
||||
use KTXF\Resource\Range\IRange;
|
||||
use KTXF\Resource\Range\Range;
|
||||
use KTXF\Resource\Range\RangeTally;
|
||||
use KTXF\Resource\Range\RangeType;
|
||||
use KTXF\Resource\Sort\ISort;
|
||||
use KTXF\Resource\Sort\Sort;
|
||||
use KTXM\ProviderImap\Providers\ServiceIdentityBasic;
|
||||
use KTXM\ProviderImap\Providers\ServiceLocation;
|
||||
use KTXM\ProviderImap\Mime\MessageBuilder;
|
||||
use KTXM\ProviderImap\Providers\CollectionResource;
|
||||
use KTXF\Mail\Collection\CollectionRoles;
|
||||
use KTXF\Mail\Object\MessagePropertiesMutableInterface;
|
||||
use KTXF\Resource\Identifier\EntityIdentifierInterface;
|
||||
use KTXM\ProviderImap\Providers\EntityResource;
|
||||
|
||||
/**
|
||||
* IMAP mail service: account configuration and serialization.
|
||||
*
|
||||
* The mail API (collection / entity methods, declared by the implemented
|
||||
* interfaces) is left to the concrete classes: LiveService talks to the IMAP
|
||||
* server, CachedService serves reads from the cache. The Provider picks the
|
||||
* class by mode when loading a service.
|
||||
*/
|
||||
abstract class ServiceBase implements ServiceBaseInterface, ServiceMutableInterface, ServiceConfigurableInterface, ServiceCollectionMutableInterface, ServiceEntityMutableInterface, ServiceEntitySubmitInterface
|
||||
{
|
||||
protected const PROVIDER_IDENTIFIER = 'imap';
|
||||
|
||||
protected ?string $serviceTenantId = null;
|
||||
protected ?string $serviceUserId = null;
|
||||
protected ?string $serviceIdentifier = null;
|
||||
protected ?string $serviceLabel = null;
|
||||
protected bool $serviceEnabled = false;
|
||||
protected array $primaryAddress = [];
|
||||
protected array $secondaryAddresses = [];
|
||||
protected ?ServiceLocation $location = null;
|
||||
protected ?ServiceIdentityBasic $identity = null;
|
||||
protected array $auxiliary = [];
|
||||
|
||||
protected array $serviceAbilities = [
|
||||
self::CAPABILITY_COLLECTION_LIST => true,
|
||||
self::CAPABILITY_COLLECTION_LIST_FILTER => [
|
||||
self::CAPABILITY_COLLECTION_FILTER_LABEL => 's:128:256:256',
|
||||
self::CAPABILITY_COLLECTION_FILTER_ROLE => 's:32:1:1',
|
||||
self::CAPABILITY_COLLECTION_FILTER_SUBSCRIBED => 'b:0:1:1',
|
||||
],
|
||||
self::CAPABILITY_COLLECTION_LIST_SORT => [
|
||||
self::CAPABILITY_COLLECTION_SORT_LABEL,
|
||||
self::CAPABILITY_COLLECTION_SORT_RANK,
|
||||
],
|
||||
self::CAPABILITY_COLLECTION_FETCH => true,
|
||||
self::CAPABILITY_COLLECTION_EXTANT => true,
|
||||
self::CAPABILITY_COLLECTION_CREATE => true,
|
||||
self::CAPABILITY_COLLECTION_UPDATE => true,
|
||||
self::CAPABILITY_COLLECTION_DELETE => true,
|
||||
self::CAPABILITY_COLLECTION_MOVE => true,
|
||||
self::CAPABILITY_ENTITY_LIST => true,
|
||||
self::CAPABILITY_ENTITY_LIST_FILTER => [
|
||||
self::CAPABILITY_ENTITY_FILTER_FROM => 's:100:256:256',
|
||||
self::CAPABILITY_ENTITY_FILTER_TO => 's:100:256:256',
|
||||
self::CAPABILITY_ENTITY_FILTER_SUBJECT => 's:200:256:256',
|
||||
self::CAPABILITY_ENTITY_FILTER_BODY => 's:200:256:256',
|
||||
self::CAPABILITY_ENTITY_FILTER_DATE_BEFORE => 's:32:1:1',
|
||||
self::CAPABILITY_ENTITY_FILTER_DATE_AFTER => 's:32:1:1',
|
||||
self::CAPABILITY_ENTITY_FILTER_SIZE_MIN => 'i:0:16:16',
|
||||
self::CAPABILITY_ENTITY_FILTER_SIZE_MAX => 'i:0:32:32',
|
||||
],
|
||||
self::CAPABILITY_ENTITY_LIST_SORT => [
|
||||
self::CAPABILITY_ENTITY_SORT_FROM,
|
||||
self::CAPABILITY_ENTITY_SORT_TO,
|
||||
self::CAPABILITY_ENTITY_SORT_SUBJECT,
|
||||
self::CAPABILITY_ENTITY_SORT_DATE_RECEIVED,
|
||||
self::CAPABILITY_ENTITY_SORT_DATE_SENT,
|
||||
self::CAPABILITY_ENTITY_SORT_SIZE,
|
||||
],
|
||||
self::CAPABILITY_ENTITY_LIST_RANGE => [
|
||||
'tally' => ['absolute', 'relative']
|
||||
],
|
||||
self::CAPABILITY_ENTITY_FETCH => true,
|
||||
self::CAPABILITY_ENTITY_EXTANT => true,
|
||||
self::CAPABILITY_ENTITY_CREATE => true,
|
||||
self::CAPABILITY_ENTITY_MODIFY => true,
|
||||
self::CAPABILITY_ENTITY_PATCH => true,
|
||||
self::CAPABILITY_ENTITY_DELETE => true,
|
||||
self::CAPABILITY_ENTITY_MOVE => true,
|
||||
self::CAPABILITY_ENTITY_COPY => false,
|
||||
'EntityTransmit' => true,
|
||||
];
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public function toStore(): array
|
||||
{
|
||||
return array_filter([
|
||||
'tid' => $this->serviceTenantId,
|
||||
'uid' => $this->serviceUserId,
|
||||
'sid' => $this->serviceIdentifier,
|
||||
'enabled' => $this->serviceEnabled,
|
||||
'label' => $this->serviceLabel,
|
||||
'primaryAddress' => $this->primaryAddress,
|
||||
'secondaryAddresses'=> $this->secondaryAddresses,
|
||||
'location' => $this->location?->toStore(),
|
||||
'identity' => $this->identity?->toStore(),
|
||||
'auxiliary' => $this->auxiliary,
|
||||
], fn($v) => $v !== null);
|
||||
}
|
||||
|
||||
public function fromStore(array $data): static
|
||||
{
|
||||
$this->serviceTenantId = $data['tid'] ?? null;
|
||||
$this->serviceUserId = $data['uid'] ?? null;
|
||||
$this->serviceIdentifier = $data['sid'] ?? null;
|
||||
$this->serviceLabel = $data['label'] ?? '';
|
||||
$this->serviceEnabled = $data['enabled'] ?? false;
|
||||
|
||||
if (isset($data['primaryAddress'])) {
|
||||
$this->primaryAddress = $data['primaryAddress'];
|
||||
}
|
||||
if (isset($data['secondaryAddresses']) && is_array($data['secondaryAddresses'])) {
|
||||
$this->secondaryAddresses = $data['secondaryAddresses'];
|
||||
}
|
||||
if (isset($data['location'])) {
|
||||
$this->location = (new ServiceLocation())->fromStore($data['location']);
|
||||
}
|
||||
if (isset($data['identity'])) {
|
||||
$this->identity = (new ServiceIdentityBasic())->fromStore($data['identity']);
|
||||
}
|
||||
if (isset($data['auxiliary']) && is_array($data['auxiliary'])) {
|
||||
$this->auxiliary = $data['auxiliary'];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return array_filter([
|
||||
self::PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::PROPERTY_PROVIDER => self::PROVIDER_IDENTIFIER,
|
||||
self::PROPERTY_IDENTIFIER => $this->serviceIdentifier,
|
||||
self::PROPERTY_LABEL => $this->serviceLabel,
|
||||
self::PROPERTY_ENABLED => $this->serviceEnabled,
|
||||
self::PROPERTY_CAPABILITIES => $this->serviceAbilities,
|
||||
self::PROPERTY_PRIMARY_ADDRESS => $this->primaryAddress,
|
||||
self::PROPERTY_SECONDARY_ADDRESSES => $this->secondaryAddresses,
|
||||
self::PROPERTY_LOCATION => $this->location?->jsonSerialize(),
|
||||
self::PROPERTY_IDENTITY => $this->identity?->jsonSerialize(),
|
||||
self::PROPERTY_AUXILIARY => $this->auxiliary,
|
||||
], fn($v) => $v !== null);
|
||||
}
|
||||
|
||||
public function jsonDeserialize(array|string $data, bool $delta = false): static
|
||||
{
|
||||
if (is_string($data)) {
|
||||
$data = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
if (isset($data[self::PROPERTY_ENABLED])) {
|
||||
$this->setEnabled($data[self::PROPERTY_ENABLED]);
|
||||
}
|
||||
if (isset($data[self::PROPERTY_LABEL])) {
|
||||
$this->setLabel($data[self::PROPERTY_LABEL]);
|
||||
}
|
||||
if (isset($data[self::PROPERTY_LOCATION])) {
|
||||
$this->setLocation($this->freshLocation(null, $data[self::PROPERTY_LOCATION]));
|
||||
}
|
||||
if (isset($data[self::PROPERTY_IDENTITY])) {
|
||||
$this->setIdentity($this->freshIdentity(null, $data[self::PROPERTY_IDENTITY]));
|
||||
}
|
||||
if (isset($data[self::PROPERTY_PRIMARY_ADDRESS])) {
|
||||
$value = $data[self::PROPERTY_PRIMARY_ADDRESS];
|
||||
$this->setPrimaryAddress(is_array($value) ? Address::fromArray($value) : new Address((string)$value));
|
||||
}
|
||||
if (isset($data[self::PROPERTY_SECONDARY_ADDRESSES]) && is_array($data[self::PROPERTY_SECONDARY_ADDRESSES])) {
|
||||
$this->setSecondaryAddresses(array_map(
|
||||
fn($addr) => new Address(is_array($addr) ? ($addr['address'] ?? $addr) : $addr),
|
||||
$data[self::PROPERTY_SECONDARY_ADDRESSES]
|
||||
));
|
||||
}
|
||||
if (isset($data[self::PROPERTY_AUXILIARY]) && is_array($data[self::PROPERTY_AUXILIARY])) {
|
||||
$this->setAuxiliary($data[self::PROPERTY_AUXILIARY]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function capable(string $value): bool
|
||||
{
|
||||
return isset($this->serviceAbilities[$value]);
|
||||
}
|
||||
|
||||
public function capabilities(): array
|
||||
{
|
||||
return $this->serviceAbilities;
|
||||
}
|
||||
|
||||
public function provider(): string
|
||||
{
|
||||
return self::PROVIDER_IDENTIFIER;
|
||||
}
|
||||
|
||||
public function identifier(): string|int
|
||||
{
|
||||
return $this->serviceIdentifier;
|
||||
}
|
||||
|
||||
public function tenantIdentifier(): ?string
|
||||
{
|
||||
return $this->serviceTenantId;
|
||||
}
|
||||
|
||||
public function getLabel(): ?string
|
||||
{
|
||||
return $this->serviceLabel;
|
||||
}
|
||||
|
||||
public function setLabel(string $label): static
|
||||
{
|
||||
$this->serviceLabel = $label;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEnabled(): bool
|
||||
{
|
||||
return $this->serviceEnabled;
|
||||
}
|
||||
|
||||
public function setEnabled(bool $enabled): static
|
||||
{
|
||||
$this->serviceEnabled = $enabled;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPrimaryAddress(): AddressInterface
|
||||
{
|
||||
return Address::fromArray($this->primaryAddress);
|
||||
}
|
||||
|
||||
public function setPrimaryAddress(AddressInterface $value): static
|
||||
{
|
||||
$this->primaryAddress = $value->toArray();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSecondaryAddresses(): array
|
||||
{
|
||||
return array_map(
|
||||
fn($addr) => $addr instanceof AddressInterface ? $addr : Address::fromArray(is_array($addr) ? $addr : ['address' => (string) $addr])
|
||||
, $this->secondaryAddresses);
|
||||
}
|
||||
|
||||
public function setSecondaryAddresses(array $addresses): static
|
||||
{
|
||||
$this->secondaryAddresses = array_map(
|
||||
fn($addr) => $addr instanceof AddressInterface ? $addr : Address::fromArray(is_array($addr) ? $addr : ['address' => (string) $addr]),
|
||||
$addresses
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasAddress(string $address): bool
|
||||
{
|
||||
$address = strtolower(trim($address));
|
||||
|
||||
if ($this->primaryAddress && strtolower($this->primaryAddress['address'] ?? '') === $address) {
|
||||
return true;
|
||||
}
|
||||
foreach ($this->secondaryAddresses as $secondary) {
|
||||
$secondaryAddr = $secondary instanceof AddressInterface ? $secondary->getAddress() : (string) $secondary;
|
||||
if (strtolower($secondaryAddr) === $address) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public function getLocation(): ServiceLocation
|
||||
{
|
||||
return $this->location;
|
||||
}
|
||||
|
||||
public function setLocation(ResourceServiceLocationInterface $location): static
|
||||
{
|
||||
$this->location = $location;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function freshLocation(?string $type = null, array $data = []): ServiceLocation
|
||||
{
|
||||
$loc = new ServiceLocation();
|
||||
$loc->jsonDeserialize($data);
|
||||
return $loc;
|
||||
}
|
||||
|
||||
public function getIdentity(): ServiceIdentityBasic
|
||||
{
|
||||
return $this->identity;
|
||||
}
|
||||
|
||||
public function setIdentity(ResourceServiceIdentityInterface $identity): static
|
||||
{
|
||||
$this->identity = $identity;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function freshIdentity(?string $type = null, array $data = []): ServiceIdentityBasic
|
||||
{
|
||||
$id = new ServiceIdentityBasic();
|
||||
$id->jsonDeserialize($data);
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function getDebug(): bool
|
||||
{
|
||||
return ($this->auxiliary['debug'] ?? false) === true;
|
||||
}
|
||||
|
||||
public function setDebug(bool $debug): static
|
||||
{
|
||||
$this->auxiliary['debug'] = $debug;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAuxiliary(): array
|
||||
{
|
||||
return $this->auxiliary;
|
||||
}
|
||||
|
||||
public function setAuxiliary(array $auxiliary): static
|
||||
{
|
||||
$this->auxiliary = $auxiliary;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function collectionListFilter(): Filter
|
||||
{
|
||||
return new Filter($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_FILTER] ?? []);
|
||||
}
|
||||
|
||||
public function collectionListSort(): Sort
|
||||
{
|
||||
return new Sort($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_SORT] ?? []);
|
||||
}
|
||||
|
||||
public function collectionFresh(): CollectionResource
|
||||
{
|
||||
return new CollectionResource($this->provider(), $this->identifier());
|
||||
}
|
||||
|
||||
public function entityListFilter(): Filter
|
||||
{
|
||||
return new Filter($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_FILTER] ?? []);
|
||||
}
|
||||
|
||||
public function entityListSort(): Sort
|
||||
{
|
||||
return new Sort($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_SORT] ?? []);
|
||||
}
|
||||
|
||||
public function entityListRange(RangeType $type): IRange
|
||||
{
|
||||
return match ($type) {
|
||||
RangeType::TALLY => new RangeTally(),
|
||||
default => new Range(),
|
||||
};
|
||||
}
|
||||
|
||||
public function entityFresh(): EntityResource
|
||||
{
|
||||
return new EntityResource($this->provider(), $this->identifier());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user