Skip to content
Snippets Groups Projects

ZERO-162-changes

Open Eduarda Lentz Rodrigues da Silva requested to merge ZERO-162-changes into develop
Compare and Show latest version
13 files
+ 90
77
Compare changes
  • Side-by-side
  • Inline
Files
13
@@ -19,13 +19,21 @@ declare(strict_types=1);
namespace Firegento\ContentProvisioning\Cron;
use Firegento\ContentProvisioning\Model\Console\BlockListCommand;
use Magento\Framework\Mail\Message;
use Psr\Log\LoggerInterface;
use Firegento\ContentProvisioning\Model\DTO\CmsBlockDto;
use Firegento\ContentProvisioning\Model\Query\GetChangedBlockIdentifiers;
use Firegento\ContentProvisioning\Model\Query\GetFilesContentInformation;
use Magento\Framework\Mail\EmailMessageInterfaceFactory;
use Magento\Framework\Mail\EmailMessageInterface;
use Magento\Framework\Mail\MimePartInterfaceFactory;
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\Mail\TransportInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Mail\MimePartInterface;
use Magento\Framework\Mail\MimeMessageFactory;
use Magento\Framework\Mail\AddressFactory;
class InconsistenciesInBlockEmailTest
class InconsistenciesInCmsBlock
{
public const EMAIL_SUBJECT = "Inconsistencies in CMS block";
@@ -39,44 +47,76 @@ class InconsistenciesInBlockEmailTest
private LoggerInterface $logger;
/**
* @var BlockListCommand
* @var TransportInterfaceFactory
*/
private BlockListCommand $blockList;
private TransportInterfaceFactory $mailTransportFactory;
/**
* @var TransportInterfaceFactory
* @var ScopeConfigInterface
*/
private TransportInterfaceFactory $mailTransportFactory;
private ScopeConfigInterface $scopeConfig;
/**
* @var Message
* @var GetChangedBlockIdentifiers
*/
private Message $message;
private GetChangedBlockIdentifiers $getChangedBlockIdentifier;
/**
* @var ScopeConfigInterface
* @var GetFilesContentInformation
*/
private ScopeConfigInterface $scopeConfig;
private GetFilesContentInformation $getFilesContentInformation;
/**
* @var EmailMessageInterfaceFactory
*/
private EmailMessageInterfaceFactory $messageFactory;
/**
* @var MimePartInterfaceFactory
*/
private MimePartInterfaceFactory $mimePartFactory;
/**
* @var AddressFactory
*/
private AddressFactory $addressFactory;
/**
* @var MimeMessageFactory
*/
private MimeMessageFactory $mimeMessageFactory;
/**
* @param LoggerInterface $logger
* @param BlockListCommand $blockList
* @param TransportInterfaceFactory $mailTransportFactory
* @param Message $message
* @param EmailMessageInterfaceFactory $messageFactory
* @param ScopeConfigInterface $scopeConfig
* @param GetFilesContentInformation $getFilesContentInformation
* @param GetChangedBlockIdentifiers $getChangedBlockIdentifier
* @param MimePartInterfaceFactory $mimePartFactory
* @param MimeMessageFactory $mimeMessageFactory
* @param AddressFactory $addressFactory
*/
public function __construct(
LoggerInterface $logger,
BlockListCommand $blockList,
TransportInterfaceFactory $mailTransportFactory,
Message $message,
ScopeConfigInterface $scopeConfig
EmailMessageInterfaceFactory $messageFactory,
ScopeConfigInterface $scopeConfig,
GetFilesContentInformation $getFilesContentInformation,
GetChangedBlockIdentifiers $getChangedBlockIdentifier,
MimePartInterfaceFactory $mimePartFactory,
MimeMessageFactory $mimeMessageFactory,
AddressFactory $addressFactory
) {
$this->logger = $logger;
$this->blockList = $blockList;
$this->mailTransportFactory = $mailTransportFactory;
$this->message = $message;
$this->messageFactory =$messageFactory;
$this->scopeConfig = $scopeConfig;
$this->getFilesContentInformation = $getFilesContentInformation;
$this->getChangedBlockIdentifier = $getChangedBlockIdentifier;
$this->mimePartFactory = $mimePartFactory;
$this->mimeMessageFactory = $mimeMessageFactory;
$this->addressFactory = $addressFactory;
}
/**
@@ -87,9 +127,10 @@ class InconsistenciesInBlockEmailTest
*/
public function execute(): void
{
$changedBlocks = $this->blockList->getChangedEntries();
if ($changedBlocks) {
$emailMessage = $this->blockList->renderValuesForEmail($changedBlocks);
$filesContentInformation = $this->getFilesContentInformation->execute();
$changedBlocksIdentifier = $this->getChangedBlockIdentifier->execute($filesContentInformation);
if ($changedBlocksIdentifier) {
$emailMessage = $this->getValuesForEmail($changedBlocksIdentifier);
$this->sendEmail($emailMessage);
} else {
$this->logger->info("No changed blocks found.");
@@ -97,18 +138,66 @@ class InconsistenciesInBlockEmailTest
}
/**
* @param string $message
* Transform array values into a string to be displayed in the email
* @param CmsBlockDto[] $entries
* @return string
*/
private function getValuesForEmail(array $entries): string
{
$entriesList = [];
foreach ($entries as $entry) {
$entriesList[] = "key: " . $entry->getKey() . ", identifier: " . $entry->getIdentifier();
}
return implode(PHP_EOL, $entriesList);
}
/**
* @param string $messageText
* @return void
*/
public function sendEmail(string $message): void
private function sendEmail(string $messageText): void
{
if ($this->hasRecipientEmail()) {
try {
$this->message->setFrom($this->scopeConfig->getValue(self::SENDER_EMAIL));
$this->message->addTo($this->scopeConfig->getValue(self::RECIPIENT_EMAIL));
$this->message->setSubject(self::EMAIL_SUBJECT);
$this->message->setBody($message);
$transport = $this->mailTransportFactory->create(['message' => $this->message]);
$mimePart = $this->mimePartFactory->create(
[
'content' => $messageText,
'type' => 'text/plain',
'charset' => 'utf-8'
]
);
$message = $this->mimeMessageFactory->create(
[
'parts' => [$mimePart]
]
);
$from = $this->addressFactory->create(
[
'email' => $this->scopeConfig->getValue(self::SENDER_EMAIL),
'name' => ''
]
);
$to = $this->addressFactory->create(
[
'email' => $this->scopeConfig->getValue(self::RECIPIENT_EMAIL),
'name' => ''
]
);
$data = $this->messageFactory->create(
[
'subject' => self::EMAIL_SUBJECT,
'from' => [$from],
'to' => [$to],
'body' => $message
]
);
/** @var TransportInterface $transport */
$transport = $this->mailTransportFactory->create(['message' => $data]);
$transport->sendMessage();
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
@@ -122,8 +211,8 @@ class InconsistenciesInBlockEmailTest
* Check if there is any email saved in the admin panel
* @return bool
*/
public function hasRecipientEmail(): bool
private function hasRecipientEmail(): bool
{
return $this->scopeConfig->getValue(self::RECIPIENT_EMAIL) ? true : false;
return $this->scopeConfig->isSetFlag(self::RECIPIENT_EMAIL) ? true : false;
}
}
Loading