Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions app/code/Magento/Sitemap/Model/EmailNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento\Sitemap\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Store\Model\ScopeInterface;
use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Sitemap\Model\Observer as Observer;
use Psr\Log\LoggerInterface;

/**
* Sends emails for the scheduled generation of the sitemap file
*/
class EmailNotification
{
/**
* @var \Magento\Framework\Translate\Inline\StateInterface
*/
private $inlineTranslation;

/**
* Core store config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var \Magento\Framework\Mail\Template\TransportBuilder
*/
private $transportBuilder;

/**
* @var \Psr\Log\LoggerInterface $logger
*/
private $logger;

/**
* EmailNotification constructor.
* @param StateInterface $inlineTranslation
* @param TransportBuilder $transportBuilder
* @param ScopeConfigInterface $scopeConfig
* @param LoggerInterface $logger
*/
public function __construct(
StateInterface $inlineTranslation,
TransportBuilder $transportBuilder,
ScopeConfigInterface $scopeConfig,
LoggerInterface $logger
) {
$this->inlineTranslation = $inlineTranslation;
$this->scopeConfig = $scopeConfig;
$this->transportBuilder = $transportBuilder;
$this->logger = $logger;
}

/**
* Send's error email if sitemap generated with errors.
*
* @param array| $errors
*/
public function sendErrors($errors)
{
$this->inlineTranslation->suspend();
try {
$this->transportBuilder->setTemplateIdentifier(
$this->scopeConfig->getValue(
Observer::XML_PATH_ERROR_TEMPLATE,
ScopeInterface::SCOPE_STORE
)
)->setTemplateOptions(
[
'area' => FrontNameResolver::AREA_CODE,
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
]
)->setTemplateVars(
['warnings' => join("\n", $errors)]
)->setFrom(
$this->scopeConfig->getValue(
Observer::XML_PATH_ERROR_IDENTITY,
ScopeInterface::SCOPE_STORE
)
)->addTo(
$this->scopeConfig->getValue(
Observer::XML_PATH_ERROR_RECIPIENT,
ScopeInterface::SCOPE_STORE
)
);

$transport = $this->transportBuilder->getTransport();
$transport->sendMessage();
} catch (\Exception $e) {
$this->logger->error('Sitemap sendErrors: '.$e->getMessage());
} finally {
$this->inlineTranslation->resume();
}
}
}
109 changes: 43 additions & 66 deletions app/code/Magento/Sitemap/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
*/
namespace Magento\Sitemap\Model;

use Magento\Store\Model\App\Emulation;
use Magento\Sitemap\Model\EmailNotification as SitemapEmail;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
use Magento\Store\Model\ScopeInterface;

/**
* Sitemap module observer
*
Expand Down Expand Up @@ -44,47 +50,40 @@ class Observer
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
private $scopeConfig;

/**
* @var \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory
*/
protected $_collectionFactory;

/**
* @var \Magento\Framework\Mail\Template\TransportBuilder
*/
protected $_transportBuilder;
private $collectionFactory;

/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var Emulation
*/
protected $_storeManager;
private $appEmulation;

/**
* @var \Magento\Framework\Translate\Inline\StateInterface
* @var $emailNotification
*/
protected $inlineTranslation;
private $emailNotification;

/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory $collectionFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
* Observer constructor.
* @param ScopeConfigInterface $scopeConfig
* @param CollectionFactory $collectionFactory
* @param EmailNotification $emailNotification
* @param Emulation $appEmulation
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory $collectionFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
ScopeConfigInterface $scopeConfig,
CollectionFactory $collectionFactory,
SitemapEmail $emailNotification,
Emulation $appEmulation
) {
$this->_scopeConfig = $scopeConfig;
$this->_collectionFactory = $collectionFactory;
$this->_storeManager = $storeManager;
$this->_transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->scopeConfig = $scopeConfig;
$this->collectionFactory = $collectionFactory;
$this->appEmulation = $appEmulation;
$this->emailNotification = $emailNotification;
}

/**
Expand All @@ -97,61 +96,39 @@ public function __construct(
public function scheduledGenerateSitemaps()
{
$errors = [];

$recipient = $this->scopeConfig->getValue(
Observer::XML_PATH_ERROR_RECIPIENT,
ScopeInterface::SCOPE_STORE
);
// check if scheduled generation enabled
if (!$this->_scopeConfig->isSetFlag(
if (!$this->scopeConfig->isSetFlag(
self::XML_PATH_GENERATION_ENABLED,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
ScopeInterface::SCOPE_STORE
)
) {
return;
}

$collection = $this->_collectionFactory->create();
$collection = $this->collectionFactory->create();
/* @var $collection \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection */
foreach ($collection as $sitemap) {
/* @var $sitemap \Magento\Sitemap\Model\Sitemap */
try {
$this->appEmulation->startEnvironmentEmulation(
$sitemap->getStoreId(),
\Magento\Framework\App\Area::AREA_FRONTEND,
true
);

$sitemap->generateXml();
} catch (\Exception $e) {
$errors[] = $e->getMessage();
} finally {
$this->appEmulation->stopEnvironmentEmulation();
}
}

if ($errors && $this->_scopeConfig->getValue(
self::XML_PATH_ERROR_RECIPIENT,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
) {
$this->inlineTranslation->suspend();

$this->_transportBuilder->setTemplateIdentifier(
$this->_scopeConfig->getValue(
self::XML_PATH_ERROR_TEMPLATE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
)->setTemplateOptions(
[
'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
]
)->setTemplateVars(
['warnings' => join("\n", $errors)]
)->setFrom(
$this->_scopeConfig->getValue(
self::XML_PATH_ERROR_IDENTITY,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
)->addTo(
$this->_scopeConfig->getValue(
self::XML_PATH_ERROR_RECIPIENT,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
);
$transport = $this->_transportBuilder->getTransport();
$transport->sendMessage();

$this->inlineTranslation->resume();
if ($errors && $recipient) {
$this->emailNotification->sendErrors($errors);
}
}
}
}
Loading