Data.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Shipping data helper
  8. */
  9. namespace Magento\Shipping\Helper;
  10. use Magento\Framework\App\ObjectManager;
  11. use Magento\Framework\UrlInterface;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  14. {
  15. /**
  16. * Allowed hash keys
  17. *
  18. * @var array
  19. */
  20. protected $_allowedHashKeys = ['ship_id', 'order_id', 'track_id'];
  21. /**
  22. * @var StoreManagerInterface
  23. */
  24. protected $_storeManager;
  25. /**
  26. * @var UrlInterface|null
  27. */
  28. private $url;
  29. /**
  30. * @param \Magento\Framework\App\Helper\Context $context
  31. * @param StoreManagerInterface $storeManager
  32. * @param UrlInterface|null $url
  33. */
  34. public function __construct(
  35. \Magento\Framework\App\Helper\Context $context,
  36. StoreManagerInterface $storeManager,
  37. UrlInterface $url = null
  38. ) {
  39. $this->_storeManager = $storeManager;
  40. $this->url = $url ?: ObjectManager::getInstance()->get(UrlInterface::class);
  41. parent::__construct($context);
  42. }
  43. /**
  44. * Decode url hash
  45. *
  46. * @param string $hash
  47. * @return array
  48. */
  49. public function decodeTrackingHash($hash)
  50. {
  51. $hash = explode(':', $this->urlDecoder->decode($hash));
  52. if (count($hash) === 3 && in_array($hash[0], $this->_allowedHashKeys)) {
  53. return ['key' => $hash[0], 'id' => (int)$hash[1], 'hash' => $hash[2]];
  54. }
  55. return [];
  56. }
  57. /**
  58. * Retrieve tracking url with params
  59. *
  60. * @param string $key
  61. * @param \Magento\Sales\Model\Order
  62. * |\Magento\Sales\Model\Order\Shipment|\Magento\Sales\Model\Order\Shipment\Track $model
  63. * @param string $method Optional - method of a model to get id
  64. * @return string
  65. */
  66. protected function _getTrackingUrl($key, $model, $method = 'getId')
  67. {
  68. $urlPart = "{$key}:{$model->{$method}()}:{$model->getProtectCode()}";
  69. $params = [
  70. '_scope' => $model->getStoreId(),
  71. '_nosid' => true,
  72. '_direct' => 'shipping/tracking/popup',
  73. '_query' => ['hash' => $this->urlEncoder->encode($urlPart)]
  74. ];
  75. return $this->url->getUrl('', $params);
  76. }
  77. /**
  78. * Shipping tracking popup URL getter
  79. *
  80. * @param \Magento\Sales\Model\AbstractModel $model
  81. * @return string
  82. */
  83. public function getTrackingPopupUrlBySalesModel($model)
  84. {
  85. if ($model instanceof \Magento\Sales\Model\Order) {
  86. return $this->_getTrackingUrl('order_id', $model);
  87. } elseif ($model instanceof \Magento\Sales\Model\Order\Shipment) {
  88. return $this->_getTrackingUrl('ship_id', $model);
  89. } elseif ($model instanceof \Magento\Sales\Model\Order\Shipment\Track) {
  90. return $this->_getTrackingUrl('track_id', $model, 'getEntityId');
  91. }
  92. return '';
  93. }
  94. }