|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\CustomerImportExport\Model\Import; |
| 10 | + |
| 11 | +use Magento\Customer\Model\Config\Share as CustomerShareConfig; |
| 12 | +use Magento\Directory\Model\AllowedCountries; |
| 13 | +use Magento\Directory\Model\ResourceModel\Country\Collection as CountryCollection; |
| 14 | +use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory; |
| 15 | +use Magento\Store\Model\ScopeInterface; |
| 16 | +use Magento\Store\Model\StoreManagerInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Return allowed countries for specified website for customer import |
| 20 | + */ |
| 21 | +class CountryWithWebsites |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var CountryCollectionFactory |
| 25 | + */ |
| 26 | + private $countriesFactory; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var AllowedCountries |
| 30 | + */ |
| 31 | + private $allowedCountriesReader; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var array |
| 35 | + */ |
| 36 | + private $allowedCountries; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var StoreManagerInterface |
| 40 | + */ |
| 41 | + private $storeManager; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var CustomerShareConfig |
| 45 | + */ |
| 46 | + private $shareConfig; |
| 47 | + |
| 48 | + /** |
| 49 | + * @param CountryCollectionFactory $countriesFactory |
| 50 | + * @param AllowedCountries $allowedCountriesReader |
| 51 | + * @param StoreManagerInterface $storeManager |
| 52 | + * @param CustomerShareConfig $shareConfig |
| 53 | + */ |
| 54 | + public function __construct( |
| 55 | + CountryCollectionFactory $countriesFactory, |
| 56 | + AllowedCountries $allowedCountriesReader, |
| 57 | + StoreManagerInterface $storeManager, |
| 58 | + CustomerShareConfig $shareConfig |
| 59 | + ) { |
| 60 | + $this->countriesFactory = $countriesFactory; |
| 61 | + $this->allowedCountriesReader = $allowedCountriesReader; |
| 62 | + $this->storeManager = $storeManager; |
| 63 | + $this->shareConfig = $shareConfig; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get allowed countries for specified website |
| 68 | + * |
| 69 | + * @return array |
| 70 | + */ |
| 71 | + public function getCountiesPerWebsite(): array |
| 72 | + { |
| 73 | + if (!$this->allowedCountries) { |
| 74 | + $websiteIds = []; |
| 75 | + |
| 76 | + foreach ($this->storeManager->getWebsites() as $website) { |
| 77 | + $countries = $this->allowedCountriesReader |
| 78 | + ->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId()); |
| 79 | + $allowedCountries[] = $countries; |
| 80 | + |
| 81 | + foreach ($countries as $countryCode) { |
| 82 | + $websiteIds[$countryCode][] = $website->getId(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + $this->allowedCountries = $this->createCountriesCollection() |
| 87 | + ->addFieldToFilter('country_id', ['in' => $allowedCountries]) |
| 88 | + ->toOptionArray(); |
| 89 | + |
| 90 | + foreach ($this->allowedCountries as &$option) { |
| 91 | + if (isset($websiteIds[$option['value']])) { |
| 92 | + $option['website_ids'] = $websiteIds[$option['value']]; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return $this->allowedCountries; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Create Countries Collection with all countries |
| 102 | + * |
| 103 | + * @return CountryCollection |
| 104 | + */ |
| 105 | + private function createCountriesCollection() |
| 106 | + { |
| 107 | + return $this->countriesFactory->create(); |
| 108 | + } |
| 109 | +} |
0 commit comments