This repository has been archived by the owner on Apr 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ConfigSaveObserver.php
211 lines (190 loc) · 6.68 KB
/
ConfigSaveObserver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* ClassyLlama_AvaTax
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @copyright Copyright (c) 2016 Avalara, Inc.
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
namespace ClassyLlama\AvaTax\Observer;
use ClassyLlama\AvaTax\Helper\Config;
use Magento\Framework\Event\ObserverInterface;
/**
* Class ConfigSaveObserver
*/
class ConfigSaveObserver implements ObserverInterface
{
/**
* @var Config
*/
protected $config = null;
/**
* @var \Magento\Framework\Message\ManagerInterface
*/
protected $messageManager;
/**
* @var \ClassyLlama\AvaTax\Helper\ModuleChecks
*/
protected $moduleChecks;
/**
* @var \ClassyLlama\AvaTax\Framework\Interaction\Tax
*/
protected $interactionTax;
/**
* Constructor
*
* @param \Magento\Framework\Message\ManagerInterface $messageManager
* @param Config $config
* @param \ClassyLlama\AvaTax\Helper\ModuleChecks $moduleChecks
* @param \ClassyLlama\AvaTax\Framework\Interaction\Tax $interactionTax
*/
public function __construct(
\Magento\Framework\Message\ManagerInterface $messageManager,
Config $config,
\ClassyLlama\AvaTax\Helper\ModuleChecks $moduleChecks,
\ClassyLlama\AvaTax\Framework\Interaction\Tax $interactionTax
) {
$this->messageManager = $messageManager;
$this->config = $config;
$this->moduleChecks = $moduleChecks;
$this->interactionTax = $interactionTax;
}
/**
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if ($observer->getStore()) {
$scopeId = $observer->getStore();
$scopeType = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
} elseif ($observer->getWebsite()) {
$scopeId = $observer->getWebsite();
$scopeType = \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE;
} else {
$scopeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
$scopeType = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
}
foreach ($this->getErrors($scopeId, $scopeType) as $error) {
$this->messageManager->addError($error);
}
foreach ($this->getNotices() as $notice) {
$this->messageManager->addNotice($notice);
}
return $this;
}
/**
* Get all errors that should display when tax config is saved
*
* @param $scopeId
* @param $scopeType
* @return array
*/
protected function getErrors($scopeId, $scopeType)
{
$errors = array();
$errors = array_merge(
$errors,
$this->sendPing($scopeId, $scopeType)
);
return $errors;
}
/**
* Get all notices that should display when tax config is saved
*
* @return array
*/
protected function getNotices()
{
$notices = array();
$notices = array_merge(
$notices,
// This check is also being displayed at the top of the page via
// \ClassyLlama\AvaTax\Model\Message\ConfigNotification, but it's not as visible as a notice message, so
// also add it as a notice.
$this->moduleChecks->checkNativeTaxRules()
);
return $notices;
}
/**
* Ping AvaTax using configured live/production mode
*
* @param $scopeId
* @param $scopeType
* @return array
*/
protected function sendPing($scopeId, $scopeType)
{
$errors = [];
if (!$this->config->isModuleEnabled($scopeId, $scopeType)) {
return $errors;
}
$message = '';
$type = $this->config->getLiveMode($scopeId, $scopeType)
? Config::API_PROFILE_NAME_PROD : Config::API_PROFILE_NAME_DEV;
if ($this->checkCredentialsForMode($scopeId, $scopeType, $type)) {
try {
$result = $this->interactionTax->getTaxService($type, $scopeId, $scopeType)->ping();
if (is_object($result) && $result->getResultCode() != \AvaTax\SeverityLevel::$Success) {
foreach ($result->getMessages() as $messages) {
$message .= $messages->getName() . ': ' . $messages->getSummary() . "\n";
}
} elseif (is_object($result) && $result->getResultCode() == \AvaTax\SeverityLevel::$Success) {
$this->messageManager->addSuccess(
__('Successfully connected to AvaTax using the '
. '<a href="#row_tax_avatax_connection_settings_header">%1 credentials</a>.', $type
)
);
}
} catch (\Exception $exception) {
$message = $exception->getMessage();
}
if ($message) {
$errors[] = __('Error connecting to AvaTax using the '
. '<a href="#row_tax_avatax_connection_settings_header">%1 credentials</a>: %2', $type, $message);
}
}
return $errors;
}
/**
* Check that credentials have been set for the supplied mode
*
* @param $scopeId
* @param $scopeType
* @param $mode
* @return bool
*/
protected function checkCredentialsForMode($scopeId, $scopeType, $mode)
{
// Check that credentials have been set for whichever mode has been chosen
if ($mode == Config::API_PROFILE_NAME_PROD) {
if (
$this->config->getAccountNumber($scopeId, $scopeType) != ''
&& $this->config->getLicenseKey($scopeId, $scopeType) != ''
&& $this->config->getCompanyCode($scopeId, $scopeType) != ''
) {
return true;
}
} else {
if (
$this->config->getDevelopmentAccountNumber($scopeId, $scopeType) != ''
&& $this->config->getDevelopmentLicenseKey($scopeId, $scopeType) != ''
&& $this->config->getDevelopmentCompanyCode($scopeId, $scopeType) != ''
) {
return true;
}
}
// One or more of the supplied mode's credentials is blank
$this->messageManager->addWarningMessage(
__('The AvaTax extension is set to "%1" mode, but %2 credentials are incomplete.', $mode, strtolower($mode))
);
return false;
}
}