OrderAddressFactory.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Location;
  6. use Magento\Directory\Model\RegionFactory;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Sales\Api\Data\OrderAddressInterface;
  9. use Magento\Sales\Api\Data\OrderAddressInterfaceFactory;
  10. use Magento\Sales\Model\Order\Address;
  11. use Temando\Shipping\Api\Data\Delivery\OrderCollectionPointInterface;
  12. use Temando\Shipping\Api\Data\Delivery\OrderPickupLocationInterface;
  13. use Temando\Shipping\Model\Shipment\LocationInterface;
  14. /**
  15. * Temando Order Address Factory
  16. *
  17. * Create order address from different location entities
  18. *
  19. * @package Temando\Shipping\Model
  20. * @author Rhodri Davies <rhodri.davies@temando.com>
  21. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link https://www.temando.com/
  23. */
  24. class OrderAddressFactory
  25. {
  26. /**
  27. * @var OrderAddressInterfaceFactory
  28. */
  29. private $addressFactory;
  30. /**
  31. * @var RegionFactory
  32. */
  33. private $regionFactory;
  34. /**
  35. * OrderAddressFactory constructor.
  36. * @param OrderAddressInterfaceFactory $addressFactory
  37. * @param RegionFactory $regionFactory
  38. */
  39. public function __construct(OrderAddressInterfaceFactory $addressFactory, RegionFactory $regionFactory)
  40. {
  41. $this->addressFactory = $addressFactory;
  42. $this->regionFactory = $regionFactory;
  43. }
  44. /**
  45. * @param string[] $addressData
  46. * @return OrderAddressInterface
  47. */
  48. private function create(array $addressData)
  49. {
  50. try {
  51. /** @var \Magento\Directory\Model\Region $region */
  52. $region = $this->regionFactory->create();
  53. $region = $region->loadByCode($addressData['region'], $addressData['country_id']);
  54. $regionName = $region->getName() ?: $addressData['region'];
  55. $addressData['region'] = $regionName;
  56. $address = $this->addressFactory->create(['data' => $addressData]);
  57. } catch (LocalizedException $e) {
  58. $address = $this->addressFactory->create(['data' => $addressData]);
  59. }
  60. $address->setAddressType(Address::TYPE_SHIPPING);
  61. return $address;
  62. }
  63. /**
  64. * @param LocationInterface $location
  65. * @return OrderAddressInterface
  66. */
  67. public function createFromShipmentLocation(LocationInterface $location)
  68. {
  69. $addressData = [
  70. 'firstname' => $location->getPersonFirstName(),
  71. 'lastname' => $location->getPersonLastName(),
  72. 'company' => $location->getCompany(),
  73. 'street' => $location->getStreet(),
  74. 'city' => $location->getCity(),
  75. 'postcode' => $location->getPostalCode(),
  76. 'region' => $location->getRegionCode(),
  77. 'country_id' => $location->getCountryCode(),
  78. 'email' => $location->getEmail(),
  79. 'telephone' => $location->getPhoneNumber(),
  80. ];
  81. return $this->create($addressData);
  82. }
  83. /**
  84. * @param OrderPickupLocationInterface $location
  85. * @return OrderAddressInterface
  86. */
  87. public function createFromPickupLocation(OrderPickupLocationInterface $location)
  88. {
  89. $addressData = [
  90. 'company' => $location->getName(),
  91. 'street' => $location->getStreet(),
  92. 'city' => $location->getCity(),
  93. 'postcode' => $location->getPostcode(),
  94. 'region' => $location->getRegion(),
  95. 'country_id' => $location->getCountry(),
  96. ];
  97. return $this->create($addressData);
  98. }
  99. /**
  100. * @param OrderCollectionPointInterface $location
  101. * @return OrderAddressInterface
  102. */
  103. public function createFromCollectionPoint(OrderCollectionPointInterface $location)
  104. {
  105. $addressData = [
  106. 'company' => $location->getName(),
  107. 'street' => $location->getStreet(),
  108. 'city' => $location->getCity(),
  109. 'postcode' => $location->getPostcode(),
  110. 'region' => $location->getRegion(),
  111. 'country_id' => $location->getCountry(),
  112. ];
  113. return $this->create($addressData);
  114. }
  115. }