Skip to content

Commit

Permalink
Non-blocking notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Apr 29, 2022
1 parent 4ef6b09 commit 6e1263c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 53 deletions.
23 changes: 12 additions & 11 deletions Model/Customer/Erase/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,29 @@
use Opengento\Gdpr\Model\Customer\Notifier\SenderInterface;
use Opengento\Gdpr\Model\Customer\OrigDataRegistry;
use Opengento\Gdpr\Model\Erase\NotifierInterface;
use Psr\Log\LoggerInterface;

final class Notifier implements NotifierInterface
{
/**
* @var SenderInterface[]
*/
/** @var SenderInterface[] */
private array $senders;

/**
* @var CustomerRepositoryInterface
*/
private CustomerRepositoryInterface $customerRepository;

/**
* @var OrigDataRegistry
*/
private OrigDataRegistry $origDataRegistry;

private LoggerInterface $logger;

public function __construct(
array $senders,
CustomerRepositoryInterface $customerRepository,
OrigDataRegistry $origDataRegistry
OrigDataRegistry $origDataRegistry,
LoggerInterface $logger
) {
$this->senders = $senders;
$this->customerRepository = $customerRepository;
$this->origDataRegistry = $origDataRegistry;
$this->logger = $logger;
}

/**
Expand All @@ -51,7 +48,11 @@ public function notify(EraseEntityInterface $eraseEntity): void
$customer = $this->origDataRegistry->get($customerId) ?? $this->customerRepository->getById($customerId);

foreach ($this->senders as $sender) {
$sender->send($customer);
try {
$sender->send($customer);
} catch (LocalizedException $e) {
$this->logger->error($e->getLogMessage(), $e->getTrace());
}
}
}
}
20 changes: 12 additions & 8 deletions Model/Customer/Export/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Model\Customer\Notifier\SenderInterface;
use Opengento\Gdpr\Model\Export\NotifierInterface;
use Psr\Log\LoggerInterface;

final class Notifier implements NotifierInterface
{
/**
* @var SenderInterface[]
*/
/** @var SenderInterface[] */
private array $senders;

/**
* @var CustomerRepositoryInterface
*/
private CustomerRepositoryInterface $customerRepository;

private LoggerInterface $logger;

public function __construct(
array $senders,
CustomerRepositoryInterface $customerRepository
CustomerRepositoryInterface $customerRepository,
LoggerInterface $logger
) {
$this->senders = $senders;
$this->customerRepository = $customerRepository;
$this->logger = $logger;
}

/**
Expand All @@ -42,7 +42,11 @@ public function notify(ExportEntityInterface $exportEntity): void
$customer = $this->customerRepository->getById($exportEntity->getEntityId());

foreach ($this->senders as $sender) {
$sender->send($customer);
try {
$sender->send($customer);
} catch (LocalizedException $e) {
$this->logger->error($e->getLogMessage(), $e->getTrace());
}
}
}
}
27 changes: 3 additions & 24 deletions Model/Customer/Notifier/MailSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,26 @@
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Store\Model\StoreManagerInterface;
use Opengento\Gdpr\Model\Notifier\AbstractMailSender;
use Psr\Log\LoggerInterface;

final class MailSender extends AbstractMailSender implements SenderInterface
{
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;

/**
* @var View
*/
private View $customerViewHelper;

/**
* @var StoreManagerInterface
*/
private $storeManager;
private StoreManagerInterface $storeManager;

public function __construct(
LoggerInterface $logger,
View $customerViewHelper,
TransportBuilder $transportBuilder,
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
array $configPaths
) {
$this->logger = $logger;
$this->customerViewHelper = $customerViewHelper;
$this->storeManager = $storeManager;
parent::__construct($transportBuilder, $scopeConfig, $configPaths);
}

/**
* @param CustomerInterface $customer
* @return void
* @throws LocalizedException
* @throws MailException
* @throws NoSuchEntityException
Expand All @@ -66,12 +50,7 @@ public function send(CustomerInterface $customer): void
'customer_name' => $this->customerViewHelper->getCustomerName($customer),
],
];

try {
$this->sendMail($customer->getEmail(), $this->customerViewHelper->getCustomerName($customer), $storeId, $vars);
$this->logger->debug(__('GDPR Email Success'));
} catch (MailException $exc) {
$this->logger->error(__('GDPR Email Error: %1', $exc->getMessage()));
}

$this->sendMail($customer->getEmail(), $this->customerViewHelper->getCustomerName($customer), $storeId, $vars);
}
}
4 changes: 4 additions & 0 deletions Model/Customer/Notifier/SenderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
namespace Opengento\Gdpr\Model\Customer\Notifier;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\LocalizedException;

/**
* @api
*/
interface SenderInterface
{
/**
* @throws LocalizedException
*/
public function send(CustomerInterface $customer): void;
}
18 changes: 13 additions & 5 deletions Model/Order/Erase/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,42 @@

namespace Opengento\Gdpr\Model\Order\Erase;

use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Api\OrderRepositoryInterface;
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
use Opengento\Gdpr\Model\Erase\NotifierInterface;
use Opengento\Gdpr\Model\Order\Notifier\SenderInterface;
use Psr\Log\LoggerInterface;

final class Notifier implements NotifierInterface
{
/**
* @var SenderInterface[]
*/
/** @var SenderInterface[] */
private array $senders;

private OrderRepositoryInterface $orderRepository;

private LoggerInterface $logger;

public function __construct(
array $senders,
OrderRepositoryInterface $orderRepository
OrderRepositoryInterface $orderRepository,
LoggerInterface $logger
) {
$this->senders = $senders;
$this->orderRepository = $orderRepository;
$this->logger = $logger;
}

public function notify(EraseEntityInterface $eraseEntity): void
{
$order = $this->orderRepository->get($eraseEntity->getEntityId());

foreach ($this->senders as $sender) {
$sender->send($order);
try {
$sender->send($order);
} catch (LocalizedException $e) {
$this->logger->error($e->getLogMessage(), $e->getTrace());
}
}
}
}
18 changes: 13 additions & 5 deletions Model/Order/Export/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@

namespace Opengento\Gdpr\Model\Order\Export;

use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Api\OrderRepositoryInterface;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Model\Export\NotifierInterface;
use Opengento\Gdpr\Model\Order\Notifier\SenderInterface;
use Psr\Log\LoggerInterface;

final class Notifier implements NotifierInterface
{
/**
* @var SenderInterface[]
*/
/** @var SenderInterface[] */
private array $senders;

private OrderRepositoryInterface $orderRepository;

private LoggerInterface $logger;

public function __construct(
array $senders,
OrderRepositoryInterface $orderRepository
OrderRepositoryInterface $orderRepository,
LoggerInterface $logger
) {
$this->senders = $senders;
$this->orderRepository = $orderRepository;
$this->logger = $logger;
}

/**
Expand All @@ -37,7 +41,11 @@ public function notify(ExportEntityInterface $exportEntity): void
$order = $this->orderRepository->get($exportEntity->getEntityId());

foreach ($this->senders as $sender) {
$sender->send($order);
try {
$sender->send($order);
} catch (LocalizedException $e) {
$this->logger->error($e->getLogMessage(), $e->getTrace());
}
}
}
}
4 changes: 4 additions & 0 deletions Model/Order/Notifier/SenderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@

namespace Opengento\Gdpr\Model\Order\Notifier;

use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Api\Data\OrderInterface;

/**
* @api
*/
interface SenderInterface
{
/**
* @throws LocalizedException
*/
public function send(OrderInterface $order): void;
}

0 comments on commit 6e1263c

Please sign in to comment.