Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\SystemMailClient;
|
||||
|
||||
use Xentral\Core\DependencyInjection\ServiceContainer;
|
||||
|
||||
final class Bootstrap
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function registerServices(): array
|
||||
{
|
||||
return [
|
||||
'MailClientConfigProvider' => 'onInitMailClientConfigProvider',
|
||||
'MailClientProvider' => 'onInitMailClientProvider',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServiceContainer $container
|
||||
*
|
||||
* @return MailClientConfigProvider
|
||||
*/
|
||||
public static function onInitMailClientConfigProvider(ServiceContainer $container): MailClientConfigProvider
|
||||
{
|
||||
return new MailClientConfigProvider(
|
||||
$container->get('EmailAccountGateway'),
|
||||
$container->get('GoogleAccountGateway'),
|
||||
$container->get('GoogleApiClientFactory')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServiceContainer $container
|
||||
*
|
||||
* @return MailClientProvider
|
||||
*/
|
||||
public static function onInitMailClientProvider(ServiceContainer $container): MailClientProvider
|
||||
{
|
||||
return new MailClientProvider(
|
||||
$container->get('MailClientFactory'),
|
||||
$container->get('MailClientConfigProvider'),
|
||||
$container->get('EmailAccountGateway')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\SystemMailClient\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class EmailAccountNotFoundException extends RuntimeException implements SystemMailClientExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\SystemMailClient\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class InvalidArgumentException extends RuntimeException implements SystemMailClientExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\SystemMailClient\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class MailClientConfigException extends RuntimeException implements SystemMailClientExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\SystemMailClient\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class OAuthException extends RuntimeException implements SystemMailClientExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\SystemMailClient\Exception;
|
||||
|
||||
use Xentral\Core\Exception\ModuleExceptionInterface;
|
||||
|
||||
interface SystemMailClientExceptionInterface extends ModuleExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\SystemMailClient;
|
||||
|
||||
use Exception;
|
||||
use Xentral\Components\MailClient\Config\ImapMailClientConfig;
|
||||
use Xentral\Modules\GoogleApi\Client\GoogleApiClientFactory;
|
||||
use Xentral\Modules\GoogleApi\Service\GoogleAccountGateway;
|
||||
use Xentral\Modules\SystemMailClient\Exception\MailClientConfigException;
|
||||
use Xentral\Modules\SystemMailClient\Exception\OAuthException;
|
||||
use Xentral\Modules\SystemMailer\Data\EmailBackupAccount;
|
||||
use Xentral\Modules\SystemMailer\Exception\EmailBackupAccountException;
|
||||
use Xentral\Modules\SystemMailer\Service\EmailAccountGateway;
|
||||
|
||||
class MailClientConfigProvider
|
||||
{
|
||||
/** @var EmailAccountGateway $accountGateway */
|
||||
private $accountGateway;
|
||||
|
||||
/** @var GoogleAccountGateway $googleAccountGateway */
|
||||
private $googleAccountGateway;
|
||||
|
||||
/** @var GoogleApiClientFactory $clientFactory */
|
||||
private $clientFactory;
|
||||
|
||||
/**
|
||||
* @param EmailAccountGateway $accountGateway
|
||||
* @param GoogleAccountGateway $googleAccountGateway
|
||||
* @param GoogleApiClientFactory $clientFactory
|
||||
*/
|
||||
public function __construct(
|
||||
EmailAccountGateway $accountGateway,
|
||||
GoogleAccountGateway $googleAccountGateway,
|
||||
GoogleApiClientFactory $clientFactory
|
||||
)
|
||||
{
|
||||
$this->accountGateway = $accountGateway;
|
||||
$this->googleAccountGateway = $googleAccountGateway;
|
||||
$this->clientFactory = $clientFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $senderEmail
|
||||
*
|
||||
* @throws MailClientConfigException
|
||||
*
|
||||
* @return ImapMailClientConfig
|
||||
*/
|
||||
public function createImapConfigFromEmail(string $senderEmail): ImapMailClientConfig
|
||||
{
|
||||
try {
|
||||
$account = $this->accountGateway->getAccountByEmail($senderEmail);
|
||||
|
||||
return $this->createImapConfigFromAccount($account);
|
||||
} catch (EmailBackupAccountException $e) {}
|
||||
try {
|
||||
return $this->createGoogleOauthImapConfig($senderEmail);
|
||||
} catch (Exception $e) {
|
||||
throw new MailClientConfigException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EmailBackupAccount $account
|
||||
*
|
||||
* @throws OAuthException
|
||||
*
|
||||
* @return ImapMailClientConfig
|
||||
*/
|
||||
public function createImapConfigFromAccount(EmailBackupAccount $account): ImapMailClientConfig
|
||||
{
|
||||
$sslEnabled = false;
|
||||
switch ($account->getImapType()) {
|
||||
|
||||
//IMAP with SSL
|
||||
case 3:
|
||||
$sslEnabled = true;
|
||||
break;
|
||||
|
||||
// IMAP via Google OAuth
|
||||
case 5:
|
||||
return $this->createGoogleOauthImapConfig($account->getEmailAddress());
|
||||
break;
|
||||
|
||||
// IMAP without SSL
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return new ImapMailClientConfig(
|
||||
$account->getImapServer(),
|
||||
$account->getImapPort(),
|
||||
$account->getUserName(),
|
||||
$account->getPassword(),
|
||||
ImapMailClientConfig::AUTH_BASIC,
|
||||
$sslEnabled,
|
||||
'INBOX'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
*
|
||||
* @throws OAuthException
|
||||
*
|
||||
* @return ImapMailClientConfig
|
||||
*/
|
||||
private function createGoogleOauthImapConfig(string $email): ImapMailClientConfig
|
||||
{
|
||||
$token = null;
|
||||
try {
|
||||
$account = $this->googleAccountGateway->getAccountByGmailAddress($email);
|
||||
$this->clientFactory->createClient($account->getUserId());
|
||||
$token = $this->googleAccountGateway->getAccessToken($account->getId());
|
||||
} catch (Exception $e) {
|
||||
throw new OAuthException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
return new ImapMailClientConfig(
|
||||
'imap.gmail.com',
|
||||
993,
|
||||
$email,
|
||||
$token->getToken(),
|
||||
ImapMailClientConfig::AUTH_XOAUTH2,
|
||||
true,
|
||||
'INBOX'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\SystemMailClient;
|
||||
|
||||
use Xentral\Components\MailClient\Client\MailClientInterface;
|
||||
use Xentral\Components\MailClient\MailClientFactory;
|
||||
use Xentral\Modules\SystemMailClient\Exception\EmailAccountNotFoundException;
|
||||
use Xentral\Modules\SystemMailClient\Exception\MailClientConfigException;
|
||||
use Xentral\Modules\SystemMailClient\Exception\OAuthException;
|
||||
use Xentral\Modules\SystemMailer\Data\EmailBackupAccount;
|
||||
use Xentral\Modules\SystemMailer\Exception\EmailBackupAccountException;
|
||||
use Xentral\Modules\SystemMailer\Service\EmailAccountGateway;
|
||||
|
||||
final class MailClientProvider implements MailClientProviderInterface
|
||||
{
|
||||
/** @var MailClientFactory */
|
||||
private $factory;
|
||||
|
||||
/** @var MailClientConfigProvider */
|
||||
private $configProvider;
|
||||
|
||||
/** @var EmailAccountGateway $accountGateway */
|
||||
private $accountGateway;
|
||||
|
||||
/**
|
||||
* @param MailClientFactory $factory
|
||||
* @param MailClientConfigProvider $configProvider
|
||||
* @param EmailAccountGateway $accountGateway
|
||||
*/
|
||||
public function __construct(
|
||||
MailClientFactory $factory,
|
||||
MailClientConfigProvider $configProvider,
|
||||
EmailAccountGateway $accountGateway
|
||||
) {
|
||||
$this->factory = $factory;
|
||||
$this->configProvider = $configProvider;
|
||||
$this->accountGateway = $accountGateway;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
*
|
||||
* @throws EmailAccountNotFoundException
|
||||
* @throws MailClientConfigException
|
||||
* @throws OAuthException
|
||||
*
|
||||
* @return MailClientInterface
|
||||
*/
|
||||
public function createMailClientByUserId(int $userId): MailClientInterface
|
||||
{
|
||||
try {
|
||||
$account = $this->accountGateway->getAccountByUser($userId);
|
||||
} catch (EmailBackupAccountException $e) {
|
||||
throw new EmailAccountNotFoundException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
return $this->createMailClientFromAccount($account);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $addressId
|
||||
*
|
||||
* @throws EmailAccountNotFoundException
|
||||
* @throws MailClientConfigException
|
||||
* @throws OAuthException
|
||||
*
|
||||
* @return MailClientInterface
|
||||
*/
|
||||
public function createMailClientByAddressId(int $addressId): MailClientInterface
|
||||
{
|
||||
try {
|
||||
$account = $this->accountGateway->getAccountByAddress($addressId);
|
||||
} catch (EmailBackupAccountException $e) {
|
||||
throw new EmailAccountNotFoundException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
return $this->createMailClientFromAccount($account);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $emailAddress
|
||||
*
|
||||
* @throws EmailAccountNotFoundException
|
||||
* @throws MailClientConfigException
|
||||
* @throws OAuthException
|
||||
*
|
||||
* @return MailClientInterface
|
||||
*/
|
||||
public function createMailClientByEmail(string $emailAddress): MailClientInterface
|
||||
{
|
||||
try {
|
||||
$account = $this->accountGateway->getAccountByEmail($emailAddress);
|
||||
} catch (EmailBackupAccountException $e) {
|
||||
throw new EmailAccountNotFoundException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
return $this->createMailClientFromAccount($account);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EmailBackupAccount $account
|
||||
*
|
||||
* @throws OAuthException
|
||||
* @throws MailClientConfigException
|
||||
*
|
||||
* @return MailClientInterface
|
||||
*/
|
||||
public function createMailClientFromAccount(EmailBackupAccount $account): MailClientInterface
|
||||
{
|
||||
switch ($account->getImapType()) {
|
||||
case 1:
|
||||
// NO BREAK
|
||||
case 3:
|
||||
// NO BREAK
|
||||
case 5:
|
||||
$config = $this->configProvider->createImapConfigFromAccount($account);
|
||||
|
||||
return $this->factory->createImapClient($config);
|
||||
}
|
||||
|
||||
throw new MailClientConfigException(sprintf('Unrecognized mail client type "%s"', $account->getImapType()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\SystemMailClient;
|
||||
|
||||
use Xentral\Components\MailClient\Client\MailClientInterface;
|
||||
use Xentral\Modules\SystemMailer\Data\EmailBackupAccount;
|
||||
|
||||
interface MailClientProviderInterface
|
||||
{
|
||||
/**
|
||||
* @param int $userId
|
||||
*
|
||||
* @return MailClientInterface
|
||||
*/
|
||||
public function createMailClientByUserId(int $userId): MailClientInterface;
|
||||
|
||||
/**
|
||||
* @param int $addressId
|
||||
*
|
||||
* @return MailClientInterface
|
||||
*/
|
||||
public function createMailClientByAddressId(int $addressId): MailClientInterface;
|
||||
|
||||
/**
|
||||
* @param string $emailAddress
|
||||
*
|
||||
* @return MailClientInterface
|
||||
*/
|
||||
public function createMailClientByEmail(string $emailAddress): MailClientInterface;
|
||||
|
||||
/**
|
||||
* @param EmailBackupAccount $account
|
||||
*
|
||||
* @return MailClientInterface
|
||||
*/
|
||||
public function createMailClientFromAccount(EmailBackupAccount $account): MailClientInterface;
|
||||
}
|
||||
Reference in New Issue
Block a user