Skip to content

Commit aa75f8a

Browse files
hschoenenbergerantoinemetifeu
authored andcommitted
feat: get last upgraded version (#513)
* feat: get last upgraded version from ps_module table * feat: introduce UpgradeService
1 parent 9476085 commit aa75f8a

File tree

5 files changed

+137
-45
lines changed

5 files changed

+137
-45
lines changed

ps_accounts.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
exit;
2222
}
2323
require_once __DIR__ . '/vendor/autoload.php';
24-
//require __DIR__ . '/src/autoload_module.php';
2524

26-
//if (!class_exists('\PrestaShop\Module\PsAccounts\Hook\HookableTrait')) {
27-
// ps_accounts_fix_upgrade();
28-
//}
25+
if (!class_exists('\PrestaShop\Module\PsAccounts\Hook\HookableTrait')) {
26+
ps_accounts_fix_upgrade();
27+
}
2928

3029
class Ps_accounts extends Module
3130
{
@@ -476,20 +475,20 @@ public function getVerifiedStatus()
476475
return false;
477476
}
478477
}
479-
//
480-
///**
481-
// * @return void
482-
// */
483-
//function ps_accounts_fix_upgrade()
484-
//{
485-
// $root = __DIR__;
486-
// $requires = array_merge([
487-
// $root . '/src/Module/Install.php',
488-
//// $root . '/src/Hook/Hook.php',
489-
// $root . '/src/Hook/HookableTrait.php',
490-
// ], []/*, glob($root . '/src/Hook/*.php')*/);
491-
//
492-
// foreach ($requires as $filename) {
493-
// require_once $filename;
494-
// }
495-
//}
478+
479+
/**
480+
* @return void
481+
*/
482+
function ps_accounts_fix_upgrade()
483+
{
484+
$root = __DIR__;
485+
$requires = array_merge([
486+
$root . '/src/Module/Install.php',
487+
// $root . '/src/Hook/Hook.php',
488+
$root . '/src/Hook/HookableTrait.php',
489+
], []/*, glob($root . '/src/Hook/*.php')*/);
490+
491+
foreach ($requires as $filename) {
492+
require_once $filename;
493+
}
494+
}

src/Account/CommandHandler/MigrateOrCreateIdentityV8Handler.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsService;
3636
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2Exception;
3737
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2Service;
38+
use PrestaShop\Module\PsAccounts\Service\UpgradeService;
3839

3940
class MigrateOrCreateIdentityV8Handler
4041
{
@@ -68,6 +69,11 @@ class MigrateOrCreateIdentityV8Handler
6869
*/
6970
private $configurationRepository;
7071

72+
/**
73+
* @var UpgradeService
74+
*/
75+
private $upgradeService;
76+
7177
/**
7278
* @var CommandBus
7379
*/
@@ -98,6 +104,7 @@ public function __construct(
98104
$this->proofManager = $proofManager;
99105
$this->configurationRepository = $configurationRepository;
100106
$this->commandBus = $commandBus;
107+
$this->upgradeService = new UpgradeService($configurationRepository);
101108
}
102109

103110
/**
@@ -109,12 +116,17 @@ public function handle(MigrateOrCreateIdentityV8Command $command)
109116
{
110117
$shopId = $command->shopId ?: \Shop::getContextShopID();
111118
$shopUuid = $this->configurationRepository->getShopUuid();
112-
$lastUpgradedVersion = $this->configurationRepository->getLastUpgrade(false);
119+
120+
$fromVersion = $this->upgradeService->getRegisteredVersion();
121+
if ($fromVersion === '0') {
122+
$fromVersion = $this->upgradeService->getCoreRegisteredVersion();
123+
}
113124

114125
$e = null;
115126
try {
116-
if (!$shopUuid || version_compare($lastUpgradedVersion, '8', '>=')) {
117-
$this->upgradeVersionNumber();
127+
// FIXME: shouldn't this condition be a specific flag
128+
if (!$shopUuid || version_compare($fromVersion, '8', '>=')) {
129+
$this->upgradeService->setRegisteredVersion();
118130

119131
$this->commandBus->handle(new CreateIdentityCommand($command->shopId));
120132

@@ -124,19 +136,18 @@ public function handle(MigrateOrCreateIdentityV8Command $command)
124136
// migrate cloudShopId locally
125137
$this->statusManager->setCloudShopId($shopUuid);
126138

127-
if (version_compare($lastUpgradedVersion, '7', '>=')) {
139+
if (version_compare($fromVersion, '7', '>=')) {
128140
$token = $this->getAccessTokenV7($shopUuid);
129141
} else {
130142
$token = $this->getFirebaseTokenV6($shopUuid);
131143
}
132144

133-
// FIXME getLastUpgradedVersion from PS Core ?
134145
$identityCreated = $this->accountsService->migrateShopIdentity(
135146
$shopUuid,
136147
$token,
137148
$this->shopProvider->getUrl($shopId),
138149
$this->proofManager->generateProof(),
139-
$lastUpgradedVersion
150+
$fromVersion
140151
);
141152

142153
if (!empty($identityCreated->clientId) &&
@@ -152,8 +163,7 @@ public function handle(MigrateOrCreateIdentityV8Command $command)
152163

153164
$this->statusManager->invalidateCache();
154165

155-
// update ps_accounts upgraded version
156-
$this->upgradeVersionNumber();
166+
$this->upgradeService->setRegisteredVersion();
157167
} catch (OAuth2Exception $e) {
158168
} catch (AccountsException $e) {
159169
} catch (RefreshTokenException $e) {
@@ -194,12 +204,4 @@ protected function getFirebaseTokenV6($shopUuid)
194204
$shopUuid
195205
)->token;
196206
}
197-
198-
/**
199-
* @return void
200-
*/
201-
protected function upgradeVersionNumber()
202-
{
203-
$this->configurationRepository->updateLastUpgrade(\Ps_accounts::VERSION);
204-
}
205207
}

src/Service/UpgradeService.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace PrestaShop\Module\PsAccounts\Service;
4+
5+
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
6+
7+
class UpgradeService
8+
{
9+
/**
10+
* @var ConfigurationRepository
11+
*/
12+
private $repository;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $moduleName = 'ps_accounts';
18+
19+
/**
20+
* @param ConfigurationRepository $configurationRepository
21+
*/
22+
public function __construct(ConfigurationRepository $configurationRepository)
23+
{
24+
$this->repository = $configurationRepository;
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getRegisteredVersion()
31+
{
32+
return $this->repository->getLastUpgrade(false);
33+
}
34+
35+
/**
36+
* @param string $version
37+
*
38+
* @return void
39+
*/
40+
public function setRegisteredVersion($version = \Ps_accounts::VERSION)
41+
{
42+
$this->repository->updateLastUpgrade($version);
43+
}
44+
45+
/**
46+
* @return string
47+
*/
48+
public function getCoreRegisteredVersion()
49+
{
50+
return \Db::getInstance()->getValue(
51+
'SELECT version FROM ' . _DB_PREFIX_ . 'module WHERE name = \'' . $this->moduleName . '\''
52+
) ?: '0';
53+
}
54+
55+
/**
56+
* @param string $version
57+
*
58+
* @return void
59+
*/
60+
public function setCoreRegisteredVersion($version = \Ps_accounts::VERSION)
61+
{
62+
\Db::getInstance()->execute(
63+
'UPDATE ' . _DB_PREFIX_ . 'module SET version = \'' . $version . '\' WHERE name = \'' . $this->moduleName . '\''
64+
);
65+
}
66+
}

tests/src/Unit/Account/CommandHandler/MigrateOrCreateIdentityV8HandlerTest.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsService;
1414
use PrestaShop\Module\PsAccounts\Provider\ShopProvider;
1515
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2Service;
16+
use PrestaShop\Module\PsAccounts\Service\UpgradeService;
1617
use PrestaShop\Module\PsAccounts\Tests\TestCase;
1718

1819
class MigrateOrCreateIdentityV8HandlerTest extends TestCase
@@ -55,6 +56,11 @@ class MigrateOrCreateIdentityV8HandlerTest extends TestCase
5556
*/
5657
public $statusManager;
5758

59+
/**
60+
* @var UpgradeService
61+
*/
62+
public $upgradeService;
63+
5864
/**
5965
* @inject
6066
*
@@ -94,6 +100,7 @@ public function set_up()
94100
$this->oAuth2Client = $this->createMock(Client::class);
95101
$this->accountsService->setClient($this->accountsClient);
96102
$this->oAuth2Service->setHttpClient($this->oAuth2Client);
103+
$this->upgradeService = new UpgradeService($this->configurationRepository);
97104

98105
// $this->accountsService = $this->createMock(AccountsService::class);
99106
// $this->oAuth2Service = $this->createMock(OAuth2Service::class);
@@ -113,7 +120,8 @@ public function itShouldMigrateIdentityFromV7()
113120
$tokenAudience = 'shop_' . $cloudShopId;
114121

115122
// introduced in v7
116-
$this->configurationRepository->updateLastUpgrade('7.2.0');
123+
//$this->configurationRepository->updateLastUpgrade('7.2.0');
124+
$this->upgradeService->setRegisteredVersion('7.2.0');
117125

118126
$this->configurationRepository->updateShopUuid($cloudShopId);
119127

@@ -149,7 +157,8 @@ public function itShouldMigrateIdentityFromV7()
149157
$this->assertArrayHasKey('frontendUrl', $options[Request::JSON]);
150158
$this->assertArrayHasKey('multiShopId', $options[Request::JSON]);
151159
$this->assertEquals($this->proofManager->getProof(), $options[Request::JSON]['proof']);
152-
$this->assertEquals((string) $this->configurationRepository->getLastUpgrade(), $options[Request::JSON]['fromVersion']);
160+
//$this->assertEquals((string) $this->configurationRepository->getLastUpgrade(), $options[Request::JSON]['fromVersion']);
161+
$this->assertEquals((string) $this->upgradeService->getRegisteredVersion(), $options[Request::JSON]['fromVersion']);
153162

154163
return $this->createResponse([
155164
'clientId' => $clientId,
@@ -160,6 +169,7 @@ public function itShouldMigrateIdentityFromV7()
160169

161170
$this->getHandler()->handle(new MigrateOrCreateIdentityV8Command($this->shopId));
162171

172+
$this->assertEquals(\Ps_accounts::VERSION, $this->upgradeService->getRegisteredVersion());
163173
$this->assertEmpty($this->configurationRepository->getAccessToken());
164174
$this->assertTrue($this->statusManager->cacheInvalidated());
165175
$this->assertEquals($cloudShopId, $this->statusManager->getCloudShopId());
@@ -237,7 +247,12 @@ public function itShouldMigrateIdentityFromV5AndV6()
237247
$token = $this->faker->uuid;
238248

239249
// introduced in v7
240-
$this->configurationRepository->updateLastUpgrade(null);
250+
//$this->configurationRepository->updateLastUpgrade(null);
251+
252+
$fromVersion = '5.6.2';
253+
$this->upgradeService->setCoreRegisteredVersion($fromVersion);
254+
// FIXME: not working with null on v9
255+
$this->upgradeService->setRegisteredVersion('0');
241256

242257
$this->configurationRepository->updateShopUuid($cloudShopId);
243258

@@ -258,14 +273,15 @@ public function itShouldMigrateIdentityFromV5AndV6()
258273
$this->matches('/v1/shop-identities/' . $cloudShopId . '/migrate'),
259274
$this->isType('array')
260275
)
261-
->willReturnCallback(function ($route, $options) use ($cloudShopId, $clientId, $clientSecret, $token) {
276+
->willReturnCallback(function ($route, $options) use ($cloudShopId, $clientId, $clientSecret, $token, $fromVersion) {
262277

263278
$this->assertEquals('Bearer ' . $token, $options[Request::HEADERS]['Authorization']);
264279
$this->assertArrayHasKey('backOfficeUrl', $options[Request::JSON]);
265280
$this->assertArrayHasKey('frontendUrl', $options[Request::JSON]);
266281
$this->assertArrayHasKey('multiShopId', $options[Request::JSON]);
267282
$this->assertEquals($this->proofManager->getProof(), $options[Request::JSON]['proof']);
268-
$this->assertEquals((string) $this->configurationRepository->getLastUpgrade(), $options[Request::JSON]['fromVersion']);
283+
//$this->assertEquals((string) $this->configurationRepository->getLastUpgrade(), $options[Request::JSON]['fromVersion']);
284+
$this->assertEquals($fromVersion, $options[Request::JSON]['fromVersion']);
269285

270286
return $this->createResponse([
271287
'clientId' => $clientId,
@@ -276,6 +292,7 @@ public function itShouldMigrateIdentityFromV5AndV6()
276292

277293
$this->getHandler()->handle(new MigrateOrCreateIdentityV8Command($this->shopId));
278294

295+
$this->assertEquals(\Ps_accounts::VERSION, $this->upgradeService->getRegisteredVersion());
279296
$this->assertTrue($this->statusManager->cacheInvalidated());
280297
$this->assertEquals($cloudShopId, $this->statusManager->getCloudShopId());
281298
$this->assertEquals($clientId, $this->oAuth2Service->getOAuth2Client()->getClientId());
@@ -294,7 +311,9 @@ public function itShouldNotMigrateOnError()
294311
$tokenAudience = 'shop_' . $cloudShopId;
295312

296313
// introduced in v7
297-
$this->configurationRepository->updateLastUpgrade('7.2.0');
314+
//$this->configurationRepository->updateLastUpgrade('7.2.0');
315+
$fromVersion = '7.2.0';
316+
$this->upgradeService->setRegisteredVersion($fromVersion);
298317

299318
$this->configurationRepository->updateShopUuid($cloudShopId);
300319

@@ -330,7 +349,8 @@ public function itShouldNotMigrateOnError()
330349
$this->assertArrayHasKey('frontendUrl', $options[Request::JSON]);
331350
$this->assertArrayHasKey('multiShopId', $options[Request::JSON]);
332351
$this->assertEquals($this->proofManager->getProof(), $options[Request::JSON]['proof']);
333-
$this->assertEquals((string) $this->configurationRepository->getLastUpgrade(), $options[Request::JSON]['fromVersion']);
352+
//$this->assertEquals((string) $this->configurationRepository->getLastUpgrade(), $options[Request::JSON]['fromVersion']);
353+
$this->assertEquals((string) $this->upgradeService->getRegisteredVersion(), $options[Request::JSON]['fromVersion']);
334354

335355
return $this->createResponse([
336356
"error" => 'store-identity/migration-failed',
@@ -340,6 +360,7 @@ public function itShouldNotMigrateOnError()
340360

341361
$this->getHandler()->handle(new MigrateOrCreateIdentityV8Command($this->shopId));
342362

363+
$this->assertEquals($fromVersion, $this->upgradeService->getRegisteredVersion());
343364
$this->assertTrue($this->statusManager->cacheInvalidated());
344365
$this->assertEquals($cloudShopId, $this->statusManager->getCloudShopId());
345366

upgrade/upgrade-8.0.0.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ function upgrade_module_8_0_0($module)
2020
/** @var CommandBus $commandBus */
2121
$commandBus = $module->getService(CommandBus::class);
2222

23-
$commandBus->handle(new MigrateOrCreateIdentitiesV8Command());
23+
$commandBus->handle(new MigrateOrCreateIdentitiesV8Command($module->getRegisteredVersion()));
2424

2525
/* @phpstan-ignore-next-line */
2626
} catch (\Throwable $e) {
2727
Logger::getInstance()->error('error during upgrade : ' . $e->getMessage());
28+
29+
return false;
2830
} catch (\Exception $e) {
2931
Logger::getInstance()->error('error during upgrade : ' . $e->getMessage());
32+
33+
return false;
3034
}
3135

3236
return true;

0 commit comments

Comments
 (0)