StoreConfigurationProvider.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Store\Api\Data\StoreInterface;
  9. use Magento\Store\Api\Data\WebsiteInterface;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. /**
  13. * Provides config data report
  14. */
  15. class StoreConfigurationProvider
  16. {
  17. /**
  18. * @var ScopeConfigInterface
  19. */
  20. private $scopeConfig;
  21. /**
  22. * @var string[]
  23. */
  24. private $configPaths;
  25. /**
  26. * @var StoreManagerInterface
  27. */
  28. private $storeManager;
  29. /**
  30. * @param ScopeConfigInterface $scopeConfig
  31. * @param StoreManagerInterface $storeManager
  32. * @param string[] $configPaths
  33. */
  34. public function __construct(
  35. ScopeConfigInterface $scopeConfig,
  36. StoreManagerInterface $storeManager,
  37. array $configPaths
  38. ) {
  39. $this->scopeConfig = $scopeConfig;
  40. $this->configPaths = $configPaths;
  41. $this->storeManager = $storeManager;
  42. }
  43. /**
  44. * Generates report using config paths from di.xml
  45. * For each website and store
  46. * @return \IteratorIterator
  47. */
  48. public function getReport()
  49. {
  50. $configReport = $this->generateReportForScope(ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 0);
  51. /** @var WebsiteInterface $website */
  52. foreach ($this->storeManager->getWebsites() as $website) {
  53. $configReport = array_merge(
  54. $this->generateReportForScope(ScopeInterface::SCOPE_WEBSITES, $website->getId()),
  55. $configReport
  56. );
  57. }
  58. /** @var StoreInterface $store */
  59. foreach ($this->storeManager->getStores() as $store) {
  60. $configReport = array_merge(
  61. $this->generateReportForScope(ScopeInterface::SCOPE_STORES, $store->getId()),
  62. $configReport
  63. );
  64. }
  65. return new \IteratorIterator(new \ArrayIterator($configReport));
  66. }
  67. /**
  68. * Creates report from config for scope type and scope id.
  69. *
  70. * @param string $scope
  71. * @param int $scopeId
  72. * @return array
  73. */
  74. private function generateReportForScope($scope, $scopeId)
  75. {
  76. $report = [];
  77. foreach ($this->configPaths as $configPath) {
  78. $report[] = [
  79. "config_path" => $configPath,
  80. "scope" => $scope,
  81. "scope_id" => $scopeId,
  82. "value" => $this->scopeConfig->getValue(
  83. $configPath,
  84. $scope,
  85. $scopeId
  86. )
  87. ];
  88. }
  89. return $report;
  90. }
  91. }