Files

83 lines
2.4 KiB
PHP

<?php
/**
* @package Plumrocket_GlobalPrivacyControl
* @copyright Copyright (c) 2025 Plumrocket Inc.
* @license https://plumrocket.com/license End-user License Agreement
*/
declare(strict_types=1);
namespace Plumrocket\GlobalPrivacyControl\Model;
use Magento\Framework\Exception\LocalizedException;
use Plumrocket\GeoIPLookup\Api\GeoLocationValidatorInterface;
use Plumrocket\GlobalPrivacyControl\Api\GeoTargetingInterface;
use Plumrocket\GlobalPrivacyControl\Helper\Config;
use Plumrocket\GlobalPrivacyControl\Helper\Config\Geotargeting as GeotargetingConfig;
use Psr\Log\LoggerInterface;
class Geotargeting implements GeoTargetingInterface
{
/**
* @var \Plumrocket\GlobalPrivacyControl\Helper\Config
*/
private $config;
/**
* @var \Plumrocket\GlobalPrivacyControl\Helper\Config\Geotargeting
*/
private $geotargetingConfig;
/**
* @var \Plumrocket\GeoIPLookup\Api\GeoLocationValidatorInterface
*/
private $geoLocationValidator;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* @param \Plumrocket\GlobalPrivacyControl\Helper\Config $config
* @param \Plumrocket\GlobalPrivacyControl\Helper\Config\Geotargeting $geotargetingConfig
* @param \Plumrocket\GeoIPLookup\Api\GeoLocationValidatorInterface $geoLocationValidator
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
Config $config,
GeotargetingConfig $geotargetingConfig,
GeoLocationValidatorInterface $geoLocationValidator,
LoggerInterface $logger
) {
$this->config = $config;
$this->geotargetingConfig = $geotargetingConfig;
$this->geoLocationValidator = $geoLocationValidator;
$this->logger = $logger;
}
/**
* @return bool
*/
public function isApplicable(): bool
{
if (! $this->config->isModuleEnabled()) {
return false;
}
if (! $this->geotargetingConfig->isEnabled()) {
return true;
}
try {
return $this->geoLocationValidator->validateByMergedOptions(
$this->geotargetingConfig->getTargetCountries(),
$this->geotargetingConfig->getTargetUsStates()
);
} catch (LocalizedException $e) {
$this->logger->critical($e->getMessage());
return true;
}
}
}