CarrierResponseMapper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest\EntityMapper;
  6. use Temando\Shipping\Model\CarrierInterface;
  7. use Temando\Shipping\Model\CarrierInterfaceFactory;
  8. use Temando\Shipping\Rest\Response\DataObject\CarrierConfiguration;
  9. use Temando\Shipping\Rest\Response\DataObject\CarrierIntegration;
  10. /**
  11. * Map API data to application data object
  12. *
  13. * @package Temando\Shipping\Rest
  14. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  15. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  16. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. * @link https://www.temando.com/
  18. */
  19. class CarrierResponseMapper
  20. {
  21. /**
  22. * @var CarrierInterfaceFactory
  23. */
  24. private $carrierFactory;
  25. /**
  26. * CarrierResponseMapper constructor.
  27. * @param CarrierInterfaceFactory $carrierFactory
  28. */
  29. public function __construct(CarrierInterfaceFactory $carrierFactory)
  30. {
  31. $this->carrierFactory = $carrierFactory;
  32. }
  33. /**
  34. * @param CarrierIntegration $apiIntegration
  35. * @return string[]
  36. */
  37. private function getAvailableServices(CarrierIntegration $apiIntegration)
  38. {
  39. $apiIntegrationServices = $apiIntegration->getAttributes()->getServices();
  40. $serviceNames = [];
  41. foreach ($apiIntegrationServices as $apiIntegrationService) {
  42. $serviceNames[$apiIntegrationService->getId()] = $apiIntegrationService->getName();
  43. }
  44. return $serviceNames;
  45. }
  46. /**
  47. * @param CarrierConfiguration $apiConfiguration
  48. * @param CarrierIntegration $apiIntegration
  49. * @return CarrierInterface
  50. */
  51. public function map(
  52. CarrierConfiguration $apiConfiguration,
  53. CarrierIntegration $apiIntegration = null
  54. ) {
  55. /** @var \Temando\Shipping\Model\Carrier $carrier */
  56. $carrier = $this->carrierFactory->create(['data' => [
  57. CarrierInterface::CONFIGURATION_ID => (string)$apiConfiguration->getId(),
  58. CarrierInterface::INTEGRATION_ID => (string)$apiConfiguration->getAttributes()->getIntegrationId(),
  59. CarrierInterface::CONNECTION_NAME => (string)$apiConfiguration->getAttributes()->getConnectionName(),
  60. CarrierInterface::STATUS => (string)$apiConfiguration->getAttributes()->getStatus(),
  61. ]]);
  62. if ($apiIntegration) {
  63. $availableServices = $this->getAvailableServices($apiIntegration);
  64. $activeServiceIds = array_combine(
  65. $apiConfiguration->getAttributes()->getIntegrationServiceIds(),
  66. $apiConfiguration->getAttributes()->getIntegrationServiceIds()
  67. );
  68. $activeServices = array_intersect_key($availableServices, $activeServiceIds);
  69. $carrier->addData([
  70. CarrierInterface::NAME => (string)$apiIntegration->getAttributes()->getName(),
  71. CarrierInterface::LOGO => (string)$apiIntegration->getAttributes()->getLogo(),
  72. CarrierInterface::ACTIVE_SERVICES => $activeServices,
  73. ]);
  74. }
  75. return $carrier;
  76. }
  77. }