CollectionPointConfigProvider.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Delivery;
  6. use Magento\Checkout\Model\ConfigProviderInterface;
  7. use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Store\Model\StoreManagerInterface;
  10. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  11. /**
  12. * Provide Collection Point Country Data.
  13. *
  14. * @package Temando\Shipping\Model
  15. * @author Sebastian Ertner <sebastian.ertner@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 CollectionPointConfigProvider implements ConfigProviderInterface
  20. {
  21. /**
  22. * @var ModuleConfigInterface
  23. */
  24. private $moduleConfig;
  25. /**
  26. * @var CollectionFactory
  27. */
  28. private $countryCollectionFactory;
  29. /**
  30. * @var StoreManagerInterface
  31. */
  32. private $storeManager;
  33. /**
  34. * CollectionPointConfigProvider constructor.
  35. * @param ModuleConfigInterface $moduleConfig
  36. * @param CollectionFactory $countryCollectionFactory
  37. * @param StoreManagerInterface $storeManager
  38. */
  39. public function __construct(
  40. ModuleConfigInterface $moduleConfig,
  41. CollectionFactory $countryCollectionFactory,
  42. StoreManagerInterface $storeManager
  43. ) {
  44. $this->moduleConfig = $moduleConfig;
  45. $this->countryCollectionFactory = $countryCollectionFactory;
  46. $this->storeManager = $storeManager;
  47. }
  48. /**
  49. * Obtain country data for display in checkout, shipping method step.
  50. *
  51. * @return string[]
  52. */
  53. public function getConfig()
  54. {
  55. try {
  56. $storeId = $this->storeManager->getStore()->getId();
  57. } catch (NoSuchEntityException $exception) {
  58. $storeId = null;
  59. }
  60. if (!$this->moduleConfig->isEnabled($storeId) || !$this->moduleConfig->isCollectionPointsEnabled($storeId)) {
  61. return ['countries' => []];
  62. }
  63. $countryCodes = $this->moduleConfig->getCollectionPointDeliveryCountries($storeId);
  64. $countryCollection = $this->countryCollectionFactory->create();
  65. $countryCollection->addFieldToFilter('country_id', ['in' => explode(',', $countryCodes)]);
  66. $countryCollection->loadByStore($storeId);
  67. return ['ts-cp-countries' => $countryCollection->toOptionArray(false)];
  68. }
  69. }