DispatchView.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Dispatch;
  6. use Magento\Directory\Model\Currency;
  7. use Magento\Directory\Model\CurrencyFactory;
  8. use Magento\Framework\DataObject;
  9. use Magento\Framework\View\Element\Block\ArgumentInterface;
  10. use Temando\Shipping\Model\DispatchProviderInterface;
  11. /**
  12. * View model for dispatch view page.
  13. *
  14. * @package Temando\Shipping\ViewModel
  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 DispatchView implements ArgumentInterface
  20. {
  21. /**
  22. * @var DispatchProviderInterface
  23. */
  24. private $dispatchProvider;
  25. /**
  26. * @var CurrencyFactory
  27. */
  28. private $currencyFactory;
  29. /**
  30. * @var Currency
  31. */
  32. private $currency;
  33. /**
  34. * DispatchView constructor.
  35. * @param DispatchProviderInterface $dispatchProvider
  36. * @param CurrencyFactory $currencyFactory
  37. */
  38. public function __construct(
  39. DispatchProviderInterface $dispatchProvider,
  40. CurrencyFactory $currencyFactory
  41. ) {
  42. $this->dispatchProvider = $dispatchProvider;
  43. $this->currencyFactory = $currencyFactory;
  44. }
  45. /**
  46. * @return DataObject[]
  47. */
  48. public function getPickupCharges(): array
  49. {
  50. $dispatch = $this->dispatchProvider->getDispatch();
  51. if (!$dispatch) {
  52. return [];
  53. }
  54. /** @var DataObject[] $pickupCharges */
  55. $pickupCharges = $dispatch->getPickupCharges();
  56. return $pickupCharges;
  57. }
  58. /**
  59. * @param string $currencyCode
  60. * @param float $amount
  61. * @return string
  62. */
  63. public function formatPrice(string $currencyCode, float $amount): string
  64. {
  65. if ($this->currency === null || $this->currency->getCurrencyCode() !== $currencyCode) {
  66. $this->currency = $this->currencyFactory->create(['data' => ['currency_code' => $currencyCode]]);
  67. }
  68. return $this->currency->format($amount);
  69. }
  70. }