-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Conversation
Hi @Nazar65. Thank you for your contribution
For more details, please, review the Magento Contributor Assistant documentation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Nazar65 thanks for the contribution. Please see the code review notes.
@@ -67,13 +69,21 @@ class Observer | |||
protected $inlineTranslation; | |||
|
|||
/** | |||
* @var \Magento\Store\Model\App\Emulation $appEmulation | |||
*/ | |||
protected $appEmulation; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please change the access level to private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
* @param \Magento\Store\Model\StoreManagerInterface $storeManager | ||
* @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder | ||
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation | ||
*/ | ||
public function __construct( | ||
Emulation $appEmulation = null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add move the new parameter to the last position in the parameters list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
@sivaschenko all done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Nazar65 thanks for your updates, please see remaining review notes
\Magento\Store\Model\StoreManagerInterface $storeManager, | ||
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, | ||
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation | ||
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, | ||
Emulation $appEmulation |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@@ -112,9 +123,17 @@ public function scheduledGenerateSitemaps() | |||
foreach ($collection as $sitemap) { | |||
/* @var $sitemap \Magento\Sitemap\Model\Sitemap */ | |||
try { | |||
$this->appEmulation->startEnvironmentEmulation( | |||
$sitemap->getStoreId(), | |||
'frontend', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the constant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Class have many dependency, so if i will use constant the codeMess test will fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case it's better to decompose the class in favour to dropping constant usage
Co-Authored-By: Nazar65 <[email protected]>
@sivaschenko please look at comments. |
Hi @Nazar65 , I think the best way to resolve the problems with static tests and keep the good code style and backward compatibility - is extracting all the business logic to a model from the observer. Actually, this observer is a very good candidate for refactoring. Could you extract it? |
@sivaschenko yes i agree with you. I'm will work on this :) |
# Conflicts: # app/code/Magento/Sitemap/Model/Observer.php
Hi @sivaschenko can you please look at newly commit ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Nazar65 , thanks for your refactoring! It looks great, however, I have some imporvement suggestions. Please see my code review notes.
@@ -24,21 +31,6 @@ class Observer | |||
*/ | |||
const XML_PATH_CRON_EXPR = 'crontab/default/jobs/generate_sitemaps/schedule/cron_expr'; | |||
|
|||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep all existing constants in the Observer as they could be already used by third-party extensions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
*/ | ||
protected $_transportBuilder; | ||
protected $_storeManager; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd change all properties to private and remove the "_" prefix (for consistency and to be compatible with PSR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
$transport->sendMessage(); | ||
|
||
$this->inlineTranslation->resume(); | ||
$this->sitemapEmail->sendErrorEmail($errors); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please call this method after the foreach (as all the errors have to be accumulated in $errors
first)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
self::XML_PATH_ERROR_RECIPIENT, | ||
ScopeInterface::SCOPE_STORE | ||
); | ||
if ($errors && $recipient) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move this condition to the observer or to a separate method, to keep this method solid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
* | ||
* @var \Magento\Framework\App\Config\ScopeConfigInterface | ||
*/ | ||
private $_scopeConfig; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Property should not be prefixed with a single underscore, see https://www.php-fig.org/psr/psr-2/#42-properties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -0,0 +1,109 @@ | |||
<?php |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use strict mode in for all new PHP files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation | ||
ScopeConfigInterface $scopeConfig, | ||
CollectionFactory $collectionFactory, | ||
StoreManagerInterface $storeManager, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neither of two classes appears to be using store manager, so I think this dependency can be removed from both of them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
if ($errors && $recipient) { | ||
$this->inlineTranslation->suspend(); | ||
|
||
$this->_transportBuilder->setTemplateIdentifier( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it may be beneficial to wrap the email send operation in a try-catch block to gracefully handle situations when a mail server is not responsive.
There is an example here
magento2/app/code/Magento/Contact/Model/Mail.php
Lines 68 to 87 in f77f96b
$this->inlineTranslation->suspend(); | |
try { | |
$transport = $this->transportBuilder | |
->setTemplateIdentifier($this->contactsConfig->emailTemplate()) | |
->setTemplateOptions( | |
[ | |
'area' => Area::AREA_FRONTEND, | |
'store' => $this->storeManager->getStore()->getId() | |
] | |
) | |
->setTemplateVars($variables) | |
->setFrom($this->contactsConfig->emailSender()) | |
->addTo($this->contactsConfig->emailRecipient()) | |
->setReplyTo($replyTo, $replyToName) | |
->getTransport(); | |
$transport->sendMessage(); | |
} finally { | |
$this->inlineTranslation->resume(); | |
} |
However, I would improve the approach by catching an exception and writing it to an exception log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
/** | ||
* Sends emails for the scheduled generation of the sitemap file | ||
*/ | ||
class SitemapSendEmail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rename the class to the something like EmailNotification
or Mail
and the method to send
or sendErrors
to avoid duplicate wording in full method reference. Currently it's Magento\Sitemap\Model\SitemapSendEmail::sendErrorEmail
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
Hi @sivaschenko all done. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Nazar65 thanks for the refactoring! I've added some code suggestions. You can simply add all my suggestions to batch and commit them.
@sivaschenko all done, thank you for the review :) |
Thank you @Nazar65 finally there are currently 14 commits, would you like to squash them into a single one? |
@sivaschenko sorry for that, now i "squash" to one commit. |
Hi @sivaschenko, thank you for the review. |
@sivaschenko @sidolov so do I change the variables again? |
Hi @sidolov, thank you for the review. |
HI @sivaschenko @sidolov any updates ? |
Hi @Nazar65 the pull request is in queue for delivery to mainline |
Hi @Nazar65, thank you for your contribution! |
… multistore on Schedule #19598
Description (*)
Images in XML sitemap are always linked to base store in multistore on Schedule
Fixed Issues (if relevant)
Manual testing scenarios (*)
Website URL 1: https://my_website.com
Website URL 2: https://my_website.co.uk
Got to Stores->Configuration->Catalog->XML Sitemap->General Settings:
2.Set to Enable, Start Time, Frequency Daily etc...
TIP: I've set to a few minutes before checking!
Website 1 should have images url referencing Website 1 domain = https://my_website.com
Contribution checklist (*)