1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2013-2014 eBay Enterprise, Inc. |
4
|
|
|
* |
5
|
|
|
* NOTICE OF LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
8
|
|
|
* that is bundled with this package in the file LICENSE.md. |
9
|
|
|
* It is also available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* @copyright Copyright (c) 2013-2014 eBay Enterprise, Inc. (http://www.ebayenterprise.com/) |
13
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Observer for gift card events in the admin. |
18
|
|
|
*/ |
19
|
|
|
class EbayEnterprise_GiftCard_Model_Adminhtml_Observer |
20
|
|
|
{ |
21
|
|
|
// post data fields for gift cards |
22
|
|
|
const CARD_NUMBER_PARAM = 'ebay_enterprise_giftcard_code'; |
23
|
|
|
const CARD_PIN_PARAM = 'ebay_enterprise_giftcard_pin'; |
24
|
|
|
const ACTION_PARAM = 'ebay_enterprise_giftcard_action'; |
25
|
|
|
// action "flags" expected to be sent in the post data |
26
|
|
|
const ADD_ACTION = 'add'; |
27
|
|
|
const REMOVE_ACTION = 'remove'; |
28
|
|
|
/** @var array post data */ |
29
|
|
|
protected $request; |
30
|
|
|
/** @var EbayEnterprise_GiftCard_Model_IContainer */ |
31
|
|
|
protected $container; |
32
|
|
|
/** @var EbayEnterprise_GiftCard_Helper_Data */ |
33
|
|
|
protected $helper; |
34
|
|
|
/** @var EbayEnterprise_MageLog_Helper_Data */ |
35
|
|
|
protected $logger; |
36
|
|
|
/** @var EbayEnterprise_MageLog_Helper_Context */ |
37
|
|
|
protected $logContext; |
38
|
|
|
/** @var Mage_Adminhtml_Model_Session_Quote */ |
39
|
|
|
protected $session; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function __construct(array $args = []) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
list( |
47
|
|
|
$this->container, |
48
|
|
|
$this->helper, |
49
|
|
|
$this->logger, |
50
|
|
|
$this->logContext, |
51
|
|
|
$this->session |
52
|
|
|
) = $this->checkTypes( |
53
|
|
|
$this->nullCoalesce($args, 'container', Mage::getModel('ebayenterprise_giftcard/container')), |
54
|
|
|
$this->nullCoalesce($args, 'helper', Mage::helper('ebayenterprise_giftcard')), |
55
|
|
|
$this->nullCoalesce($args, 'logger', Mage::helper('ebayenterprise_magelog')), |
56
|
|
|
$this->nullCoalesce($args, 'log_context', Mage::helper('ebayenterprise_magelog/context')), |
57
|
|
|
$this->nullCoalesce($args, 'session', null) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param EbayEnterprise_GiftCard_Model_IContainer |
63
|
|
|
* @param EbayEnterprise_GiftCard_Helper_Data |
64
|
|
|
* @param EbayEnterprise_MageLog_Helper_Data |
65
|
|
|
* @param EbayEnterprise_MageLog_Helper_Context |
66
|
|
|
* @param Mage_Adminhtml_Model_Session_Quote|null |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected function checkTypes( |
|
|
|
|
70
|
|
|
EbayEnterprise_GiftCard_Model_IContainer $container, |
|
|
|
|
71
|
|
|
EbayEnterprise_GiftCard_Helper_Data $helper, |
|
|
|
|
72
|
|
|
EbayEnterprise_MageLog_Helper_Data $logger, |
|
|
|
|
73
|
|
|
EbayEnterprise_MageLog_Helper_Context $logContext, |
|
|
|
|
74
|
|
|
Mage_Adminhtml_Model_Session_Quote $session = null |
|
|
|
|
75
|
|
|
) { |
76
|
|
|
return func_get_args(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return the value at field in array if it exists. Otherwise, use the default value. |
81
|
|
|
* |
82
|
|
|
* @param array |
83
|
|
|
* @param string $field Valid array key |
84
|
|
|
* @param mixed |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
protected function nullCoalesce(array $arr, $field, $default) |
88
|
|
|
{ |
89
|
|
|
return isset($arr[$field]) ? $arr[$field] : $default; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Process post data and set usage of GC into order creation model |
94
|
|
|
* |
95
|
|
|
* @param Varien_Event_Observer $observer |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
|
|
public function processOrderCreationData(Varien_Event_Observer $observer) |
99
|
|
|
{ |
100
|
|
|
if ($this->helper->getConfigModel()->isEnabled) { |
|
|
|
|
101
|
|
|
$this->request = $observer->getEvent()->getRequest(); |
102
|
|
|
list($cardNumber, $pin) = $this->getCardInfoFromRequest(); |
103
|
|
|
if ($cardNumber) { |
104
|
|
|
$this->processCard($cardNumber, $pin); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Add or remove the gift card, depending on the requested action. |
112
|
|
|
* |
113
|
|
|
* @param string $cardNumber |
114
|
|
|
* @param string $pin |
115
|
|
|
* @return self |
116
|
|
|
*/ |
117
|
|
|
protected function processCard($cardNumber, $pin) |
118
|
|
|
{ |
119
|
|
|
if ($this->isAddRequest()) { |
120
|
|
|
$this->addGiftCard($cardNumber, $pin); |
121
|
|
|
} elseif ($this->isRemoveRequest()) { |
122
|
|
|
$this->removeGiftCard($cardNumber); |
123
|
|
|
} |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Is the gift card action param in the request for an add. |
129
|
|
|
* |
130
|
|
|
* @return boolean |
131
|
|
|
*/ |
132
|
|
|
protected function isAddRequest() |
133
|
|
|
{ |
134
|
|
|
return $this->getPostData(self::ACTION_PARAM, '') === self::ADD_ACTION; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Is the gift card action param in the request for a remove. |
139
|
|
|
* |
140
|
|
|
* @return boolean |
141
|
|
|
*/ |
142
|
|
|
protected function isRemoveRequest() |
143
|
|
|
{ |
144
|
|
|
return $this->getPostData(self::ACTION_PARAM, '') === self::REMOVE_ACTION; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* add a giftcard. |
149
|
|
|
* |
150
|
|
|
* @param string $cardNumber |
151
|
|
|
* @param string $pin |
152
|
|
|
* @return self |
153
|
|
|
*/ |
154
|
|
|
protected function addGiftCard($cardNumber, $pin) |
155
|
|
|
{ |
156
|
|
|
$giftcard = $this->container->getGiftCard($cardNumber)->setPin($pin); |
157
|
|
|
try { |
158
|
|
|
$this->helper->addGiftCardToOrder($giftcard, $this->container); |
159
|
|
|
$this->getSession()->addSuccess($this->helper->__(EbayEnterprise_GiftCard_Helper_Data::GIFT_CARD_ADD_SUCCESS, $cardNumber)); |
160
|
|
|
} catch (EbayEnterprise_GiftCard_Exception $e) { |
161
|
|
|
$this->getSession()->addError($this->helper->__($e->getMessage())); |
162
|
|
|
$this->logger->debug('Failed to add gift card to admin order. See exception log for more details.', $this->logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); |
163
|
|
|
$this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e)); |
164
|
|
|
} |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* remove a giftcard. |
170
|
|
|
* |
171
|
|
|
* @param string $cardNumber |
172
|
|
|
* @return self |
173
|
|
|
*/ |
174
|
|
|
protected function removeGiftCard($cardNumber) |
175
|
|
|
{ |
176
|
|
|
$giftcard = $this->container->getGiftCard($cardNumber); |
177
|
|
|
$this->container->removeGiftCard($giftcard); |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Extract the card number and pin from the request. If either is not present, |
183
|
|
|
* will return an empty string for that value. |
184
|
|
|
* |
185
|
|
|
* @return string[] Tuple of card number and pin |
186
|
|
|
*/ |
187
|
|
|
protected function getCardInfoFromRequest() |
188
|
|
|
{ |
189
|
|
|
return [$this->getPostData(self::CARD_NUMBER_PARAM, ''), $this->getPostData(self::CARD_PIN_PARAM, '')]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get post data from the request. If not set, return the default value. |
194
|
|
|
* |
195
|
|
|
* @param string|int $field |
196
|
|
|
* @param string $default |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
protected function getPostData($field, $default) |
200
|
|
|
{ |
201
|
|
|
return $this->nullCoalesce($this->request, $field, $default); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get the adminhtml quote session. |
206
|
|
|
* |
207
|
|
|
* @return Mage_Adminhtml_Model_Session_Quote |
208
|
|
|
*/ |
209
|
|
|
protected function getSession() |
210
|
|
|
{ |
211
|
|
|
if (!$this->session) { |
212
|
|
|
$this->session = Mage::getSingleton('adminhtml/session_quote'); |
213
|
|
|
} |
214
|
|
|
return $this->session; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.