Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Attributes
Observer Class Reference

Public Member Functions

 __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)
 
 scheduledUpdateCurrencyRates ($schedule)
 

Data Fields

const CRON_STRING_PATH = 'crontab/default/jobs/currency_rates_update/schedule/cron_expr'
 
const IMPORT_ENABLE = 'currency/import/enabled'
 
const IMPORT_SERVICE = 'currency/import/service'
 
const XML_PATH_ERROR_TEMPLATE = 'currency/import/error_email_template'
 
const XML_PATH_ERROR_IDENTITY = 'currency/import/error_email_identity'
 
const XML_PATH_ERROR_RECIPIENT = 'currency/import/error_email'
 

Protected Attributes

 $_importFactory
 
 $_scopeConfig
 
 $_transportBuilder
 
 $_storeManager
 
 $_currencyFactory
 
 $inlineTranslation
 

Detailed Description

Definition at line 15 of file Observer.php.

Constructor & Destructor Documentation

◆ __construct()

__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 
)
Parameters
\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 at line 69 of file Observer.php.

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  }
$storeManager

Member Function Documentation

◆ scheduledUpdateCurrencyRates()

scheduledUpdateCurrencyRates (   $schedule)
Parameters
mixed$schedule
Returns
void
Exceptions

Definition at line 91 of file Observer.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$rates
Definition: tax.phtml:35
$errors
Definition: overview.phtml:9

Field Documentation

◆ $_currencyFactory

$_currencyFactory
protected

Definition at line 54 of file Observer.php.

◆ $_importFactory

$_importFactory
protected

Definition at line 32 of file Observer.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 39 of file Observer.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 49 of file Observer.php.

◆ $_transportBuilder

$_transportBuilder
protected

Definition at line 44 of file Observer.php.

◆ $inlineTranslation

$inlineTranslation
protected

Definition at line 59 of file Observer.php.

◆ CRON_STRING_PATH

const CRON_STRING_PATH = 'crontab/default/jobs/currency_rates_update/schedule/cron_expr'

Definition at line 17 of file Observer.php.

◆ IMPORT_ENABLE

const IMPORT_ENABLE = 'currency/import/enabled'

Definition at line 19 of file Observer.php.

◆ IMPORT_SERVICE

const IMPORT_SERVICE = 'currency/import/service'

Definition at line 21 of file Observer.php.

◆ XML_PATH_ERROR_IDENTITY

const XML_PATH_ERROR_IDENTITY = 'currency/import/error_email_identity'

Definition at line 25 of file Observer.php.

◆ XML_PATH_ERROR_RECIPIENT

const XML_PATH_ERROR_RECIPIENT = 'currency/import/error_email'

Definition at line 27 of file Observer.php.

◆ XML_PATH_ERROR_TEMPLATE

const XML_PATH_ERROR_TEMPLATE = 'currency/import/error_email_template'

Definition at line 23 of file Observer.php.


The documentation for this class was generated from the following file: