AmazonAddressFactory.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Core\Domain;
  17. use Amazon\Core\Api\Data\AmazonAddressInterface;
  18. use Magento\Framework\Exception\LocalizedException;
  19. use Magento\Framework\ObjectManagerInterface;
  20. use Magento\Store\Model\StoreManagerInterface;
  21. use Magento\Framework\Escaper;
  22. class AmazonAddressFactory
  23. {
  24. /**
  25. * @var Escaper
  26. */
  27. private $escaper;
  28. /**
  29. * @var ObjectManagerInterface
  30. */
  31. private $objectManager = null;
  32. /**
  33. * @var AmazonAddressInterface[]
  34. */
  35. private $addressDecoratorPool;
  36. /**
  37. * @var AmazonNameFactory
  38. */
  39. private $amazonNameFactory;
  40. /**
  41. * AmazonAddressFactory constructor.
  42. *
  43. * @param ObjectManagerInterface $objectManager
  44. * @param AmazonNameFactory $amazonNameFactory
  45. * @param Escaper $escaper
  46. * @param array $addressDecoratorPool Per-country custom decorators of incoming address data.
  47. * The key as an "ISO 3166-1 alpha-2" country code and
  48. * the value as an FQCN of a child of AmazonAddress.
  49. */
  50. public function __construct(
  51. ObjectManagerInterface $objectManager,
  52. AmazonNameFactory $amazonNameFactory,
  53. Escaper $escaper,
  54. array $addressDecoratorPool = []
  55. ) {
  56. $this->objectManager = $objectManager;
  57. $this->amazonNameFactory = $amazonNameFactory;
  58. $this->escaper = $escaper;
  59. $this->addressDecoratorPool = $addressDecoratorPool;
  60. }
  61. /**
  62. * @param array $responseData
  63. *
  64. * @return AmazonAddressInterface
  65. * @throws LocalizedException
  66. */
  67. public function create(array $responseData = []): AmazonAddressInterface
  68. {
  69. $address = $responseData['address'];
  70. $amazonName = $this->amazonNameFactory->create(
  71. [
  72. 'name' => $this->escaper->escapeHtml($address['Name']),
  73. 'country' => $this->escaper->escapeHtml($address['CountryCode'])]
  74. );
  75. $data = [
  76. AmazonAddressInterface::POSTAL_CODE => isset($address['PostalCode']) ? $this->escaper->escapeHtml($address['PostalCode']) : '',
  77. AmazonAddressInterface::COUNTRY_CODE => $this->escaper->escapeHtml($address['CountryCode']),
  78. AmazonAddressInterface::TELEPHONE => isset($address['Phone']) ? $this->escaper->escapeHtml($address['Phone']) : '',
  79. AmazonAddressInterface::STATE_OR_REGION => isset($address['StateOrRegion']) ? $this->escaper->escapeHtml($address['StateOrRegion']) : '',
  80. AmazonAddressInterface::FIRST_NAME => $this->escaper->escapeHtml($amazonName->getFirstName()),
  81. AmazonAddressInterface::LAST_NAME => $this->escaper->escapeHtml($amazonName->getLastName()),
  82. AmazonAddressInterface::LINES => $this->getLines($address)
  83. ];
  84. if (isset($address['City'])) {
  85. $data[AmazonAddressInterface::CITY] = $this->escaper->escapeHtml($address['City']);
  86. }
  87. $amazonAddress = $this->objectManager->create(AmazonAddress::class, ['data' => $data]);
  88. $countryCode = strtoupper($address['CountryCode']);
  89. if (empty($this->addressDecoratorPool[$countryCode])) {
  90. return $amazonAddress;
  91. }
  92. $amazonAddress = $this->objectManager->create(
  93. $this->addressDecoratorPool[$countryCode],
  94. [
  95. 'amazonAddress' => $amazonAddress,
  96. ]
  97. );
  98. if (!$amazonAddress instanceof AmazonAddressInterface) {
  99. throw new LocalizedException(
  100. __(
  101. 'Address country handler %1 must be of type %2',
  102. [$this->addressDecoratorPool[$countryCode], AmazonAddress::class]
  103. )
  104. );
  105. }
  106. return $amazonAddress;
  107. }
  108. /**
  109. * Returns address lines.
  110. *
  111. * @param array $responseData
  112. * @return array
  113. */
  114. private function getLines(array $responseData = []): array
  115. {
  116. $lines = [];
  117. for ($i = 1; $i <= 3; $i++) {
  118. if (isset($responseData['AddressLine' . $i]) && $responseData['AddressLine' . $i]) {
  119. $lines[$i] = $this->escaper->escapeHtml($responseData['AddressLine' . $i]);
  120. }
  121. }
  122. return $lines;
  123. }
  124. }