GroupRepository.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Framework\App\ObjectManager;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\App\Config;
  10. /**
  11. * Information Expert in store groups handling
  12. *
  13. * @package Magento\Store\Model
  14. */
  15. class GroupRepository implements \Magento\Store\Api\GroupRepositoryInterface
  16. {
  17. /**
  18. * @var GroupFactory
  19. */
  20. protected $groupFactory;
  21. /**
  22. * @var \Magento\Store\Api\Data\GroupInterface[]
  23. */
  24. protected $entities = [];
  25. /**
  26. * @var bool
  27. */
  28. protected $allLoaded = false;
  29. /**
  30. * @var \Magento\Store\Model\ResourceModel\Group\CollectionFactory
  31. */
  32. protected $groupCollectionFactory;
  33. /**
  34. * @var Config
  35. */
  36. private $appConfig;
  37. /**
  38. * @param GroupFactory $groupFactory
  39. * @param \Magento\Store\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory
  40. */
  41. public function __construct(
  42. GroupFactory $groupFactory,
  43. \Magento\Store\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory
  44. ) {
  45. $this->groupFactory = $groupFactory;
  46. $this->groupCollectionFactory = $groupCollectionFactory;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function get($id)
  52. {
  53. if (isset($this->entities[$id])) {
  54. return $this->entities[$id];
  55. }
  56. $group = $this->groupFactory->create([
  57. 'data' => $this->getAppConfig()->get('scopes', "groups/$id", [])
  58. ]);
  59. if (null === $group->getId()) {
  60. throw new NoSuchEntityException();
  61. }
  62. $this->entities[$id] = $group;
  63. return $group;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getList()
  69. {
  70. if (!$this->allLoaded) {
  71. $groups = $this->getAppConfig()->get('scopes', 'groups', []);
  72. foreach ($groups as $data) {
  73. $group = $this->groupFactory->create([
  74. 'data' => $data
  75. ]);
  76. $this->entities[$group->getId()] = $group;
  77. }
  78. $this->allLoaded = true;
  79. }
  80. return $this->entities;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function clean()
  86. {
  87. $this->entities = [];
  88. $this->allLoaded = false;
  89. }
  90. /**
  91. * Retrieve application config.
  92. *
  93. * @deprecated 100.1.3
  94. * @return Config
  95. */
  96. private function getAppConfig()
  97. {
  98. if (!$this->appConfig) {
  99. $this->appConfig = ObjectManager::getInstance()->get(Config::class);
  100. }
  101. return $this->appConfig;
  102. }
  103. }