Skip to content

Commit

Permalink
[ACCOUNT-2706] fix: getUncachedConfiguration single shop context (#436)
Browse files Browse the repository at this point in the history
* fix: getUncachedConfiguration single shop context

* fix: don't fix the tests, fix the code !
  • Loading branch information
hschoenenberger authored Oct 4, 2024
1 parent a657573 commit 811ba50
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/Account/Session/ShopSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,21 @@ protected function getAccessToken($shopUid)
}

/**
* @param int $waitForOAuth2ClientSeconds
* @param int $oauth2ClientReceiptTimeout
*
* @return void
*
* @throws InconsistentAssociationStateException
*/
protected function assertAssociationState($waitForOAuth2ClientSeconds = 60)
protected function assertAssociationState($oauth2ClientReceiptTimeout = 60)
{
$linkedAtTs = $currentTs = time();
if ($this->linkShop->linkedAt()) {
$linkedAtTs = (new \DateTime($this->linkShop->linkedAt()))->getTimestamp();
}

if ($this->linkShop->exists() &&
$currentTs - $linkedAtTs > $waitForOAuth2ClientSeconds &&
$currentTs - $linkedAtTs > $oauth2ClientReceiptTimeout &&
!$this->oauth2ClientProvider->getOauth2Client()->exists()) {
throw new InconsistentAssociationStateException('Invalid OAuth2 client');
}
Expand Down
35 changes: 21 additions & 14 deletions src/Adapter/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,35 +183,30 @@ public function setGlobal($key, $values, $html = false)
* @param string|bool $default
*
* @return mixed
*
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function getUncached($key, $idShopGroup = null, $idShop = null, $default = false)
{
$id = \Configuration::getIdByName($key, $idShopGroup, $idShop);
if ($id > 0) {
$found = (new \Configuration($id));
$found->clearCache();

return $found->value;
try {
return $this->getUncachedConfiguration($key, $idShopGroup, $idShop)->value;
} catch (\Exception $e) {
return $default;
}

return $default;
}

/**
* @param string $key
* @param int|null $idShopGroup
* @param int|null $idShop
* @param mixed $default
*
* @return \Configuration
*
* @throw \Exception
*/
public function getUncachedConfiguration($key, $idShopGroup = null, $idShop = null, $default = false)
public function getUncachedConfiguration($key, $idShopGroup = null, $idShop = null)
{
if (!$this->isMultishopActive()) {
$idShopGroup = $idShop = null;
}
$id = \Configuration::getIdByName($key, $idShopGroup, $idShop);
if ($id > 0) {
$found = (new \Configuration($id));
Expand All @@ -220,6 +215,18 @@ public function getUncachedConfiguration($key, $idShopGroup = null, $idShop = nu
return $found;
}

throw new \Exception('Configuration entry not found');
throw new \Exception('Configuration entry not found: ' . $key . '|grp:' . $idShopGroup . '|shop:' . $idShop);
}

/**
* is multi-shop active "right now"
*
* @return bool
*/
public function isMultishopActive()
{
//return \Shop::isFeatureActive();
return \Db::getInstance()->getValue('SELECT value FROM `' . _DB_PREFIX_ . 'configuration` WHERE `name` = "PS_MULTISHOP_FEATURE_ACTIVE"')
&& (\Db::getInstance()->getValue('SELECT COUNT(*) FROM ' . _DB_PREFIX_ . 'shop') > 1);
}
}
5 changes: 3 additions & 2 deletions src/Repository/ConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ public function getShopUuidDateUpd()

return $entry->date_upd;
} catch (\Exception $e) {
Logger::getInstance()->error(__METHOD__ . ': ' . $e->getMessage());

return null;
}
}
Expand Down Expand Up @@ -386,8 +388,7 @@ public function fixMultiShopConfig()
public function isMultishopActive()
{
//return \Shop::isFeatureActive();
return \Db::getInstance()->getValue('SELECT value FROM `' . _DB_PREFIX_ . 'configuration` WHERE `name` = "PS_MULTISHOP_FEATURE_ACTIVE"')
&& (\Db::getInstance()->getValue('SELECT COUNT(*) FROM ' . _DB_PREFIX_ . 'shop') > 1);
return $this->configuration->isMultishopActive();
}

/**
Expand Down
3 changes: 0 additions & 3 deletions tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ function setUp(): void
$commandBus
);

// Fix single shop context
$this->configuration->setIdShop(null);
$this->configuration->setIdShopGroup(null);
$this->shopSession->cleanup();
}

Expand Down

0 comments on commit 811ba50

Please sign in to comment.