XmlToArrayConfigConverter.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\PaymentMethodMapper;
  7. use Magento\Framework\Config\Dom\ValidationSchemaException;
  8. /**
  9. * Converts XML config file to payment methods mapping.
  10. */
  11. class XmlToArrayConfigConverter implements \Magento\Framework\Config\ConverterInterface
  12. {
  13. /**
  14. * Node type wrapper for magento and signifyd payment codes
  15. *
  16. * @var string
  17. */
  18. private static $paymentMethodNodeType = 'payment_method';
  19. /**
  20. * Node type for payment methods code
  21. *
  22. * @var string
  23. */
  24. private static $magentoCodeNodeType = 'magento_code';
  25. /**
  26. * Node type for Sygnifyd payment methods code
  27. *
  28. * @var string
  29. */
  30. private static $signifydCodeNodeType = 'signifyd_code';
  31. /**
  32. * @inheritdoc
  33. */
  34. public function convert($source)
  35. {
  36. $paymentMethods = $source->getElementsByTagName(self::$paymentMethodNodeType);
  37. $paymentsList = [];
  38. foreach ($paymentMethods as $paymentMethod) {
  39. $paymentsList += $this->getPaymentMethodMapping($paymentMethod);
  40. }
  41. return $paymentsList;
  42. }
  43. /**
  44. * Adds a payment method as key and a Sygnifyd payment method as value
  45. * in the payment list array
  46. *
  47. * @param \DOMElement $payment
  48. * @return array
  49. * @throws ValidationSchemaException
  50. */
  51. private function getPaymentMethodMapping(\DOMElement $payment)
  52. {
  53. $paymentMethodCode = $this->readSubnodeValue($payment, self::$magentoCodeNodeType);
  54. $signifyPaymentMethodCode = $this->readSubnodeValue($payment, self::$signifydCodeNodeType);
  55. return [$paymentMethodCode => $signifyPaymentMethodCode];
  56. }
  57. /**
  58. * Reads node value by node type
  59. *
  60. * @param \DOMElement $element
  61. * @param string $subNodeType
  62. * @return mixed
  63. * @throws ValidationSchemaException
  64. */
  65. private function readSubnodeValue(\DOMElement $element, $subNodeType)
  66. {
  67. $domList = $element->getElementsByTagName($subNodeType);
  68. if (empty($domList[0])) {
  69. throw new ValidationSchemaException(__('Only single entrance of "%1" node is required.', $subNodeType));
  70. }
  71. $subNodeValue = trim($domList[0]->nodeValue);
  72. if (!$subNodeValue) {
  73. throw new ValidationSchemaException(__('Not empty value for "%1" node is required.', $subNodeType));
  74. }
  75. return $subNodeValue;
  76. }
  77. }