Observer.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Directory module observer
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Directory\Model;
  12. class Observer
  13. {
  14. const CRON_STRING_PATH = 'crontab/default/jobs/currency_rates_update/schedule/cron_expr';
  15. const IMPORT_ENABLE = 'currency/import/enabled';
  16. const IMPORT_SERVICE = 'currency/import/service';
  17. const XML_PATH_ERROR_TEMPLATE = 'currency/import/error_email_template';
  18. const XML_PATH_ERROR_IDENTITY = 'currency/import/error_email_identity';
  19. const XML_PATH_ERROR_RECIPIENT = 'currency/import/error_email';
  20. /**
  21. * @var \Magento\Directory\Model\Currency\Import\Factory
  22. */
  23. protected $_importFactory;
  24. /**
  25. * Core store config
  26. *
  27. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  28. */
  29. protected $_scopeConfig;
  30. /**
  31. * @var \Magento\Framework\Mail\Template\TransportBuilder
  32. */
  33. protected $_transportBuilder;
  34. /**
  35. * @var \Magento\Store\Model\StoreManagerInterface
  36. */
  37. protected $_storeManager;
  38. /**
  39. * @var \Magento\Directory\Model\CurrencyFactory
  40. */
  41. protected $_currencyFactory;
  42. /**
  43. * @var \Magento\Framework\Translate\Inline\StateInterface
  44. */
  45. protected $inlineTranslation;
  46. /**
  47. * @param \Magento\Directory\Model\Currency\Import\Factory $importFactory
  48. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  49. * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
  50. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  51. * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
  52. * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
  53. */
  54. public function __construct(
  55. \Magento\Directory\Model\Currency\Import\Factory $importFactory,
  56. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  57. \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
  58. \Magento\Store\Model\StoreManagerInterface $storeManager,
  59. \Magento\Directory\Model\CurrencyFactory $currencyFactory,
  60. \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
  61. ) {
  62. $this->_importFactory = $importFactory;
  63. $this->_scopeConfig = $scopeConfig;
  64. $this->_transportBuilder = $transportBuilder;
  65. $this->_storeManager = $storeManager;
  66. $this->_currencyFactory = $currencyFactory;
  67. $this->inlineTranslation = $inlineTranslation;
  68. }
  69. /**
  70. * @param mixed $schedule
  71. * @return void
  72. * @throws \Exception
  73. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  74. */
  75. public function scheduledUpdateCurrencyRates($schedule)
  76. {
  77. $importWarnings = [];
  78. if (!$this->_scopeConfig->getValue(
  79. self::IMPORT_ENABLE,
  80. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  81. ) || !$this->_scopeConfig->getValue(
  82. self::CRON_STRING_PATH,
  83. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  84. )
  85. ) {
  86. return;
  87. }
  88. $errors = [];
  89. $rates = [];
  90. $service = $this->_scopeConfig->getValue(
  91. self::IMPORT_SERVICE,
  92. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  93. );
  94. if ($service) {
  95. try {
  96. $importModel = $this->_importFactory->create($service);
  97. $rates = $importModel->fetchRates();
  98. $errors = $importModel->getMessages();
  99. } catch (\Exception $e) {
  100. $importWarnings[] = __('FATAL ERROR:') . ' '
  101. . __("The import model can't be initialized. Verify the model and try again.");
  102. throw $e;
  103. }
  104. } else {
  105. $importWarnings[] = __('FATAL ERROR:') . ' ' . __('Please specify the correct Import Service.');
  106. }
  107. if (sizeof($errors) > 0) {
  108. foreach ($errors as $error) {
  109. $importWarnings[] = __('WARNING:') . ' ' . $error;
  110. }
  111. }
  112. $errorRecipient = $this->_scopeConfig->getValue(
  113. self::XML_PATH_ERROR_RECIPIENT,
  114. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  115. );
  116. if (sizeof($importWarnings) == 0) {
  117. $this->_currencyFactory->create()->saveRates($rates);
  118. } elseif ($errorRecipient) {
  119. //if $errorRecipient is not set, there is no sense send email to nobody
  120. $this->inlineTranslation->suspend();
  121. $this->_transportBuilder->setTemplateIdentifier(
  122. $this->_scopeConfig->getValue(
  123. self::XML_PATH_ERROR_TEMPLATE,
  124. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  125. )
  126. )->setTemplateOptions(
  127. [
  128. 'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
  129. 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
  130. ]
  131. )->setTemplateVars(
  132. ['warnings' => join("\n", $importWarnings)]
  133. )->setFrom(
  134. $this->_scopeConfig->getValue(
  135. self::XML_PATH_ERROR_IDENTITY,
  136. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  137. )
  138. )->addTo($errorRecipient);
  139. $transport = $this->_transportBuilder->getTransport();
  140. $transport->sendMessage();
  141. $this->inlineTranslation->resume();
  142. }
  143. }
  144. }