Newer
Older
<?php
declare(strict_types=1);
/**
* Copyright (c) 2024 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see https://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*
* @copyright Copyright (c) 2024 TechDivision GmbH (https://www.techdivision.com)
* @author TechDivision Team Zero <zero@techdivision.com>
* @link https://www.techdivision.com/
*/
namespace Firegento\ContentProvisioning\Cron;
use Firegento\ContentProvisioning\Model\Query\GetChangedBlockIdentifiers;
use Firegento\ContentProvisioning\Model\Query\GetFilesContentInformation;
use Firegento\ContentProvisioning\Model\RenderChangedBlockList;
use Magento\Framework\Mail\Message;
use Psr\Log\LoggerInterface;
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
class InconsistenciesInBlockEmail
{
public const EMAIL_SUBJECT = "Inconsistencies in CMS block";
public const SENDER_EMAIL = 'trans_email/ident_general/email';
public const RECIPIENT_EMAIL = 'trans_email/ident_content_provisioning/email';
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @var TransportInterfaceFactory
*/
private TransportInterfaceFactory $mailTransportFactory;
/**
* @var Message
*/
private Message $message;
/**
* @var ScopeConfigInterface
*/
private ScopeConfigInterface $scopeConfig;
/**
* @var RenderChangedBlockList
*/
private RenderChangedBlockList $renderChangedBlockList;
/**
* @var GetChangedBlockIdentifiers
*/
private GetChangedBlockIdentifiers $getChangedBlockIdentifier;
/**
* @var GetFilesContentInformation
*/
private GetFilesContentInformation $getFilesContentInformation;
/**
* @param LoggerInterface $logger
* @param TransportInterfaceFactory $mailTransportFactory
* @param Message $message
* @param ScopeConfigInterface $scopeConfig
* @param RenderChangedBlockList $renderChangedBlockList
* @param GetFilesContentInformation $getFilesContentInformation
* @param GetChangedBlockIdentifiers $getChangedBlockIdentifier
*/
public function __construct(
LoggerInterface $logger,
TransportInterfaceFactory $mailTransportFactory,
Message $message,
ScopeConfigInterface $scopeConfig,
RenderChangedBlockList $renderChangedBlockList,
GetFilesContentInformation $getFilesContentInformation,
GetChangedBlockIdentifiers $getChangedBlockIdentifier
) {
$this->logger = $logger;
$this->mailTransportFactory = $mailTransportFactory;
$this->message = $message;
$this->scopeConfig = $scopeConfig;
$this->renderChangedBlockList = $renderChangedBlockList;
$this->getFilesContentInformation = $getFilesContentInformation;
$this->getChangedBlockIdentifier = $getChangedBlockIdentifier;
}
/**
* call sendEmail function or write to system.log
*
* @return void
* @throws \Exception
*/
public function execute(): void
{
$files = $this->getFilesContentInformation->execute();
$changedBlocks = $this->getChangedBlockIdentifier->execute($files);
$emailMessage = $this->getValuesForEmail($changedBlocks);
$this->sendEmail($emailMessage);
} else {
$this->logger->info("No changed blocks found.");
}
}
/**
* Transform array values into a string to be displayed in the email
* @param array $entries
* @return string
*/
public function getValuesForEmail(array $entries): string
{
$entriesList = [];
foreach ($entries as $entry) {
$entriesList[] = "key: " . $entry['key'] . ", identifier: " . $entry['identifier'];
}
return implode(PHP_EOL, $entriesList);
}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @param string $message
* @return void
*/
public function sendEmail(string $message): 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]);
$transport->sendMessage();
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
} else {
$this->logger->error("Email not sent! No recipient email found.");
}
}
/**
* Check if there is any email saved in the admin panel
* @return bool
*/
public function hasRecipientEmail(): bool
{
return $this->scopeConfig->getValue(self::RECIPIENT_EMAIL) ? true : false;
}
}