Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazar65 committed Dec 12, 2018
1 parent d2d04b6 commit b88fa48
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento\Sitemap\Model;

Expand All @@ -11,28 +12,14 @@
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Store\Model\ScopeInterface;
use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Store\Model\Store;
use Magento\Sitemap\Model\Observer as Observer;
use Psr\Log\LoggerInterface;

/**
* Sends emails for the scheduled generation of the sitemap file
*/
class SitemapSendEmail
class EmailNotification
{
/**
* Error email template configuration
*/
const XML_PATH_ERROR_TEMPLATE = 'sitemap/generate/error_email_template';

/**
* Error email identity configuration
*/
const XML_PATH_ERROR_IDENTITY = 'sitemap/generate/error_email_identity';

/**
* 'Send error emails to' configuration
*/
const XML_PATH_ERROR_RECIPIENT = 'sitemap/generate/error_email';

/**
* @var \Magento\Framework\Translate\Inline\StateInterface
*/
Expand All @@ -43,66 +30,75 @@ class SitemapSendEmail
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $_scopeConfig;
private $scopeConfig;

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

/**
* @var $logger
*/
private $logger;

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

/**
* Send's error email if sitemap generated with errors.
*
* @param array $errors
* @throws \Magento\Framework\Exception\MailException
* @param array| $errors
*/
public function sendErrorEmail($errors)
public function sendErrors($errors)
{
$recipient = $this->_scopeConfig->getValue(
self::XML_PATH_ERROR_RECIPIENT,
ScopeInterface::SCOPE_STORE
);
if ($errors && $recipient) {
$this->inlineTranslation->suspend();

$this->_transportBuilder->setTemplateIdentifier(
$this->_scopeConfig->getValue(
self::XML_PATH_ERROR_TEMPLATE,
$this->inlineTranslation->suspend();
try {
$this->transportBuilder->setTemplateIdentifier(
$this->scopeConfig->getValue(
Observer::XML_PATH_ERROR_TEMPLATE,
ScopeInterface::SCOPE_STORE
)
)->setTemplateOptions(
[
'area' => FrontNameResolver::AREA_CODE,
'store' => Store::DEFAULT_STORE_ID,
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
]
)->setTemplateVars(
['warnings' => join("\n", $errors)]
)->setFrom(
$this->_scopeConfig->getValue(
self::XML_PATH_ERROR_IDENTITY,
$this->scopeConfig->getValue(
Observer::XML_PATH_ERROR_IDENTITY,
ScopeInterface::SCOPE_STORE
)
)->addTo(
$this->scopeConfig->getValue(
Observer::XML_PATH_ERROR_RECIPIENT,
ScopeInterface::SCOPE_STORE
)
)->addTo($recipient);
);

$transport = $this->_transportBuilder->getTransport();
$transport = $this->transportBuilder->getTransport();
$transport->sendMessage();

} catch (\Exception $e) {
$this->logger->error('Sitemap sendErrors: '.$e->getMessage());
} finally {
$this->inlineTranslation->resume();
}
}
Expand Down
53 changes: 36 additions & 17 deletions app/code/Magento/Sitemap/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Magento\Sitemap\Model;

use Magento\Store\Model\App\Emulation;
use Magento\Sitemap\Model\SitemapSendEmail as SitemapEmail;
use Magento\Sitemap\Model\EmailNotification as SitemapEmail;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
Expand All @@ -31,53 +31,68 @@ class Observer
*/
const XML_PATH_CRON_EXPR = 'crontab/default/jobs/generate_sitemaps/schedule/cron_expr';

/**
* Error email template configuration
*/
const XML_PATH_ERROR_TEMPLATE = 'sitemap/generate/error_email_template';

/**
* Error email identity configuration
*/
const XML_PATH_ERROR_IDENTITY = 'sitemap/generate/error_email_identity';

/**
* 'Send error emails to' configuration
*/
const XML_PATH_ERROR_RECIPIENT = 'sitemap/generate/error_email';

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

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

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

/**
* @var Emulation
*/
private $appEmulation;

/**
* @var $sitemapEmail
* @var $emailNotification
*/
private $sitemapEmail;
private $emailNotification;

/**
* Observer constructor.
* @param ScopeConfigInterface $scopeConfig
* @param CollectionFactory $collectionFactory
* @param StoreManagerInterface $storeManager
* @param SitemapSendEmail $sitemapEmail
* @param EmailNotification $emailNotification
* @param Emulation $appEmulation
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
CollectionFactory $collectionFactory,
StoreManagerInterface $storeManager,
SitemapEmail $sitemapEmail,
SitemapEmail $emailNotification,
Emulation $appEmulation
) {
$this->_scopeConfig = $scopeConfig;
$this->_collectionFactory = $collectionFactory;
$this->_storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->collectionFactory = $collectionFactory;
$this->storeManager = $storeManager;
$this->appEmulation = $appEmulation;
$this->sitemapEmail = $sitemapEmail;
$this->emailNotification = $emailNotification;
}

/**
Expand All @@ -90,17 +105,20 @@ 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,
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 */
Expand All @@ -114,11 +132,12 @@ public function scheduledGenerateSitemaps()
$sitemap->generateXml();
} catch (\Exception $e) {
$errors[] = $e->getMessage();

$this->sitemapEmail->sendErrorEmail($errors);
} finally {
$this->appEmulation->stopEnvironmentEmulation();
}
}
if ($errors && $recipient) {
$this->emailNotification->sendErrors($errors);
}
}
}

0 comments on commit b88fa48

Please sign in to comment.