Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACCOUNT-2706] fix: getUncachedConfiguration single shop context #436

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading