use assertions for condition checks

This commit is contained in:
Daniel Seifert 2023-06-20 11:14:11 +02:00
parent 37ec1f8b3c
commit d47993ef64
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
2 changed files with 98 additions and 32 deletions

View File

@ -37,7 +37,8 @@
}, },
"require": { "require": {
"oxid-esales/oxideshop-ce": "6.8 - 6.14", "oxid-esales/oxideshop-ce": "6.8 - 6.14",
"d3/modcfg": "^6" "d3/modcfg": "^6",
"beberlei/assert": "^3.3"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* This Software is the property of Data Development and is protected * This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware. * by copyright law - it is NOT Freeware.
@ -16,6 +17,8 @@
namespace D3\GeoIp\Application\Model; namespace D3\GeoIp\Application\Model;
use Assert\Assert;
use Assert\InvalidArgumentException;
use D3\ModCfg\Application\Model\Configuration\d3_cfg_mod; use D3\ModCfg\Application\Model\Configuration\d3_cfg_mod;
use D3\ModCfg\Application\Model\d3str; use D3\ModCfg\Application\Model\d3str;
use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception; use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception;
@ -257,12 +260,9 @@ class d3geoip extends BaseModel
$oCountry = $this->getUserLocationCountryObject(); $oCountry = $this->getUserLocationCountryObject();
if (!$this->isAdmin() try {
&& Registry::getUtils()->isSearchEngine() === false $this->canChangeLanguage( $oCountry );
&& Registry::getSession()->getId()
&& Registry::getSession()->getVariable('d3isSetLang') === null
&& $oCountry->getId() && $oCountry->getFieldData('d3geoiplang') > -1
) {
$language = (int) $oCountry->getFieldData('d3geoiplang'); $language = (int) $oCountry->getFieldData('d3geoiplang');
$this->_getModConfig()->d3getLog()->info( $this->_getModConfig()->d3getLog()->info(
__CLASS__, __CLASS__,
@ -274,6 +274,9 @@ class d3geoip extends BaseModel
Registry::getLang()->setTplLanguage($language); Registry::getLang()->setTplLanguage($language);
Registry::getLang()->setBaseLanguage($language); Registry::getLang()->setBaseLanguage($language);
Registry::getSession()->setVariable('d3isSetLang', true); Registry::getSession()->setVariable('d3isSetLang', true);
Registry::getLogger()->debug('language set', [$language]);
} catch (InvalidArgumentException $e) {
Registry::getLogger()->debug($e->getMessage(), [$oCountry]);
} }
stopProfile(__METHOD__); stopProfile(__METHOD__);
@ -301,12 +304,9 @@ class d3geoip extends BaseModel
$oCountry = $this->getUserLocationCountryObject(); $oCountry = $this->getUserLocationCountryObject();
if (!$this->isAdmin() try {
&& Registry::getUtils()->isSearchEngine() === false $this->canChangeCurrency( $oCountry );
&& !Registry::getSession()->getVariable('d3isSetCurr')
&& $oCountry->getId()
&& $oCountry->getFieldData('d3geoipcur') > -1
) {
$this->_getLog()->log( $this->_getLog()->log(
d3log::INFO, d3log::INFO,
__CLASS__, __CLASS__,
@ -317,6 +317,8 @@ class d3geoip extends BaseModel
); );
Registry::getConfig()->setActShopCurrency((int) $oCountry->getFieldData('d3geoipcur')); Registry::getConfig()->setActShopCurrency((int) $oCountry->getFieldData('d3geoipcur'));
Registry::getSession()->setVariable('d3isSetCurr', true); Registry::getSession()->setVariable('d3isSetCurr', true);
} catch (InvalidArgumentException $e) {
Registry::getLogger()->debug($e->getMessage(), [$oCountry]);
} }
stopProfile(__METHOD__); stopProfile(__METHOD__);
@ -369,19 +371,10 @@ class d3geoip extends BaseModel
$this->_getModConfig()->d3getLog()->info(__CLASS__, __FUNCTION__, __LINE__, 'check allowed shop change'); $this->_getModConfig()->d3getLog()->info(__CLASS__, __FUNCTION__, __LINE__, 'check allowed shop change');
if (Registry::getRequest()->getRequestEscapedParameter('d3redirect') != 1 try {
&& false == $this->isAdmin() $this->canChangeShop( $oCountry, $iNewShop );
&& Registry::getUtils()->isSearchEngine() === false
&& $oCountry->getId()
&& Registry::getConfig()->isMall()
&& $iNewShop > -1 &&
(
$iNewShop != Registry::getConfig()->getShopId()
|| strtolower(Registry::getConfig()->getActiveView()->getClassKey()) == 'mallstart'
)
) {
$this->_getModConfig()->d3getLog()->info(__CLASS__, __FUNCTION__, __LINE__, 'prepare shop change to '.$iNewShop);
$this->_getModConfig()->d3getLog()->info(__CLASS__, __FUNCTION__, __LINE__, 'prepare shop change to '.$iNewShop);
$oNewConf = new Config(); $oNewConf = new Config();
$oNewConf->setShopId($iNewShop); $oNewConf->setShopId($iNewShop);
$oNewConf->init(); $oNewConf->init();
@ -423,6 +416,8 @@ class d3geoip extends BaseModel
header("Location: ".$sUrl); header("Location: ".$sUrl);
exit(); exit();
} catch (InvalidArgumentException $e) {
Registry::getLogger()->debug($e->getMessage(), [$oCountry]);
} }
stopProfile(__METHOD__); stopProfile(__METHOD__);
@ -453,13 +448,9 @@ class d3geoip extends BaseModel
$oCountry = $this->getUserLocationCountryObject(); $oCountry = $this->getUserLocationCountryObject();
if ($this->dontSkipUrlRedirect() try {
&& false == $this->isAdmin() $this->canRedirect( $oCountry );
&& Registry::getUtils()->isSearchEngine() === false
&& $oCountry->getId()
&& $oCountry->getFieldData('d3geoipurl')
&& strlen(trim($oCountry->getFieldData('d3geoipurl'))) > 0
) {
$sNewUrl = $oCountry->getFieldData('d3geoipurl'); $sNewUrl = $oCountry->getFieldData('d3geoipurl');
$this->_getLog()->log( $this->_getLog()->log(
@ -473,6 +464,8 @@ class d3geoip extends BaseModel
header("Location: ".$sNewUrl); header("Location: ".$sNewUrl);
exit(); exit();
} catch (InvalidArgumentException $e) {
Registry::getLogger()->debug($e->getMessage(), [$oCountry]);
} }
stopProfile(__METHOD__); stopProfile(__METHOD__);
@ -540,5 +533,77 @@ class d3geoip extends BaseModel
return $this->oD3Log; return $this->oD3Log;
} }
/**
* @param Country $oCountry
*
* @throws InvalidArgumentException
*/
protected function canRedirect( Country $oCountry )
{
Assert::that($this->dontSkipUrlRedirect())->true('redirect skip is not provided');
$this->isUserInFrontend();
Assert::that($oCountry->getId())->notBlank('no country loaded');
Assert::that($oCountry->getFieldData( 'd3geoipurl' ))->notBlank('no redirect url set');
}
/**
* @param Country $oCountry
*
* @return bool
*/
/**
* @param Country $oCountry
*
* @throws InvalidArgumentException
*/
protected function canChangeLanguage( Country $oCountry )
{
$this->isUserInFrontend();
Assert::that(Registry::getSession()->getVariable( 'd3isSetLang' ))->null('language already set');
Assert::that($oCountry->getId())->notBlank('no country loaded');
Assert::that($oCountry->getFieldData( 'd3geoiplang' ))->greaterThan(-1, 'no target language set');
}
/**
* @param Country $oCountry
*
* @throws InvalidArgumentException
*/
protected function canChangeCurrency( Country $oCountry )
{
$this->isUserInFrontend();
Assert::that(Registry::getSession()->getVariable( 'd3isSetCurr' ))->null('currency already set');
Assert::that($oCountry->getId())->notBlank('no country loaded');
Assert::that($oCountry->getFieldData( 'd3geoipcur' ))->greaterThan(-1, 'no target currency set');
}
/**
* @param Country $oCountry
* @param $iNewShop
*
* @throws InvalidArgumentException
*/
protected function canChangeShop( Country $oCountry, $iNewShop )
{
Assert::that(Registry::getRequest()->getRequestEscapedParameter( 'd3redirect' ))->notSame('1', 'redirect already performed');
$this->isUserInFrontend();
Assert::that($oCountry->getId())->notBlank('no country loaded');
Assert::that(Registry::getConfig()->isMall())->true('no mall installation');
Assert::that($iNewShop)->greaterThan(-1, 'no target shop id set');
Assert::that(
$iNewShop != Registry::getConfig()->getShopId() ||
strtolower( Registry::getConfig()->getActiveView()->getClassKey() ) == 'mallstart'
)->true('no different shop and no mallstart');
}
/**
* @throws InvalidArgumentException
*/
protected function isUserInFrontend()
{
Assert::that($this->isAdmin())->false('User is in admin');
Assert::that(Registry::getUtils()->isSearchEngine())->false('User is search engine');
}
} }