Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebsiteRepository.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Store\Model;
8 
11 use Magento\Store\Model\ResourceModel\Website\CollectionFactory;
13 
18 {
22  protected $factory;
23 
28 
32  protected $entities = [];
33 
37  protected $entitiesById = [];
38 
42  protected $allLoaded = false;
43 
47  protected $default;
48 
52  private $appConfig;
53 
58  public function __construct(
59  WebsiteFactory $factory,
60  CollectionFactory $websiteCollectionFactory
61  ) {
62  $this->factory = $factory;
63  $this->websiteCollectionFactory = $websiteCollectionFactory;
64  }
65 
69  public function get($code)
70  {
71  if (isset($this->entities[$code])) {
72  return $this->entities[$code];
73  }
74 
75  $websiteData = $this->getAppConfig()->get('scopes', "websites/$code", []);
76  $website = $this->factory->create([
77  'data' => $websiteData
78  ]);
79 
80  if ($website->getId() === null) {
81  throw new NoSuchEntityException();
82  }
83  $this->entities[$code] = $website;
84  $this->entitiesById[$website->getId()] = $website;
85  return $website;
86  }
87 
91  public function getById($id)
92  {
93  if (isset($this->entitiesById[$id])) {
94  return $this->entitiesById[$id];
95  }
96 
97  $websiteData = $this->getAppConfig()->get('scopes', "websites/$id", []);
98  $website = $this->factory->create([
99  'data' => $websiteData
100  ]);
101 
102  if ($website->getId() === null) {
103  throw new NoSuchEntityException();
104  }
105  $this->entities[$website->getCode()] = $website;
106  $this->entitiesById[$id] = $website;
107  return $website;
108  }
109 
113  public function getList()
114  {
115  if (!$this->allLoaded) {
116  $websites = $this->getAppConfig()->get('scopes', 'websites', []);
117  foreach ($websites as $data) {
118  $website = $this->factory->create([
119  'data' => $data
120  ]);
121  $this->entities[$website->getCode()] = $website;
122  $this->entitiesById[$website->getId()] = $website;
123  }
124  $this->allLoaded = true;
125  }
126  return $this->entities;
127  }
128 
132  public function getDefault()
133  {
134  if (!$this->default) {
135  foreach ($this->entities as $entity) {
136  if ($entity->getIsDefault()) {
137  $this->default = $entity;
138  return $this->default;
139  }
140  }
141  if (!$this->allLoaded) {
142  $this->initDefaultWebsite();
143  }
144  if (!$this->default) {
145  throw new \DomainException(__("The default website isn't defined. Set the website and try again."));
146  }
147  }
148 
149  return $this->default;
150  }
151 
155  public function clean()
156  {
157  $this->entities = [];
158  $this->entitiesById = [];
159  $this->default = null;
160  $this->allLoaded = false;
161  }
162 
169  private function getAppConfig()
170  {
171  if (!$this->appConfig) {
172  $this->appConfig = ObjectManager::getInstance()->get(Config::class);
173  }
174  return $this->appConfig;
175  }
176 
181  private function initDefaultWebsite()
182  {
183  $websites = (array) $this->getAppConfig()->get('scopes', 'websites', []);
184  foreach ($websites as $data) {
185  if (isset($data['is_default']) && $data['is_default'] == 1) {
186  if ($this->default) {
187  throw new \DomainException(
188  __(
189  'The default website is invalid. '
190  . 'Make sure no more than one default is defined and try again.'
191  )
192  );
193  }
194  $website = $this->factory->create([
195  'data' => $data
196  ]);
197  $this->default = $website;
198  $this->entities[$this->default->getCode()] = $this->default;
199  $this->entitiesById[$this->default->getId()] = $this->default;
200  }
201  }
202  }
203 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
__construct(WebsiteFactory $factory, CollectionFactory $websiteCollectionFactory)
$entity
Definition: element.phtml:22
$code
Definition: info.phtml:12