Skip to content

Commit fda07a2

Browse files
committed
Refactoring Sitemap Observer
1 parent 16f11f7 commit fda07a2

File tree

2 files changed

+132
-75
lines changed

2 files changed

+132
-75
lines changed

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

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

88
use Magento\Store\Model\App\Emulation;
9+
use Magento\Sitemap\Model\SitemapSendEmail as SitemapEmail;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use ResourceModel\Sitemap\CollectionFactory;
13+
use Magento\Store\Model\ScopeInterface;
914

1015
/**
1116
* Sitemap module observer
@@ -26,21 +31,6 @@ class Observer
2631
*/
2732
const XML_PATH_CRON_EXPR = 'crontab/default/jobs/generate_sitemaps/schedule/cron_expr';
2833

29-
/**
30-
* Error email template configuration
31-
*/
32-
const XML_PATH_ERROR_TEMPLATE = 'sitemap/generate/error_email_template';
33-
34-
/**
35-
* Error email identity configuration
36-
*/
37-
const XML_PATH_ERROR_IDENTITY = 'sitemap/generate/error_email_identity';
38-
39-
/**
40-
* 'Send error emails to' configuration
41-
*/
42-
const XML_PATH_ERROR_RECIPIENT = 'sitemap/generate/error_email';
43-
4434
/**
4535
* Core store config
4636
*
@@ -53,49 +43,41 @@ class Observer
5343
*/
5444
protected $_collectionFactory;
5545

56-
/**
57-
* @var \Magento\Framework\Mail\Template\TransportBuilder
58-
*/
59-
protected $_transportBuilder;
60-
6146
/**
6247
* @var \Magento\Store\Model\StoreManagerInterface
6348
*/
6449
protected $_storeManager;
6550

6651
/**
67-
* @var \Magento\Framework\Translate\Inline\StateInterface
52+
* @var Emulation
6853
*/
69-
protected $inlineTranslation;
54+
private $appEmulation;
7055

7156
/**
72-
* @var \Magento\Store\Model\App\Emulation $appEmulation
57+
* @var $sitemapEmail
7358
*/
74-
private $appEmulation;
59+
private $sitemapEmail;
7560

7661
/**
7762
* Observer constructor.
78-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
79-
* @param ResourceModel\Sitemap\CollectionFactory $collectionFactory
80-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
81-
* @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
82-
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
83-
* @param Emulation|null $appEmulation
63+
* @param ScopeConfigInterface $scopeConfig
64+
* @param CollectionFactory $collectionFactory
65+
* @param StoreManagerInterface $storeManager
66+
* @param SitemapSendEmail $sitemapEmail
67+
* @param Emulation $appEmulation
8468
*/
8569
public function __construct(
86-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
87-
ResourceModel\Sitemap\CollectionFactory $collectionFactory,
88-
\Magento\Store\Model\StoreManagerInterface $storeManager,
89-
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
90-
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
70+
ScopeConfigInterface $scopeConfig,
71+
CollectionFactory $collectionFactory,
72+
StoreManagerInterface $storeManager,
73+
SitemapEmail $sitemapEmail,
9174
Emulation $appEmulation
9275
) {
9376
$this->_scopeConfig = $scopeConfig;
9477
$this->_collectionFactory = $collectionFactory;
9578
$this->_storeManager = $storeManager;
96-
$this->_transportBuilder = $transportBuilder;
97-
$this->inlineTranslation = $inlineTranslation;
9879
$this->appEmulation = $appEmulation;
80+
$this->sitemapEmail = $sitemapEmail;
9981
}
10082

10183
/**
@@ -112,7 +94,7 @@ public function scheduledGenerateSitemaps()
11294
// check if scheduled generation enabled
11395
if (!$this->_scopeConfig->isSetFlag(
11496
self::XML_PATH_GENERATION_ENABLED,
115-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
97+
ScopeInterface::SCOPE_STORE
11698
)
11799
) {
118100
return;
@@ -125,52 +107,18 @@ public function scheduledGenerateSitemaps()
125107
try {
126108
$this->appEmulation->startEnvironmentEmulation(
127109
$sitemap->getStoreId(),
128-
'frontend',
110+
\Magento\Framework\App\Area::AREA_FRONTEND,
129111
true
130112
);
131113

132114
$sitemap->generateXml();
133115
} catch (\Exception $e) {
134116
$errors[] = $e->getMessage();
117+
118+
$this->sitemapEmail->sendErrorEmail($errors);
135119
} finally {
136120
$this->appEmulation->stopEnvironmentEmulation();
137121
}
138122
}
139-
140-
if ($errors && $this->_scopeConfig->getValue(
141-
self::XML_PATH_ERROR_RECIPIENT,
142-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
143-
)
144-
) {
145-
$this->inlineTranslation->suspend();
146-
147-
$this->_transportBuilder->setTemplateIdentifier(
148-
$this->_scopeConfig->getValue(
149-
self::XML_PATH_ERROR_TEMPLATE,
150-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
151-
)
152-
)->setTemplateOptions(
153-
[
154-
'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
155-
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
156-
]
157-
)->setTemplateVars(
158-
['warnings' => join("\n", $errors)]
159-
)->setFrom(
160-
$this->_scopeConfig->getValue(
161-
self::XML_PATH_ERROR_IDENTITY,
162-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
163-
)
164-
)->addTo(
165-
$this->_scopeConfig->getValue(
166-
self::XML_PATH_ERROR_RECIPIENT,
167-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
168-
)
169-
);
170-
$transport = $this->_transportBuilder->getTransport();
171-
$transport->sendMessage();
172-
173-
$this->inlineTranslation->resume();
174-
}
175123
}
176124
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Sitemap\Model;
8+
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\Translate\Inline\StateInterface;
11+
use Magento\Framework\Mail\Template\TransportBuilder;
12+
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Backend\App\Area\FrontNameResolver;
14+
use Magento\Store\Model\Store;
15+
16+
/**
17+
* Sends emails for the scheduled generation of the sitemap file
18+
*/
19+
class SitemapSendEmail
20+
{
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+
36+
/**
37+
* @var \Magento\Framework\Translate\Inline\StateInterface
38+
*/
39+
private $inlineTranslation;
40+
41+
/**
42+
* Core store config
43+
*
44+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
45+
*/
46+
private $_scopeConfig;
47+
48+
/**
49+
* @var \Magento\Framework\Mail\Template\TransportBuilder
50+
*/
51+
private $_transportBuilder;
52+
53+
/**
54+
* SitemapSendEmail constructor.
55+
* @param StateInterface $inlineTranslation
56+
* @param TransportBuilder $transportBuilder
57+
* @param ScopeConfigInterface $scopeConfig
58+
*/
59+
public function __construct(
60+
StateInterface $inlineTranslation,
61+
TransportBuilder $transportBuilder,
62+
ScopeConfigInterface $scopeConfig
63+
) {
64+
$this->inlineTranslation = $inlineTranslation;
65+
$this->_scopeConfig = $scopeConfig;
66+
$this->_transportBuilder = $transportBuilder;
67+
}
68+
69+
/**
70+
* Send's error email if sitemap generated with errors.
71+
*
72+
* @param array $errors
73+
* @throws \Magento\Framework\Exception\MailException
74+
*/
75+
public function sendErrorEmail($errors)
76+
{
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,
87+
ScopeInterface::SCOPE_STORE
88+
)
89+
)->setTemplateOptions(
90+
[
91+
'area' => FrontNameResolver::AREA_CODE,
92+
'store' => Store::DEFAULT_STORE_ID,
93+
]
94+
)->setTemplateVars(
95+
['warnings' => join("\n", $errors)]
96+
)->setFrom(
97+
$this->_scopeConfig->getValue(
98+
self::XML_PATH_ERROR_IDENTITY,
99+
ScopeInterface::SCOPE_STORE
100+
)
101+
)->addTo($recipient);
102+
103+
$transport = $this->_transportBuilder->getTransport();
104+
$transport->sendMessage();
105+
106+
$this->inlineTranslation->resume();
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)