-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStoreManager.php
158 lines (128 loc) · 4.35 KB
/
StoreManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Wizzy\Search\Services\Store;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
class StoreManager
{
private $storeManager;
public function __construct(StoreManagerInterface $storeManager)
{
$this->storeManager = $storeManager;
}
/**
* Get specific store by store ID
* @param $storeId
* @return StoreInterface|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getStoreById($storeId) : ?StoreInterface
{
return $this->storeManager->getStore($storeId);
}
public function getCurrentStoreId()
{
return $this->storeManager->getStore()->getId();
}
public function getCurrentStoreCurrency()
{
return $this->storeManager->getStore()->getCurrentCurrency();
}
public function getToSyncStoreIds($storeId = '')
{
$storeIds = [];
if ($storeId != "" && $storeId != "0") {
$storeIds [] = $storeId;
} else {
$storeIds = $this->getActivateWizzyStores();
}
return $storeIds;
}
public function getCredentials($storeId)
{
foreach ($this->storeManager->getStores() as $store) {
if ($store->getId() == $storeId) {
$storeConfigs = $store->getConfig('wizzy_store_credentials/store_credentials');
$storeId = trim($storeConfigs['store_id']);
$storeSecret = trim($storeConfigs['store_secret']);
$apiKey = trim($storeConfigs['api_key']);
return [
'storeId' => $storeId,
'storeSecret' => $storeSecret,
'apiKey' => $apiKey,
];
}
}
return null;
}
public function getActivateWizzyStores()
{
$storeIds = [];
foreach ($this->storeManager->getStores() as $store) {
$storeConfigs = $store->getConfig('wizzy_store_credentials/store_credentials');
if ($storeConfigs !== null && is_array($storeConfigs)) {
$storeId = trim($storeConfigs['store_id']);
$storeSecret = trim($storeConfigs['store_secret']);
$apiKey = trim($storeConfigs['api_key']);
if (!empty($storeId) && !empty($storeSecret) && !empty($apiKey)) {
$storeIds[] = $store->getId();
}
}
}
return $storeIds;
}
public function getActivateWizzyStoreNames()
{
$storeNames = [];
foreach ($this->storeManager->getStores() as $store) {
$storeConfigs = $store->getConfig('wizzy_store_credentials/store_credentials');
if ($storeConfigs !== null && is_array($storeConfigs)) {
$storeId = trim($storeConfigs['store_id']);
$storeSecret = trim($storeConfigs['store_secret']);
$apiKey = trim($storeConfigs['api_key']);
if (!empty($storeId) && !empty($storeSecret) && !empty($apiKey)) {
$storeNames[] = $store->getName();
}
}
}
return $storeNames;
}
public function getActiveWizzyWebsites()
{
$websiteIds = [];
foreach ($this->storeManager->getStores() as $store) {
$websiteId = $store->getWebsiteId();
$websiteIds[$websiteId] = true;
}
return array_keys($websiteIds);
}
public function getAllStores()
{
$storeIds = [];
foreach ($this->storeManager->getStores() as $store) {
$storeIds[] = $store->getId();
}
return $storeIds;
}
public function getAllStoreBaseUrls()
{
$baseUrls = [];
$storeIds = $this->getActivateWizzyStores();
foreach ($storeIds as $storeId) {
$store = $this->storeManager->getStore($storeId);
$storeId = $store->getId();
$baseUrl = $store->getBaseUrl();
$baseUrls[] = [
'store_id' => $storeId,
'base_url' => $baseUrl,
];
}
return $baseUrls;
}
public function getCurrentStoreBaseUrl()
{
return $currentStore = [
'store_id' => $this->storeManager->getStore()->getId(),
'base_url' => $this->storeManager->getStore()->getBaseUrl(),
];
}
}