init
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Cart\Model;
|
||||
|
||||
use Magento\Customer\Model\Context as CustomerContext;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\Http\Context as HttpContext;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
class TestModeConfig
|
||||
{
|
||||
private const XML_PATH_ENABLED = 'esun_cart/test_mode/enabled';
|
||||
private const XML_PATH_ALLOWED_CUSTOMER_GROUPS = 'esun_cart/test_mode/allowed_customer_groups';
|
||||
private const XML_PATH_POPUP_TITLE = 'esun_cart/test_mode/popup_title';
|
||||
private const XML_PATH_POPUP_MESSAGE = 'esun_cart/test_mode/popup_message';
|
||||
private const DEFAULT_POPUP_TITLE = 'Test Mode';
|
||||
private const DEFAULT_POPUP_MESSAGE =
|
||||
'Our website is currently in beta testing. Purchases are not available at this time.';
|
||||
|
||||
public function __construct(
|
||||
private readonly ScopeConfigInterface $scopeConfig,
|
||||
private readonly HttpContext $httpContext
|
||||
) {
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLED, ScopeInterface::SCOPE_STORE);
|
||||
}
|
||||
|
||||
/** @return int[] */
|
||||
public function getAllowedCustomerGroupIds(): array
|
||||
{
|
||||
$value = (string)$this->scopeConfig->getValue(
|
||||
self::XML_PATH_ALLOWED_CUSTOMER_GROUPS,
|
||||
ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
|
||||
if ($value === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values(array_unique(array_map('intval', array_filter(explode(',', $value), 'strlen'))));
|
||||
}
|
||||
|
||||
public function isCurrentCustomerAllowed(): bool
|
||||
{
|
||||
$customerGroupId = (int)$this->httpContext->getValue(CustomerContext::CONTEXT_GROUP);
|
||||
|
||||
return in_array($customerGroupId, $this->getAllowedCustomerGroupIds(), true);
|
||||
}
|
||||
|
||||
public function isRestricted(): bool
|
||||
{
|
||||
return $this->isEnabled() && !$this->isCurrentCustomerAllowed();
|
||||
}
|
||||
|
||||
public function getPopupTitle(): string
|
||||
{
|
||||
return $this->getTextValue(self::XML_PATH_POPUP_TITLE, self::DEFAULT_POPUP_TITLE);
|
||||
}
|
||||
|
||||
public function getPopupMessage(): string
|
||||
{
|
||||
return $this->getTextValue(self::XML_PATH_POPUP_MESSAGE, self::DEFAULT_POPUP_MESSAGE);
|
||||
}
|
||||
|
||||
private function getTextValue(string $path, string $default): string
|
||||
{
|
||||
$value = trim((string)$this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE));
|
||||
|
||||
return $value !== '' ? $value : $default;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user