Information.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model;
  7. use Magento\Directory\Model\CountryFactory;
  8. use Magento\Directory\Model\RegionFactory;
  9. use Magento\Framework\DataObject;
  10. use Magento\Store\Model\Address\Renderer;
  11. /**
  12. * Store information class used to retrieve and format store information as set in store config
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Information
  18. {
  19. /**#@+
  20. * Configuration paths
  21. */
  22. const XML_PATH_STORE_INFO_NAME = 'general/store_information/name';
  23. const XML_PATH_STORE_INFO_PHONE = 'general/store_information/phone';
  24. const XML_PATH_STORE_INFO_HOURS = 'general/store_information/hours';
  25. const XML_PATH_STORE_INFO_STREET_LINE1 = 'general/store_information/street_line1';
  26. const XML_PATH_STORE_INFO_STREET_LINE2 = 'general/store_information/street_line2';
  27. const XML_PATH_STORE_INFO_CITY = 'general/store_information/city';
  28. const XML_PATH_STORE_INFO_POSTCODE = 'general/store_information/postcode';
  29. const XML_PATH_STORE_INFO_REGION_CODE = 'general/store_information/region_id';
  30. const XML_PATH_STORE_INFO_COUNTRY_CODE = 'general/store_information/country_id';
  31. const XML_PATH_STORE_INFO_VAT_NUMBER = 'general/store_information/merchant_vat_number';
  32. /**#@-*/
  33. /**#@-*/
  34. protected $renderer;
  35. /**
  36. * @var CountryFactory
  37. */
  38. protected $countryFactory;
  39. /**
  40. * @var RegionFactory
  41. */
  42. protected $regionFactory;
  43. /**
  44. * @param Renderer $renderer
  45. * @param RegionFactory $regionFactory
  46. * @param CountryFactory $countryFactory
  47. */
  48. public function __construct(
  49. Renderer $renderer,
  50. RegionFactory $regionFactory,
  51. CountryFactory $countryFactory
  52. ) {
  53. $this->renderer = $renderer;
  54. $this->regionFactory = $regionFactory;
  55. $this->countryFactory = $countryFactory;
  56. }
  57. /**
  58. * Retrieve generic object with all the misc store information values
  59. *
  60. * @param Store $store
  61. * @return DataObject
  62. */
  63. public function getStoreInformationObject(Store $store)
  64. {
  65. $info = new DataObject([
  66. 'name' => $store->getConfig(self::XML_PATH_STORE_INFO_NAME),
  67. 'phone' => $store->getConfig(self::XML_PATH_STORE_INFO_PHONE),
  68. 'hours' => $store->getConfig(self::XML_PATH_STORE_INFO_HOURS),
  69. 'street_line1' => $store->getConfig(self::XML_PATH_STORE_INFO_STREET_LINE1),
  70. 'street_line2' => $store->getConfig(self::XML_PATH_STORE_INFO_STREET_LINE2),
  71. 'city' => $store->getConfig(self::XML_PATH_STORE_INFO_CITY),
  72. 'postcode' => $store->getConfig(self::XML_PATH_STORE_INFO_POSTCODE),
  73. 'region_id' => $store->getConfig(self::XML_PATH_STORE_INFO_REGION_CODE),
  74. 'country_id' => $store->getConfig(self::XML_PATH_STORE_INFO_COUNTRY_CODE),
  75. 'vat_number' => $store->getConfig(self::XML_PATH_STORE_INFO_VAT_NUMBER),
  76. ]);
  77. if ($info->getRegionId()) {
  78. $info->setRegion($this->regionFactory->create()->load($info->getRegionId())->getName());
  79. }
  80. if ($info->getCountryId()) {
  81. $info->setCountry($this->countryFactory->create()->loadByCode($info->getCountryId())->getName());
  82. }
  83. return $info;
  84. }
  85. /**
  86. * Retrieve formatted store address from config
  87. *
  88. * @param Store $store
  89. * @return string
  90. */
  91. public function getFormattedAddress(Store $store)
  92. {
  93. return $this->renderer->format($this->getStoreInformationObject($store));
  94. }
  95. }