Skip to content

Commit b88fa48

Browse files
committed
Refactoring
1 parent d2d04b6 commit b88fa48

File tree

2 files changed

+74
-59
lines changed

2 files changed

+74
-59
lines changed

app/code/Magento/Sitemap/Model/SitemapSendEmail.php renamed to app/code/Magento/Sitemap/Model/EmailNotification.php

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types = 1);
67

78
namespace Magento\Sitemap\Model;
89

@@ -11,28 +12,14 @@
1112
use Magento\Framework\Mail\Template\TransportBuilder;
1213
use Magento\Store\Model\ScopeInterface;
1314
use Magento\Backend\App\Area\FrontNameResolver;
14-
use Magento\Store\Model\Store;
15+
use Magento\Sitemap\Model\Observer as Observer;
16+
use Psr\Log\LoggerInterface;
1517

1618
/**
1719
* Sends emails for the scheduled generation of the sitemap file
1820
*/
19-
class SitemapSendEmail
21+
class EmailNotification
2022
{
21-
/**
22-
* Error email template configuration
23-
*/
24-
const XML_PATH_ERROR_TEMPLATE = 'sitemap/generate/error_email_template';
25-
26-
/**
27-
* Error email identity configuration
28-
*/
29-
const XML_PATH_ERROR_IDENTITY = 'sitemap/generate/error_email_identity';
30-
31-
/**
32-
* 'Send error emails to' configuration
33-
*/
34-
const XML_PATH_ERROR_RECIPIENT = 'sitemap/generate/error_email';
35-
3623
/**
3724
* @var \Magento\Framework\Translate\Inline\StateInterface
3825
*/
@@ -43,66 +30,75 @@ class SitemapSendEmail
4330
*
4431
* @var \Magento\Framework\App\Config\ScopeConfigInterface
4532
*/
46-
private $_scopeConfig;
33+
private $scopeConfig;
4734

4835
/**
4936
* @var \Magento\Framework\Mail\Template\TransportBuilder
5037
*/
51-
private $_transportBuilder;
38+
private $transportBuilder;
39+
40+
/**
41+
* @var $logger
42+
*/
43+
private $logger;
5244

5345
/**
54-
* SitemapSendEmail constructor.
46+
* EmailNotification constructor.
5547
* @param StateInterface $inlineTranslation
5648
* @param TransportBuilder $transportBuilder
5749
* @param ScopeConfigInterface $scopeConfig
50+
* @param LoggerInterface $logger
5851
*/
5952
public function __construct(
6053
StateInterface $inlineTranslation,
6154
TransportBuilder $transportBuilder,
62-
ScopeConfigInterface $scopeConfig
55+
ScopeConfigInterface $scopeConfig,
56+
LoggerInterface $logger
6357
) {
6458
$this->inlineTranslation = $inlineTranslation;
65-
$this->_scopeConfig = $scopeConfig;
66-
$this->_transportBuilder = $transportBuilder;
59+
$this->scopeConfig = $scopeConfig;
60+
$this->transportBuilder = $transportBuilder;
61+
$this->logger = $logger;
6762
}
6863

6964
/**
7065
* Send's error email if sitemap generated with errors.
7166
*
72-
* @param array $errors
73-
* @throws \Magento\Framework\Exception\MailException
67+
* @param array| $errors
7468
*/
75-
public function sendErrorEmail($errors)
69+
public function sendErrors($errors)
7670
{
77-
$recipient = $this->_scopeConfig->getValue(
78-
self::XML_PATH_ERROR_RECIPIENT,
79-
ScopeInterface::SCOPE_STORE
80-
);
81-
if ($errors && $recipient) {
82-
$this->inlineTranslation->suspend();
83-
84-
$this->_transportBuilder->setTemplateIdentifier(
85-
$this->_scopeConfig->getValue(
86-
self::XML_PATH_ERROR_TEMPLATE,
71+
$this->inlineTranslation->suspend();
72+
try {
73+
$this->transportBuilder->setTemplateIdentifier(
74+
$this->scopeConfig->getValue(
75+
Observer::XML_PATH_ERROR_TEMPLATE,
8776
ScopeInterface::SCOPE_STORE
8877
)
8978
)->setTemplateOptions(
9079
[
9180
'area' => FrontNameResolver::AREA_CODE,
92-
'store' => Store::DEFAULT_STORE_ID,
81+
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
9382
]
9483
)->setTemplateVars(
9584
['warnings' => join("\n", $errors)]
9685
)->setFrom(
97-
$this->_scopeConfig->getValue(
98-
self::XML_PATH_ERROR_IDENTITY,
86+
$this->scopeConfig->getValue(
87+
Observer::XML_PATH_ERROR_IDENTITY,
88+
ScopeInterface::SCOPE_STORE
89+
)
90+
)->addTo(
91+
$this->scopeConfig->getValue(
92+
Observer::XML_PATH_ERROR_RECIPIENT,
9993
ScopeInterface::SCOPE_STORE
10094
)
101-
)->addTo($recipient);
95+
);
10296

103-
$transport = $this->_transportBuilder->getTransport();
97+
$transport = $this->transportBuilder->getTransport();
10498
$transport->sendMessage();
105-
99+
} catch (\Exception $e) {
100+
$this->logger->error('Sitemap sendErrors: '.$e->getMessage());
101+
} finally {
106102
$this->inlineTranslation->resume();
107103
}
108104
}

app/code/Magento/Sitemap/Model/Observer.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Sitemap\Model;
77

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

34+
/**
35+
* Error email template configuration
36+
*/
37+
const XML_PATH_ERROR_TEMPLATE = 'sitemap/generate/error_email_template';
38+
39+
/**
40+
* Error email identity configuration
41+
*/
42+
const XML_PATH_ERROR_IDENTITY = 'sitemap/generate/error_email_identity';
43+
44+
/**
45+
* 'Send error emails to' configuration
46+
*/
47+
const XML_PATH_ERROR_RECIPIENT = 'sitemap/generate/error_email';
48+
3449
/**
3550
* Core store config
3651
*
3752
* @var \Magento\Framework\App\Config\ScopeConfigInterface
3853
*/
39-
protected $_scopeConfig;
54+
private $scopeConfig;
4055

4156
/**
4257
* @var \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory
4358
*/
44-
protected $_collectionFactory;
59+
private $collectionFactory;
4560

4661
/**
4762
* @var \Magento\Store\Model\StoreManagerInterface
4863
*/
49-
protected $_storeManager;
64+
private $storeManager;
5065

5166
/**
5267
* @var Emulation
5368
*/
5469
private $appEmulation;
5570

5671
/**
57-
* @var $sitemapEmail
72+
* @var $emailNotification
5873
*/
59-
private $sitemapEmail;
74+
private $emailNotification;
6075

6176
/**
6277
* Observer constructor.
6378
* @param ScopeConfigInterface $scopeConfig
6479
* @param CollectionFactory $collectionFactory
6580
* @param StoreManagerInterface $storeManager
66-
* @param SitemapSendEmail $sitemapEmail
81+
* @param EmailNotification $emailNotification
6782
* @param Emulation $appEmulation
6883
*/
6984
public function __construct(
7085
ScopeConfigInterface $scopeConfig,
7186
CollectionFactory $collectionFactory,
7287
StoreManagerInterface $storeManager,
73-
SitemapEmail $sitemapEmail,
88+
SitemapEmail $emailNotification,
7489
Emulation $appEmulation
7590
) {
76-
$this->_scopeConfig = $scopeConfig;
77-
$this->_collectionFactory = $collectionFactory;
78-
$this->_storeManager = $storeManager;
91+
$this->scopeConfig = $scopeConfig;
92+
$this->collectionFactory = $collectionFactory;
93+
$this->storeManager = $storeManager;
7994
$this->appEmulation = $appEmulation;
80-
$this->sitemapEmail = $sitemapEmail;
95+
$this->emailNotification = $emailNotification;
8196
}
8297

8398
/**
@@ -90,17 +105,20 @@ public function __construct(
90105
public function scheduledGenerateSitemaps()
91106
{
92107
$errors = [];
93-
108+
$recipient = $this->scopeConfig->getValue(
109+
Observer::XML_PATH_ERROR_RECIPIENT,
110+
ScopeInterface::SCOPE_STORE
111+
);
94112
// check if scheduled generation enabled
95-
if (!$this->_scopeConfig->isSetFlag(
113+
if (!$this->scopeConfig->isSetFlag(
96114
self::XML_PATH_GENERATION_ENABLED,
97115
ScopeInterface::SCOPE_STORE
98116
)
99117
) {
100118
return;
101119
}
102120

103-
$collection = $this->_collectionFactory->create();
121+
$collection = $this->collectionFactory->create();
104122
/* @var $collection \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection */
105123
foreach ($collection as $sitemap) {
106124
/* @var $sitemap \Magento\Sitemap\Model\Sitemap */
@@ -114,11 +132,12 @@ public function scheduledGenerateSitemaps()
114132
$sitemap->generateXml();
115133
} catch (\Exception $e) {
116134
$errors[] = $e->getMessage();
117-
118-
$this->sitemapEmail->sendErrorEmail($errors);
119135
} finally {
120136
$this->appEmulation->stopEnvironmentEmulation();
121137
}
122138
}
139+
if ($errors && $recipient) {
140+
$this->emailNotification->sendErrors($errors);
141+
}
123142
}
124143
}

0 commit comments

Comments
 (0)