Skip to content

Commit

Permalink
feat: mail provider backend
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>
  • Loading branch information
SebastianKrupinski committed Aug 27, 2024
1 parent 1776775 commit d1f506e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 58 deletions.
2 changes: 1 addition & 1 deletion lib/Provider/Command/MessageSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
}

/**
* performs send operation
* Performs send operation
*
* @since 4.0.0
*
Expand Down
33 changes: 18 additions & 15 deletions lib/Provider/MailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@
use OCP\Mail\Provider\IProvider;
use OCP\Mail\Provider\IService;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class MailProvider implements IProvider {

private ?array $ServiceCollection = [];

public function __construct(
protected ContainerInterface $container,
protected AccountService $accountService
protected AccountService $accountService,
protected LoggerInterface $logger
) {
}

/**
* arbitrary unique text string identifying this provider
* Arbitrary unique text string identifying this provider
*
* @since 4.0.0
*
Expand All @@ -37,7 +39,7 @@ public function id(): string {
}

/**
* localized human friendly name of this provider
* Localized human friendly name of this provider
*
* @since 4.0.0
*
Expand All @@ -48,7 +50,7 @@ public function label(): string {
}

/**
* determine if any services are configured for a specific user
* Determine if any services are configured for a specific user
*
* @since 4.0.0
*
Expand All @@ -61,7 +63,7 @@ public function hasServices(string $userId): bool {
}

/**
* retrieve collection of services for a specific user
* Retrieve collection of services for a specific user
*
* @since 4.0.0
*
Expand All @@ -74,23 +76,24 @@ public function listServices(string $userId): array {
try {
// retrieve service(s) details from data store
$accounts = $this->accountService->findByUserId($userId);
} catch (\Throwable $th) {
} catch (\Throwable $e) {
$this->logger->error('Error occurred while retrieving mail account details', [ 'exception' => $e ]);
return [];
}
// construct temporary collection
$services = [];
// add services to collection
foreach ($accounts as $entry) {
// add service to collection
$services[$serviceId] = $this->serviceFromAccount($userId, $entry);
$services[(string)$entry->getId()] = $this->serviceFromAccount($userId, $entry);

Check failure on line 88 in lib/Provider/MailProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidArgument

lib/Provider/MailProvider.php:88:76: InvalidArgument: Argument 2 of OCA\Mail\Provider\MailProvider::serviceFromAccount expects string, but OCA\Mail\Account provided (see https://psalm.dev/004)
}
// return list of services for user
return $services;

}

/**
* retrieve a service with a specific id
* Retrieve a service with a specific id
*
* @since 4.0.0
*
Expand All @@ -110,7 +113,8 @@ public function findServiceById(string $userId, string $serviceId): IService|nul
try {
// retrieve service details from data store
$account = $this->accountService->find($userId, (int)$serviceId);
} catch(\Throwable $th) {
} catch(\Throwable $e) {
$this->logger->error('Error occurred while retrieving mail account details', [ 'exception' => $e ]);
return null;
}

Expand All @@ -120,7 +124,7 @@ public function findServiceById(string $userId, string $serviceId): IService|nul
}

/**
* retrieve a service for a specific mail address
* Retrieve a service for a specific mail address
*
* @since 4.0.0
*
Expand All @@ -134,7 +138,8 @@ public function findServiceByAddress(string $userId, string $address): IService|
try {
// retrieve service details from data store
$accounts = $this->accountService->findByUserIdAndAddress($userId, $address);
} catch(\Throwable $th) {
} catch(\Throwable $e) {
$this->logger->error('Error occurred while retrieving mail account details', [ 'exception' => $e ]);
return null;
}
// evaluate if service details where found
Expand All @@ -148,20 +153,18 @@ public function findServiceByAddress(string $userId, string $address): IService|
}

/**
* construct a new fresh service object
* Construct a new fresh service object
*
* @since 4.0.0
*
* @return IService fresh service object
*/
public function initiateService(): IService {

return new MailService($this->container);

}

/**
* construct a service object from a mail account
* Construct a service object from a mail account
*
* @since 4.0.0
*
Expand Down
52 changes: 16 additions & 36 deletions lib/Provider/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/
namespace OCA\Mail\Provider;

use OCA\Mail\Provider\Command\MessageSend;
use OCP\Mail\Provider\Address;
use OCP\Mail\Provider\Exception\SendException;
use OCP\Mail\Provider\IAddress;
use OCP\Mail\Provider\IMessage;
use OCP\Mail\Provider\IMessageSend;
Expand All @@ -33,20 +35,18 @@ public function __construct(
}

/**
* arbitrary unique text string identifying this service
* Arbitrary unique text string identifying this service
*
* @since 4.0.0
*
* @return string id of this service (e.g. 1 or service1 or anything else)
*/
public function id(): string {

return $this->serviceId;

}

/**
* checks if a service is able of performing an specific action
* Checks if a service is able of performing an specific action
*
* @since 4.0.0
*
Expand All @@ -55,44 +55,38 @@ public function id(): string {
* @return bool true/false if ability is supplied and found in collection
*/
public function capable(string $value): bool {

// evaluate if required ability exists
if (isset($this->serviceAbilities[$value])) {
return (bool)$this->serviceAbilities[$value];
}

return false;

}

/**
* retrieves a collection of what actions a service can perform
* Retrieves a collection of what actions a service can perform
*
* @since 4.0.0
*
* @return array collection of abilities otherwise empty collection
*/
public function capabilities(): array {

return $this->serviceAbilities;

}

/**
* gets the localized human friendly name of this service
* Gets the localized human friendly name of this service
*
* @since 4.0.0
*
* @return string label/name of service (e.g. ACME Company Mail Service)
*/
public function getLabel(): string {

return $this->serviceLabel;

}

/**
* sets the localized human frendly name of this service
* Sets the localized human friendly name of this service
*
* @since 4.0.0
*
Expand All @@ -101,28 +95,24 @@ public function getLabel(): string {
* @return self return this object for command chaining
*/
public function setLabel(string $value): self {

$this->serviceLabel = $value;
return $this;

}

/**
* gets the primary mailing address for this service
* Gets the primary mailing address for this service
*
* @since 4.0.0
*
* @return IAddress mail address object
*/
public function getPrimaryAddress(): IAddress {

// retrieve and return primary service address
return $this->servicePrimaryAddress;

}

/**
* sets the primary mailing address for this service
* Sets the primary mailing address for this service
*
* @since 4.0.0
*
Expand All @@ -131,28 +121,24 @@ public function getPrimaryAddress(): IAddress {
* @return self return this object for command chaining
*/
public function setPrimaryAddress(IAddress $value): self {

$this->servicePrimaryAddress = $value;
return $this;

}

/**
* gets the secondary mailing addresses (aliases) collection for this service
* Gets the secondary mailing addresses (aliases) collection for this service
*
* @since 4.0.0
*
* @return array<int, IAddress> collection of mail address object [IAddress, IAddress]
*/
public function getSecondaryAddresses(): array {

// retrieve and return secondary service addresses (aliases) collection
return $this->serviceSecondaryAddresses;

}

/**
* sets the secondary mailing addresses (aliases) for this service
* Sets the secondary mailing addresses (aliases) for this service
*
* @since 4.0.0
*
Expand All @@ -161,42 +147,36 @@ public function getSecondaryAddresses(): array {
* @return self return this object for command chaining
*/
public function setSecondaryAddresses(IAddress ...$value): self {

$this->serviceSecondaryAddresses = $value;
return $this;

}

/**
* construct a new fresh message object
* Construct a new fresh message object
*
* @since 30.0.0
*
* @return IMessage fresh message object
*/
public function initiateMessage(): IMessage {

return new Message();

}

/**
* sends an outbound message
* Sends an outbound message
*
* @since 4.0.0
*
* @param IMessage $message mail message object with all required parameters to send a message
* @param array $options array of options reserved for future use
*
* @throws \OCP\Mail\Provider\Exception\SendException on failure, check message for reason
* @throws SendException on failure, check message for reason
*/
public function sendMessage(IMessage $message, array $option = []): void {

/** @var \OCA\Mail\Provider\Command\MessageSend $cmd */
$cmd = $this->container->get(\OCA\Mail\Provider\Command\MessageSend::class);
/** @var MessageSend $cmd */
$cmd = $this->container->get(MessageSend::class);
// perform action
$cmd->perform($this->userId, $this->serviceId, $message, $option);

}

}
Loading

0 comments on commit d1f506e

Please sign in to comment.