CarrierRegistration.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Carrier;
  6. use Magento\Framework\App\ProductMetadataInterface;
  7. use Magento\Framework\App\RequestInterface;
  8. use Magento\Framework\View\Element\Block\ArgumentInterface;
  9. use Temando\Shipping\Model\CarrierInterface;
  10. use Temando\Shipping\ViewModel\DataProvider\CarrierUrl;
  11. use Temando\Shipping\ViewModel\DataProvider\EntityUrlInterface;
  12. use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccess;
  13. use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccessInterface;
  14. use Temando\Shipping\ViewModel\ShippingApiInterface;
  15. /**
  16. * View model for carrier registration JS component.
  17. *
  18. * @package Temando\Shipping\ViewModel
  19. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  20. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  21. * @link https://www.temando.com/
  22. */
  23. class CarrierRegistration implements ArgumentInterface, ShippingApiInterface
  24. {
  25. /**
  26. * @var RequestInterface
  27. */
  28. private $request;
  29. /**
  30. * @var ShippingApiAccess
  31. */
  32. private $apiAccess;
  33. /**
  34. * @var CarrierUrl
  35. */
  36. private $carrierUrl;
  37. /**
  38. * @var ProductMetadataInterface
  39. */
  40. private $productMetaData;
  41. /**
  42. * CarrierRegistration constructor.
  43. * @param RequestInterface $request
  44. * @param ShippingApiAccess $apiAccess
  45. * @param CarrierUrl $carrierUrl
  46. * @param ProductMetadataInterface $productMetaData
  47. */
  48. public function __construct(
  49. RequestInterface $request,
  50. ShippingApiAccess $apiAccess,
  51. CarrierUrl $carrierUrl,
  52. ProductMetadataInterface $productMetaData
  53. ) {
  54. $this->request = $request;
  55. $this->apiAccess = $apiAccess;
  56. $this->carrierUrl = $carrierUrl;
  57. $this->productMetaData = $productMetaData;
  58. }
  59. /**
  60. * @return ShippingApiAccessInterface
  61. */
  62. public function getShippingApiAccess(): ShippingApiAccessInterface
  63. {
  64. return $this->apiAccess;
  65. }
  66. /**
  67. * @return EntityUrlInterface|CarrierUrl
  68. */
  69. public function getCarrierUrl(): EntityUrlInterface
  70. {
  71. return $this->carrierUrl;
  72. }
  73. /**
  74. * Obtain the Temando carrier integration id.
  75. *
  76. * @return string The Temando carrier integration ID.
  77. */
  78. public function getCarrierIntegrationId(): string
  79. {
  80. $integrationId = $this->request->getParam(CarrierInterface::INTEGRATION_ID);
  81. return preg_replace('/[^\w0-9-_]/', '', $integrationId);
  82. }
  83. /**
  84. * Get the Magento version number.
  85. *
  86. * @return string
  87. */
  88. public function getMagentoVersion()
  89. {
  90. if (!preg_match('/\d+\.\d+(\.\d+)?/', $this->productMetaData->getVersion(), $matches)) {
  91. return '';
  92. }
  93. return $matches[0];
  94. }
  95. }