Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images in XML sitemap are always linked to base store in multistore on Schedule #19598

Merged
merged 9 commits into from
Feb 13, 2019
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
Nazar65 marked this conversation as resolved.
Show resolved Hide resolved
*/
private $inlineTranslation;

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

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

/**
* @var \Psr\Log\LoggerInterface $logger
Nazar65 marked this conversation as resolved.
Show resolved Hide 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
Nazar65 marked this conversation as resolved.
Show resolved Hide resolved
*/
public function sendErrors($errors)
Nazar65 marked this conversation as resolved.
Show resolved Hide 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;

/**
Nazar65 marked this conversation as resolved.
Show resolved Hide 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;
sidolov marked this conversation as resolved.
Show resolved Hide 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
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
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
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);
}
}
}