Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoreRepository.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Store\Model;
8 
12 
17 {
21  protected $storeFactory;
22 
27 
31  protected $entities = [];
32 
36  protected $entitiesById = [];
37 
41  protected $allLoaded = false;
42 
46  private $appConfig;
47 
52  public function __construct(
53  StoreFactory $storeFactory,
55  ) {
56  $this->storeFactory = $storeFactory;
57  $this->storeCollectionFactory = $storeCollectionFactory;
58  }
59 
63  public function get($code)
64  {
65  if (isset($this->entities[$code])) {
66  return $this->entities[$code];
67  }
68 
69  $storeData = $this->getAppConfig()->get('scopes', "stores/$code", []);
70  $store = $this->storeFactory->create([
71  'data' => $storeData
72  ]);
73 
74  if ($store->getId() === null) {
75  throw new NoSuchEntityException(
76  __("The store that was requested wasn't found. Verify the store and try again.")
77  );
78  }
79  $this->entities[$code] = $store;
80  $this->entitiesById[$store->getId()] = $store;
81  return $store;
82  }
83 
87  public function getActiveStoreByCode($code)
88  {
89  $store = $this->get($code);
90 
91  if (!$store->isActive()) {
92  throw new StoreIsInactiveException();
93  }
94  return $store;
95  }
96 
100  public function getById($id)
101  {
102  if (isset($this->entitiesById[$id])) {
103  return $this->entitiesById[$id];
104  }
105 
106  $storeData = $this->getAppConfig()->get('scopes', "stores/$id", []);
107  $store = $this->storeFactory->create([
108  'data' => $storeData
109  ]);
110 
111  if ($store->getId() === null) {
112  throw new NoSuchEntityException(
113  __("The store that was requested wasn't found. Verify the store and try again.")
114  );
115  }
116 
117  $this->entitiesById[$id] = $store;
118  $this->entities[$store->getCode()] = $store;
119  return $store;
120  }
121 
125  public function getActiveStoreById($id)
126  {
127  $store = $this->getById($id);
128 
129  if (!$store->isActive()) {
130  throw new StoreIsInactiveException();
131  }
132  return $store;
133  }
134 
138  public function getList()
139  {
140  if ($this->allLoaded) {
141  return $this->entities;
142  }
143  $stores = $this->getAppConfig()->get('scopes', "stores", []);
144  foreach ($stores as $data) {
145  $store = $this->storeFactory->create([
146  'data' => $data
147  ]);
148  $this->entities[$store->getCode()] = $store;
149  $this->entitiesById[$store->getId()] = $store;
150  }
151  $this->allLoaded = true;
152  return $this->entities;
153  }
154 
161  private function getAppConfig()
162  {
163  if (!$this->appConfig) {
164  $this->appConfig = ObjectManager::getInstance()->get(Config::class);
165  }
166  return $this->appConfig;
167  }
168 
172  public function clean()
173  {
174  $this->entities = [];
175  $this->entitiesById = [];
176  $this->allLoaded = false;
177  }
178 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
__construct(StoreFactory $storeFactory, \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory)
$code
Definition: info.phtml:12