Skip to content

Commit 1e3299c

Browse files
committed
Refactoring.
1 parent e35fb04 commit 1e3299c

32 files changed

+350
-635
lines changed

Block/Adminhtml/System/Config/Form.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Form extends \Magento\Config\Block\System\Config\Form
99
/**
1010
* @var \Tobai\GeoStoreSwitcher\Model\Config\System\GroupGeneratorInterface
1111
*/
12-
protected $groupGenerator;
12+
private $groupGenerator;
1313

1414
/**
1515
* @param \Magento\Backend\Block\Template\Context $context

Helper/Config/AppState.php

-39
This file was deleted.

Helper/Config/Request.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ class Request
99
/**
1010
* @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
1111
*/
12-
protected $remoteAddress;
12+
private $remoteAddress;
1313

1414
/**
1515
* @var \Magento\Framework\HTTP\Header
1616
*/
17-
protected $httpHeader;
17+
private $httpHeader;
1818

1919
/**
2020
* @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress

Model/Config/Backend/Regexp.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Regexp extends \Magento\Framework\App\Config\Value
99
/**
1010
* @var \Magento\Framework\Message\ManagerInterface
1111
*/
12-
protected $messageManager;
12+
private $messageManager;
1313

1414
/**
1515
* @param \Magento\Framework\Model\Context $context

Model/Config/Backend/ScopeConfig.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 ToBai. All rights reserved.
4+
*/
5+
namespace Tobai\GeoStoreSwitcher\Model\Config\Backend;
6+
7+
use Magento\Store\Model\ScopeInterface;
8+
9+
class ScopeConfig extends \Tobai\GeoStoreSwitcher\Model\Config\ScopeConfig
10+
{
11+
/**
12+
* @var \Tobai\GeoStoreSwitcher\Model\Config\ScopeDefiner
13+
*/
14+
private $scopeDefiner;
15+
16+
/**
17+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $appScopeConfig
18+
* @param \Tobai\GeoStoreSwitcher\Model\Config\ScopeDefiner $scopeDefiner
19+
*/
20+
public function __construct(
21+
\Magento\Framework\App\Config\ScopeConfigInterface $appScopeConfig,
22+
\Tobai\GeoStoreSwitcher\Model\Config\ScopeDefiner $scopeDefiner
23+
) {
24+
$this->scopeDefiner = $scopeDefiner;
25+
parent::__construct($appScopeConfig);
26+
}
27+
28+
/**
29+
* @param string $path
30+
* @return string
31+
*/
32+
public function getStoreValue($path)
33+
{
34+
return $this->appScopeConfig->getValue($path, $this->scopeDefiner->getScope(), $this->scopeDefiner->getScopeValue());
35+
}
36+
37+
/**
38+
* @param string $path
39+
* @return string
40+
*/
41+
public function getWebsiteValue($path)
42+
{
43+
return $this->scopeDefiner->getScope() != ScopeInterface::SCOPE_STORE
44+
? $this->appScopeConfig->getValue($path, $this->scopeDefiner->getScope(), $this->scopeDefiner->getScopeValue())
45+
: '';
46+
}
47+
}

Model/Config/General.php

+12-24
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,45 @@
44
*/
55
namespace Tobai\GeoStoreSwitcher\Model\Config;
66

7-
use Magento\Store\Api\Data\StoreInterface;
8-
97
class General
108
{
119
/**
1210
* @var \Tobai\GeoStoreSwitcher\Model\Config\ScopeConfig
1311
*/
14-
protected $scopeConfig;
15-
16-
/**
17-
* @var \Tobai\GeoStoreSwitcher\Helper\Config\AppState
18-
*/
19-
protected $appStateHelper;
12+
private $scopeConfig;
2013

2114
/**
2215
* @var \Tobai\GeoStoreSwitcher\Helper\Config\Request
2316
*/
24-
protected $requestHelper;
17+
private $requestHelper;
2518

2619
/**
2720
* @param \Tobai\GeoStoreSwitcher\Model\Config\ScopeConfig $scopeConfig
28-
* @param \Tobai\GeoStoreSwitcher\Helper\Config\AppState $appStateHelper
2921
* @param \Tobai\GeoStoreSwitcher\Helper\Config\Request $requestHelper
3022
*/
3123
public function __construct(
3224
\Tobai\GeoStoreSwitcher\Model\Config\ScopeConfig $scopeConfig,
33-
\Tobai\GeoStoreSwitcher\Helper\Config\AppState $appStateHelper,
3425
\Tobai\GeoStoreSwitcher\Helper\Config\Request $requestHelper
3526
) {
3627
$this->scopeConfig = $scopeConfig;
37-
$this->appStateHelper = $appStateHelper;
3828
$this->requestHelper = $requestHelper;
3929
}
4030

4131
/**
42-
* @param \Magento\Store\Api\Data\StoreInterface $store
43-
* @return $this
32+
* @param \Tobai\GeoStoreSwitcher\Model\Config\ScopeConfig $scopeConfig
33+
* @return void
4434
*/
45-
public function setOriginStore(StoreInterface $store)
35+
public function setScopeConfig(\Tobai\GeoStoreSwitcher\Model\Config\ScopeConfig $scopeConfig)
4636
{
47-
$this->scopeConfig->setOriginStore($store);
48-
return $this;
37+
$this->scopeConfig = $scopeConfig;
4938
}
5039

5140
/**
5241
* @return bool
5342
*/
5443
public function isAvailable()
5544
{
56-
return $this->appStateHelper->isFrontendArea()
57-
&& !$this->requestHelper->isCurrentIp($this->getWhiteIps())
45+
return !$this->requestHelper->isCurrentIp($this->getWhiteIps())
5846
&& !$this->requestHelper->isCurrentUserAgent($this->getWhiteUa())
5947
&& $this->isActive();
6048
}
@@ -64,7 +52,7 @@ public function isAvailable()
6452
*/
6553
public function isActive()
6654
{
67-
return $this->scopeConfig->getFrontendStoreOrBackendValue('tobai_geo_store_switcher/general/active');
55+
return $this->scopeConfig->getStoreValue('tobai_geo_store_switcher/general/active');
6856
}
6957

7058
/**
@@ -116,15 +104,15 @@ public function isMappingSore()
116104
public function isCountries()
117105
{
118106
return $this->isActive()
119-
&& $this->scopeConfig->getFrontendWebsiteOrBackendValue('tobai_geo_store_switcher/general/by_countries');
107+
&& $this->scopeConfig->getWebsiteValue('tobai_geo_store_switcher/general/by_countries');
120108
}
121109

122110
/**
123111
* @return array
124112
*/
125113
public function getCountryList()
126114
{
127-
$countriesData = $this->scopeConfig->getFrontendWebsiteOrBackendValue('tobai_geo_store_switcher/general/country_list');
115+
$countriesData = $this->scopeConfig->getWebsiteValue('tobai_geo_store_switcher/general/country_list');
128116
$countries = $this->isCountries() && !empty($countriesData) ? explode(',', $countriesData) : [];
129117
return $countries;
130118
}
@@ -144,7 +132,7 @@ public function getCountryStore($countryCode)
144132
public function getGroupCount()
145133
{
146134
return $this->isActive()
147-
? (int)$this->scopeConfig->getFrontendWebsiteOrBackendValue('tobai_geo_store_switcher/general/by_groups')
135+
? (int)$this->scopeConfig->getWebsiteValue('tobai_geo_store_switcher/general/by_groups')
148136
: 0;
149137
}
150138

@@ -154,7 +142,7 @@ public function getGroupCount()
154142
*/
155143
public function getGroupCountryList($group)
156144
{
157-
$countriesData = $this->scopeConfig->getFrontendWebsiteOrBackendValue("tobai_geo_store_switcher/group_{$group}/country_list");
145+
$countriesData = $this->scopeConfig->getWebsiteValue("tobai_geo_store_switcher/group_{$group}/country_list");
158146
$countries = !empty($countriesData) ? explode(',', $countriesData) : [];
159147
return $countries;
160148
}

Model/Config/ScopeCodeResolver.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 ToBai. All rights reserved.
4+
*/
5+
namespace Tobai\GeoStoreSwitcher\Model\Config;
6+
7+
use Magento\Framework\App\Config\ScopeConfigInterface;
8+
9+
class ScopeCodeResolver extends \Magento\Framework\App\Config\ScopeCodeResolver
10+
{
11+
/**
12+
* @var \Magento\Framework\App\ScopeResolverPool
13+
*/
14+
private $scopeResolverPool;
15+
16+
/**
17+
* @var array
18+
*/
19+
private $resolvedScopeCodes = [];
20+
21+
/**
22+
* @param \Magento\Framework\App\ScopeResolverPool $scopeResolverPool
23+
*/
24+
public function __construct(
25+
\Magento\Framework\App\ScopeResolverPool $scopeResolverPool
26+
) {
27+
$this->scopeResolverPool = $scopeResolverPool;
28+
}
29+
30+
/**
31+
* Resolve scope code
32+
*
33+
* @param string $scopeType
34+
* @param string $scopeCode
35+
* @return string
36+
*/
37+
public function resolve($scopeType, $scopeCode)
38+
{
39+
if (isset($this->resolvedScopeCodes[$scopeType][$scopeCode])) {
40+
return $this->resolvedScopeCodes[$scopeType][$scopeCode];
41+
}
42+
if (($scopeCode === null || is_numeric($scopeCode))
43+
&& $scopeType !== ScopeConfigInterface::SCOPE_TYPE_DEFAULT
44+
) {
45+
$scopeResolver = $this->scopeResolverPool->get($scopeType);
46+
$resolverScopeCode = $scopeResolver->getScope($scopeCode);
47+
} else {
48+
$resolverScopeCode = $scopeCode;
49+
}
50+
51+
if ($resolverScopeCode instanceof \Magento\Framework\App\ScopeInterface) {
52+
$resolverScopeCode = $resolverScopeCode->getCode();
53+
}
54+
55+
$this->resolvedScopeCodes[$scopeType][$scopeCode] = $resolverScopeCode;
56+
return $resolverScopeCode;
57+
}
58+
59+
/**
60+
* @return void
61+
*/
62+
public function reset()
63+
{
64+
$this->resolvedScopeCodes = [];
65+
}
66+
}

0 commit comments

Comments
 (0)