Skip to content
Merged
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
Comment thread
Nazar65 marked this conversation as resolved.
*/
private $inlineTranslation;

/**
* Core store config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
Comment thread
Nazar65 marked this conversation as resolved.
*/
private $scopeConfig;

/**
* @var \Magento\Framework\Mail\Template\TransportBuilder
Comment thread
Nazar65 marked this conversation as resolved.
*/
private $transportBuilder;

/**
* @var \Psr\Log\LoggerInterface $logger
Comment thread
Nazar65 marked this conversation as resolved.
*/
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
Comment thread
Nazar65 marked this conversation as resolved.
*/
public function sendErrors($errors)
Comment thread
Nazar65 marked this conversation as resolved.
{
$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();
}
}
}
107 changes: 42 additions & 65 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;

/**
Comment thread
Nazar65 marked this conversation as resolved.
* 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;
Comment thread
sidolov marked this conversation as resolved.

/**
* @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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new parameter should be optional

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here if i add optional, as this have been in older commit the codeMess test will fail.
Backward Compatibility Policy is not applied to Plugins, Observers and Setup Scripts.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class Observer has a coupling between objects value of 13.

) {
$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);
}
}
}
134 changes: 134 additions & 0 deletions app/code/Magento/Sitemap/Test/Unit/Model/EmailNotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento\Sitemap\Test\Unit\Model;

use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\TransportInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Sitemap\Model\EmailNotification;
use Magento\Sitemap\Model\Observer;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use PHPUnit\Framework\TestCase;

/**
* Test for Magento\Sitemap\Model\EmailNotification
*/
class EmailNotificationTest extends TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var EmailNotification
*/
private $model;

/**
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $scopeConfigMock;

/**
* @var TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
private $transportBuilderMock;

/**
* @var StateInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $inlineTranslationMock;

/**
* @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $objectManagerMock;

protected function setUp()
{
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
->getMock();
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->getMock();
$this->transportBuilderMock = $this->getMockBuilder(TransportBuilder::class)
->disableOriginalConstructor()
->getMock();
$this->inlineTranslationMock = $this->getMockBuilder(StateInterface::class)
->getMock();

$this->objectManager = new ObjectManager($this);
$this->model = $this->objectManager->getObject(
EmailNotification::class,
[
'inlineTranslation' => $this->inlineTranslationMock,
'scopeConfig' => $this->scopeConfigMock,
'transportBuilder' => $this->transportBuilderMock,
]
);
}

public function testSendErrors()
{
$exception = 'Sitemap Exception';
$transport = $this->createMock(TransportInterface::class);

$this->scopeConfigMock->expects($this->at(0))
->method('getValue')
->with(
Observer::XML_PATH_ERROR_TEMPLATE,
ScopeInterface::SCOPE_STORE
)
->willReturn('error-recipient@example.com');

$this->inlineTranslationMock->expects($this->once())
->method('suspend');

$this->transportBuilderMock->expects($this->once())
->method('setTemplateIdentifier')
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('setTemplateOptions')
->with([
'area' => FrontNameResolver::AREA_CODE,
'store' => Store::DEFAULT_STORE_ID,
])
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('setTemplateVars')
->with(['warnings' => $exception])
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('setFrom')
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('addTo')
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('getTransport')
->willReturn($transport);

$transport->expects($this->once())
->method('sendMessage');

$this->inlineTranslationMock->expects($this->once())
->method('resume');

$this->model->sendErrors(['Sitemap Exception']);
}
}
Loading