123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sitemap\Model;
- /**
- * Sitemap module observer
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- class Observer
- {
- /**
- * Enable/disable configuration
- */
- const XML_PATH_GENERATION_ENABLED = 'sitemap/generate/enabled';
- /**
- * Cronjob expression configuration
- *
- * @deprecated Use \Magento\Cron\Model\Config\Backend\Sitemap::CRON_STRING_PATH instead.
- */
- const XML_PATH_CRON_EXPR = 'crontab/default/jobs/generate_sitemaps/schedule/cron_expr';
- /**
- * Error email template configuration
- */
- const XML_PATH_ERROR_TEMPLATE = 'sitemap/generate/error_email_template';
- /**
- * Error email identity configuration
- */
- const XML_PATH_ERROR_IDENTITY = 'sitemap/generate/error_email_identity';
- /**
- * 'Send error emails to' configuration
- */
- const XML_PATH_ERROR_RECIPIENT = 'sitemap/generate/error_email';
- /**
- * Core store config
- *
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $_scopeConfig;
- /**
- * @var \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory
- */
- protected $_collectionFactory;
- /**
- * @var \Magento\Framework\Mail\Template\TransportBuilder
- */
- protected $_transportBuilder;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var \Magento\Framework\Translate\Inline\StateInterface
- */
- protected $inlineTranslation;
- /**
- * @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
- */
- 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
- ) {
- $this->_scopeConfig = $scopeConfig;
- $this->_collectionFactory = $collectionFactory;
- $this->_storeManager = $storeManager;
- $this->_transportBuilder = $transportBuilder;
- $this->inlineTranslation = $inlineTranslation;
- }
- /**
- * Generate sitemaps
- *
- * @return void
- * @throws \Exception
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
- */
- public function scheduledGenerateSitemaps()
- {
- $errors = [];
- // check if scheduled generation enabled
- if (!$this->_scopeConfig->isSetFlag(
- self::XML_PATH_GENERATION_ENABLED,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- )
- ) {
- return;
- }
- $collection = $this->_collectionFactory->create();
- /* @var $collection \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection */
- foreach ($collection as $sitemap) {
- /* @var $sitemap \Magento\Sitemap\Model\Sitemap */
- try {
- $sitemap->generateXml();
- } catch (\Exception $e) {
- $errors[] = $e->getMessage();
- }
- }
- 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();
- }
- }
- }
|