PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Directory/Model/CountryInformationAcquirer.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 147 lines | 84 code | 19 blank | 44 comment | 2 complexity | 600356814b2b60011a71bc7389cc571f MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. /**
  9. * Currency information acquirer class
  10. */
  11. class CountryInformationAcquirer implements \Magento\Directory\Api\CountryInformationAcquirerInterface
  12. {
  13. /**
  14. * @var \Magento\Directory\Model\Data\CountryInformationFactory
  15. */
  16. protected $countryInformationFactory;
  17. /**
  18. * @var \Magento\Directory\Model\Data\RegionInformationFactory
  19. */
  20. protected $regionInformationFactory;
  21. /**
  22. * @var \Magento\Directory\Helper\Data
  23. */
  24. protected $directoryHelper;
  25. /**
  26. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  27. */
  28. protected $scopeConfig;
  29. /**
  30. * @var \Magento\Store\Model\StoreManagerInterface
  31. */
  32. protected $storeManager;
  33. /**
  34. * @param \Magento\Directory\Model\Data\CountryInformationFactory $countryInformationFactory
  35. * @param \Magento\Directory\Model\Data\RegionInformationFactory $regionInformationFactory
  36. * @param \Magento\Directory\Helper\Data $directoryHelper
  37. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  38. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  39. */
  40. public function __construct(
  41. \Magento\Directory\Model\Data\CountryInformationFactory $countryInformationFactory,
  42. \Magento\Directory\Model\Data\RegionInformationFactory $regionInformationFactory,
  43. \Magento\Directory\Helper\Data $directoryHelper,
  44. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  45. \Magento\Store\Model\StoreManagerInterface $storeManager
  46. ) {
  47. $this->countryInformationFactory = $countryInformationFactory;
  48. $this->regionInformationFactory = $regionInformationFactory;
  49. $this->directoryHelper = $directoryHelper;
  50. $this->scopeConfig = $scopeConfig;
  51. $this->storeManager = $storeManager;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getCountriesInfo()
  57. {
  58. $countriesInfo = [];
  59. /** @var \Magento\Store\Model\Store $store */
  60. $store = $this->storeManager->getStore();
  61. $storeLocale = $this->scopeConfig->getValue(
  62. 'general/locale/code',
  63. \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
  64. $store->getCode()
  65. );
  66. $countries = $this->directoryHelper->getCountryCollection($store);
  67. $regions = $this->directoryHelper->getRegionData();
  68. foreach ($countries as $data) {
  69. $countryInfo = $this->setCountryInfo($data, $regions, $storeLocale);
  70. $countriesInfo[] = $countryInfo;
  71. }
  72. return $countriesInfo;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getCountryInfo($countryId)
  78. {
  79. $store = $this->storeManager->getStore();
  80. $storeLocale = $this->scopeConfig->getValue(
  81. 'general/locale/code',
  82. \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
  83. $store->getCode()
  84. );
  85. $countriesCollection = $this->directoryHelper->getCountryCollection($store)->load();
  86. $regions = $this->directoryHelper->getRegionData();
  87. $country = $countriesCollection->getItemById($countryId);
  88. if (!$country) {
  89. throw new NoSuchEntityException(
  90. __(
  91. 'Requested country is not available.'
  92. )
  93. );
  94. }
  95. $countryInfo = $this->setCountryInfo($country, $regions, $storeLocale);
  96. return $countryInfo;
  97. }
  98. /**
  99. * Creates and initializes the information for \Magento\Directory\Model\Data\CountryInformation
  100. *
  101. * @param \Magento\Directory\Model\ResourceModel\Country $country
  102. * @param array $regions
  103. * @param string $storeLocale
  104. * @return \Magento\Directory\Model\Data\CountryInformation
  105. */
  106. protected function setCountryInfo($country, $regions, $storeLocale)
  107. {
  108. $countryId = $country->getCountryId();
  109. $countryInfo = $this->countryInformationFactory->create();
  110. $countryInfo->setId($countryId);
  111. $countryInfo->setTwoLetterAbbreviation($country->getData('iso2_code'));
  112. $countryInfo->setThreeLetterAbbreviation($country->getData('iso3_code'));
  113. $countryInfo->setFullNameLocale($country->getName($storeLocale));
  114. $countryInfo->setFullNameEnglish($country->getName('en_US'));
  115. if (array_key_exists($countryId, $regions)) {
  116. $regionsInfo = [];
  117. foreach ($regions[$countryId] as $id => $regionData) {
  118. $regionInfo = $this->regionInformationFactory->create();
  119. $regionInfo->setId($id);
  120. $regionInfo->setCode($regionData['code']);
  121. $regionInfo->setName($regionData['name']);
  122. $regionsInfo[] = $regionInfo;
  123. }
  124. $countryInfo->setAvailableRegions($regionsInfo);
  125. }
  126. return $countryInfo;
  127. }
  128. }