TransactionMap.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model\Report\Row;
  7. use Braintree\Transaction;
  8. use DateTime;
  9. use Magento\Framework\Api\AttributeValueFactory;
  10. use Magento\Framework\Api\Search\DocumentInterface;
  11. /**
  12. * Class TransactionMap
  13. */
  14. class TransactionMap implements DocumentInterface
  15. {
  16. const TRANSACTION_FIELD_MAP_DELIMITER = '_';
  17. /**
  18. * @var AttributeValueFactory
  19. */
  20. private $attributeValueFactory;
  21. /**
  22. * @var Transaction
  23. */
  24. private $transaction;
  25. /**
  26. * @var array
  27. */
  28. public static $simpleFieldsMap = [
  29. 'id',
  30. 'merchantAccountId',
  31. 'orderId',
  32. 'paymentInstrumentType',
  33. 'paypalDetails_paymentId',
  34. 'type',
  35. 'createdAt',
  36. 'amount',
  37. 'processorSettlementResponseCode',
  38. 'status',
  39. 'processorSettlementResponseText',
  40. 'refundIds',
  41. 'settlementBatchId',
  42. 'currencyIsoCode'
  43. ];
  44. /**
  45. * @param AttributeValueFactory $attributeValueFactory
  46. * @param Transaction $transaction
  47. */
  48. public function __construct(
  49. AttributeValueFactory $attributeValueFactory,
  50. Transaction $transaction
  51. ) {
  52. $this->attributeValueFactory = $attributeValueFactory;
  53. $this->transaction = $transaction;
  54. }
  55. /**
  56. * Get Id
  57. *
  58. * @return string
  59. */
  60. public function getId()
  61. {
  62. return $this->getMappedValue('id');
  63. }
  64. /**
  65. * Set Id
  66. *
  67. * @param int $id
  68. * @return void
  69. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  70. */
  71. public function setId($id)
  72. {
  73. }
  74. /**
  75. * Get an attribute value.
  76. *
  77. * @param string $attributeCode
  78. * @return \Magento\Framework\Api\AttributeInterface|null
  79. */
  80. public function getCustomAttribute($attributeCode)
  81. {
  82. /** @var \Magento\Framework\Api\AttributeInterface $attributeValue */
  83. $attributeValue = $this->attributeValueFactory->create();
  84. $attributeValue->setAttributeCode($attributeCode);
  85. $attributeValue->setValue($this->getMappedValue($attributeCode));
  86. return $attributeValue;
  87. }
  88. /**
  89. * Set an attribute value for a given attribute code
  90. *
  91. * @param string $attributeCode
  92. * @param mixed $attributeValue
  93. * @return $this
  94. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  95. */
  96. public function setCustomAttribute($attributeCode, $attributeValue)
  97. {
  98. return $this;
  99. }
  100. /**
  101. * Retrieve custom attributes values.
  102. *
  103. * @return \Magento\Framework\Api\AttributeInterface[]|null
  104. */
  105. public function getCustomAttributes()
  106. {
  107. $shouldBeLocalized = ['paymentInstrumentType', 'type', 'status'];
  108. $output = [];
  109. foreach ($this->getMappedValues() as $key => $value) {
  110. $attribute = $this->attributeValueFactory->create();
  111. if (in_array($key, $shouldBeLocalized)) {
  112. $value = __($value);
  113. }
  114. $output[] = $attribute->setAttributeCode($key)->setValue($value);
  115. }
  116. return $output;
  117. }
  118. /**
  119. * Set array of custom attributes
  120. *
  121. * @param \Magento\Framework\Api\AttributeInterface[] $attributes
  122. * @return $this
  123. * @throws \LogicException
  124. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  125. */
  126. public function setCustomAttributes(array $attributes)
  127. {
  128. return $this;
  129. }
  130. /**
  131. * Get mapped value
  132. *
  133. * @param string $key
  134. * @return mixed
  135. */
  136. private function getMappedValue($key)
  137. {
  138. if (!in_array($key, static::$simpleFieldsMap)) {
  139. return null;
  140. }
  141. $val = $this->getTransactionFieldValue($key);
  142. $val = $this->convertToText($val);
  143. return $val;
  144. }
  145. /**
  146. * @return array
  147. */
  148. private function getMappedValues()
  149. {
  150. $result = [];
  151. foreach (static::$simpleFieldsMap as $key) {
  152. $val = $this->getTransactionFieldValue($key);
  153. $val = $this->convertToText($val);
  154. $result[$key] = $val;
  155. }
  156. return $result;
  157. }
  158. /**
  159. * Recursive get transaction field value
  160. *
  161. * @param string $key
  162. * @return Transaction|mixed|null
  163. */
  164. private function getTransactionFieldValue($key)
  165. {
  166. $keys = explode(self::TRANSACTION_FIELD_MAP_DELIMITER, $key);
  167. $result = $this->transaction;
  168. foreach ($keys as $k) {
  169. if (!isset($result->$k)) {
  170. $result = null;
  171. break;
  172. }
  173. $result = $result->$k;
  174. }
  175. return $result;
  176. }
  177. /**
  178. * Convert value to text representation
  179. *
  180. * @param string $val
  181. * @return string
  182. */
  183. private function convertToText($val)
  184. {
  185. if (is_object($val)) {
  186. switch (get_class($val)) {
  187. case 'DateTime':
  188. /** @var DateTime $val */
  189. $val = $val->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
  190. }
  191. } elseif (is_array($val)) {
  192. $val = implode(', ', $val);
  193. }
  194. return (string) $val;
  195. }
  196. }