AddOns.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Shipment;
  6. use Magento\Backend\Block\Template;
  7. use Magento\Framework\View\Element\Block\ArgumentInterface;
  8. use Magento\Framework\View\LayoutInterface;
  9. use Temando\Shipping\Model\Shipment\CapabilityInterface;
  10. use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;
  11. use Temando\Shipping\Model\ShipmentInterface;
  12. /**
  13. * View model for shipment capabilities.
  14. *
  15. * @package Temando\Shipping\ViewModel
  16. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  17. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link http://www.temando.com/
  19. */
  20. class AddOns implements ArgumentInterface
  21. {
  22. /**
  23. * @var LayoutInterface
  24. */
  25. private $layout;
  26. /**
  27. * @var ShipmentProviderInterface
  28. */
  29. private $shipmentProvider;
  30. /**
  31. * AddOns constructor.
  32. * @param LayoutInterface $layout
  33. * @param ShipmentProviderInterface $shipmentProvider
  34. */
  35. public function __construct(
  36. LayoutInterface $layout,
  37. ShipmentProviderInterface $shipmentProvider
  38. ) {
  39. $this->layout = $layout;
  40. $this->shipmentProvider = $shipmentProvider;
  41. }
  42. /**
  43. * @return CapabilityInterface[]
  44. */
  45. private function getCapabilities()
  46. {
  47. /** @var ShipmentInterface $shipment */
  48. $shipment = $this->shipmentProvider->getShipment();
  49. if (!$shipment) {
  50. return [];
  51. }
  52. return (array) $shipment->getCapabilities();
  53. }
  54. /**
  55. * @return string
  56. */
  57. private function getAddressType()
  58. {
  59. /** @var ShipmentInterface $shipment */
  60. $shipment = $this->shipmentProvider->getShipment();
  61. if (!$shipment) {
  62. return '';
  63. }
  64. return (string) $shipment->getDestinationLocation()->getType();
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getAddOnsHtml()
  70. {
  71. $addOnsHtml = '';
  72. $capabilities = $this->getCapabilities();
  73. $addressType = $this->getAddressType();
  74. if (empty($capabilities) && empty($addressType)) {
  75. return $addOnsHtml;
  76. }
  77. /** @var Template $addOnBlock */
  78. $addOnBlock = $this->layout->getBlock('addon_listing');
  79. $templates = $addOnBlock->getData('templates');
  80. $addOnBlock = $this->layout->createBlock(Template::class, 'addon_item');
  81. foreach ($capabilities as $capability) {
  82. $addOnBlock->setData('capability', $capability);
  83. if (!isset($templates[$capability->getCapabilityId()])) {
  84. $addOnBlock->setTemplate($templates['default']);
  85. } else {
  86. $addOnBlock->setTemplate($templates[$capability->getCapabilityId()]);
  87. }
  88. $addOnsHtml.= $addOnBlock->toHtml();
  89. }
  90. $addOnBlock->setData('address_type', $addressType);
  91. $addOnBlock->setTemplate($templates['addressType']);
  92. $addOnsHtml.= $addOnBlock->toHtml();
  93. return $addOnsHtml;
  94. }
  95. }