Skip to content

Commit

Permalink
Index email/scopes config by scope email
Browse files Browse the repository at this point in the history
  • Loading branch information
tr33m4n committed Jan 7, 2023
1 parent d27ad56 commit a4467b3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Block/Adminhtml/Form/Field/ImpersonatedEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\DataObject;
use Magento\Framework\View\Element\BlockInterface;
use tr33m4n\OauthGmail\Model\Config\Provider;

class ImpersonatedEmails extends AbstractFieldArray
{
Expand All @@ -20,15 +21,15 @@ class ImpersonatedEmails extends AbstractFieldArray
protected function _prepareToRender()
{
$this->addColumn(
'email',
Provider::EMAIL_KEY,
[
'label' => __('Email'),
'class' => 'required-entry validate-email'
]
);

$this->addColumn(
'scopes',
Provider::SCOPES_KEY,
[
'label' => __('Scopes'),
'renderer' => $this->getScopesRenderer()
Expand Down
55 changes: 50 additions & 5 deletions src/Model/Config/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\ReadInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\Serialize\SerializerInterface;
use tr33m4n\OauthGmail\Exception\ConfigException;

class Provider
Expand All @@ -26,19 +28,36 @@ class Provider

private const AUTH_PATH_TEMPLATE = 'oauth_gmail' . DIRECTORY_SEPARATOR . '%s';

public const EMAIL_KEY = 'email';

public const SCOPES_KEY = 'scopes';

private SerializerInterface $serializer;

private ScopeConfigInterface $scopeConfig;

private ReadInterface $varDirectory;

private SenderResolverInterface $senderResolver;

/**
* @var array<string, string>
*/
private ?array $impersonatedEmails = null;

/**
* Provider constructor.
*/
public function __construct(
SerializerInterface $serializer,
ScopeConfigInterface $scopeConfig,
Filesystem $filesystem
Filesystem $filesystem,
SenderResolverInterface $senderResolver
) {
$this->serializer = $serializer;
$this->scopeConfig = $scopeConfig;
$this->varDirectory = $filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
$this->senderResolver = $senderResolver;
}

/**
Expand Down Expand Up @@ -118,16 +137,42 @@ public function shouldUseImpersonated(): bool
* Get impersonated emails
*
* @throws \tr33m4n\OauthGmail\Exception\ConfigException
* @throws \Magento\Framework\Exception\MailException
* @return array<string, string>
*/
public function getImpersonatedEmails(): array
{
$impersonatedEmail = $this->scopeConfig->getValue(self::XML_CONFIG_IMPERSONATED_EMAILS);
if (!filter_var($impersonatedEmail, FILTER_VALIDATE_EMAIL)) {
throw new ConfigException(__('Impersonated email is not a valid email'));
if (null !== $this->impersonatedEmails) {
return $this->impersonatedEmails;
}

$impersonatedEmails = $this->scopeConfig->getValue(self::XML_CONFIG_IMPERSONATED_EMAILS);
if (is_string($impersonatedEmails)) {
$impersonatedEmails = $this->serializer->unserialize($impersonatedEmails);
}

if (empty($impersonatedEmails)) {
throw new ConfigException(__('No impersonated emails have been set'));
}

$indexedEmails = [];
foreach ($impersonatedEmails as $impersonatedEmail) {
$scopes = $impersonatedEmail[self::SCOPES_KEY] ?? [];
if (empty($scopes)) {
continue;
}

foreach ($scopes as $scope) {
$resolvedEmail = $this->senderResolver->resolve($scope)[self::EMAIL_KEY] ?? null;
if (!$resolvedEmail) {
continue;
}

$indexedEmails[$resolvedEmail] = $impersonatedEmails[self::EMAIL_KEY];
}
}

return $impersonatedEmail;
return $this->impersonatedEmails = $indexedEmails;
}

/**
Expand Down

0 comments on commit a4467b3

Please sign in to comment.