Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Observer.php
Go to the documentation of this file.
1 <?php
13 namespace Magento\Directory\Model;
14 
15 class Observer
16 {
17  const CRON_STRING_PATH = 'crontab/default/jobs/currency_rates_update/schedule/cron_expr';
18 
19  const IMPORT_ENABLE = 'currency/import/enabled';
20 
21  const IMPORT_SERVICE = 'currency/import/service';
22 
23  const XML_PATH_ERROR_TEMPLATE = 'currency/import/error_email_template';
24 
25  const XML_PATH_ERROR_IDENTITY = 'currency/import/error_email_identity';
26 
27  const XML_PATH_ERROR_RECIPIENT = 'currency/import/error_email';
28 
32  protected $_importFactory;
33 
39  protected $_scopeConfig;
40 
44  protected $_transportBuilder;
45 
49  protected $_storeManager;
50 
54  protected $_currencyFactory;
55 
59  protected $inlineTranslation;
60 
69  public function __construct(
70  \Magento\Directory\Model\Currency\Import\Factory $importFactory,
71  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
72  \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
74  \Magento\Directory\Model\CurrencyFactory $currencyFactory,
75  \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
76  ) {
77  $this->_importFactory = $importFactory;
78  $this->_scopeConfig = $scopeConfig;
79  $this->_transportBuilder = $transportBuilder;
80  $this->_storeManager = $storeManager;
81  $this->_currencyFactory = $currencyFactory;
82  $this->inlineTranslation = $inlineTranslation;
83  }
84 
91  public function scheduledUpdateCurrencyRates($schedule)
92  {
93  $importWarnings = [];
94  if (!$this->_scopeConfig->getValue(
95  self::IMPORT_ENABLE,
96  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
97  ) || !$this->_scopeConfig->getValue(
98  self::CRON_STRING_PATH,
99  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
100  )
101  ) {
102  return;
103  }
104 
105  $errors = [];
106  $rates = [];
107  $service = $this->_scopeConfig->getValue(
108  self::IMPORT_SERVICE,
109  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
110  );
111  if ($service) {
112  try {
113  $importModel = $this->_importFactory->create($service);
114  $rates = $importModel->fetchRates();
115  $errors = $importModel->getMessages();
116  } catch (\Exception $e) {
117  $importWarnings[] = __('FATAL ERROR:') . ' '
118  . __("The import model can't be initialized. Verify the model and try again.");
119  throw $e;
120  }
121  } else {
122  $importWarnings[] = __('FATAL ERROR:') . ' ' . __('Please specify the correct Import Service.');
123  }
124 
125  if (sizeof($errors) > 0) {
126  foreach ($errors as $error) {
127  $importWarnings[] = __('WARNING:') . ' ' . $error;
128  }
129  }
130 
131  $errorRecipient = $this->_scopeConfig->getValue(
132  self::XML_PATH_ERROR_RECIPIENT,
133  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
134  );
135  if (sizeof($importWarnings) == 0) {
136  $this->_currencyFactory->create()->saveRates($rates);
137  } elseif ($errorRecipient) {
138  //if $errorRecipient is not set, there is no sense send email to nobody
139  $this->inlineTranslation->suspend();
140 
141  $this->_transportBuilder->setTemplateIdentifier(
142  $this->_scopeConfig->getValue(
143  self::XML_PATH_ERROR_TEMPLATE,
144  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
145  )
146  )->setTemplateOptions(
147  [
148  'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
149  'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
150  ]
151  )->setTemplateVars(
152  ['warnings' => join("\n", $importWarnings)]
153  )->setFrom(
154  $this->_scopeConfig->getValue(
155  self::XML_PATH_ERROR_IDENTITY,
156  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
157  )
158  )->addTo($errorRecipient);
159  $transport = $this->_transportBuilder->getTransport();
160  $transport->sendMessage();
161 
162  $this->inlineTranslation->resume();
163  }
164  }
165 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
__()
Definition: __.php:13
$rates
Definition: tax.phtml:35
__construct(\Magento\Directory\Model\Currency\Import\Factory $importFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation)
Definition: Observer.php:69
scheduledUpdateCurrencyRates($schedule)
Definition: Observer.php:91
$errors
Definition: overview.phtml:9