Files
esun3d/app/code/Esun/PromotionPopup/Model/Config.php
T
2026-09-15 10:35:56 +08:00

124 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\PromotionPopup\Model;
use Esun\PromotionPopup\Model\Config\Source\Audience;
use Esun\PromotionPopup\Model\Config\Source\Frequency;
use Esun\PromotionPopup\Model\Config\Source\PromotionType;
use Magento\Cms\Api\GetBlockByIdentifierInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
class Config
{
private const XML_PATH_PREFIX = 'esun_promotion_popup/general/';
public function __construct(
private readonly ScopeConfigInterface $scopeConfig,
private readonly StoreManagerInterface $storeManager,
private readonly TimezoneInterface $timezone,
private readonly RequestInterface $request,
private readonly GetBlockByIdentifierInterface $blockByIdentifier
) {
}
public function canDisplay(): bool
{
if (!$this->isCampaignActive()) {
return false;
}
return !in_array($this->request->getFullActionName(), $this->getExcludedHandles(), true);
}
public function isCampaignActive(): bool
{
$identifier = $this->getCmsBlockIdentifier();
$store = $this->storeManager->getStore();
if (!$this->isEnabled()
|| $identifier === ''
|| !$this->timezone->isScopeDateInInterval(
$store,
$this->getValue('start_date') ?: null,
$this->getValue('end_date') ?: null
)
) {
return false;
}
try {
return $this->blockByIdentifier->execute($identifier, (int)$store->getId())->isActive();
} catch (NoSuchEntityException) {
return false;
}
}
public function isRegistrationNewsletterCouponPromotion(): bool
{
return $this->getValue('promotion_type') === PromotionType::REGISTRATION_NEWSLETTER_COUPON;
}
public function isEnabled(): bool
{
return $this->scopeConfig->isSetFlag(
self::XML_PATH_PREFIX . 'enabled',
ScopeInterface::SCOPE_STORE
);
}
public function getCmsBlockIdentifier(): string
{
return trim($this->getValue('cms_block_identifier'));
}
public function getWebsiteId(): int
{
return (int)$this->storeManager->getStore()->getWebsiteId();
}
public function getDisplayDelay(): int
{
return min(60000, max(0, (int)$this->getValue('display_delay')));
}
public function getFrequency(): string
{
$frequency = $this->getValue('frequency');
$allowed = [Frequency::ALWAYS, Frequency::SESSION, Frequency::DAILY, Frequency::CAMPAIGN];
return in_array($frequency, $allowed, true) ? $frequency : Frequency::DAILY;
}
public function getAudience(): string
{
return $this->getValue('audience') === Audience::ALL ? Audience::ALL : Audience::GUEST;
}
public function getCampaignVersion(): int
{
return max(1, (int)$this->getValue('campaign_version'));
}
/** @return string[] */
private function getExcludedHandles(): array
{
return array_values(array_unique(array_filter(array_map(
static fn(string $handle): string => trim($handle),
preg_split('/[\r\n,]+/', $this->getValue('excluded_handles')) ?: []
))));
}
private function getValue(string $field): string
{
return (string)$this->scopeConfig->getValue(
self::XML_PATH_PREFIX . $field,
ScopeInterface::SCOPE_STORE
);
}
}