Skip to content

Commit b75d010

Browse files
Merge branch 'SPE-72/opc-migration' into SPE-108/feat/ui-errors-managment
2 parents 0520d6e + faf1907 commit b75d010

5 files changed

Lines changed: 49 additions & 207 deletions

File tree

ps_onepagecheckout.php

Lines changed: 6 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414

1515
use PrestaShop\Module\PsOnePageCheckout\Analytics\Analytics;
1616
use PrestaShop\Module\PsOnePageCheckout\Checkout\OnePageCheckoutAvailability;
17-
use PrestaShop\Module\PsOnePageCheckout\Checkout\OnePageCheckoutProcessBuilder;
17+
use PrestaShop\Module\PsOnePageCheckout\Checkout\OnePageCheckoutProcessProvider;
1818
use PrestaShop\Module\PsOnePageCheckout\Form\BackOfficeConfigurationForm;
1919
use PrestaShop\Module\PsOnePageCheckout\Translation\ModuleTranslation;
20-
use Symfony\Contracts\Translation\TranslatorInterface;
20+
use PrestaShop\PrestaShop\Adapter\Order\Checkout\CheckoutProcessProviderInterface;
2121

2222
class Ps_Onepagecheckout extends Module
2323
{
2424
public const CONFIG_ONE_PAGE_CHECKOUT_ENABLED = 'PS_ONE_PAGE_CHECKOUT_ENABLED';
25-
public const CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE = 'PS_CHECKOUT_PROCESS_PROVIDER_MODULE';
2625
private ?BackOfficeConfigurationForm $backOfficeConfigurationForm = null;
2726

2827
public function __construct()
@@ -77,7 +76,6 @@ public function install()
7776
{
7877
return $this->installInParent()
7978
&& $this->installOnePageCheckoutConfiguration()
80-
&& $this->initializeCheckoutProcessProviderConfiguration()
8179
&& $this->registerHook('actionCheckoutBuildProcess')
8280
&& $this->registerHook('actionFrontControllerSetMedia')
8381
&& $this->registerHook('actionFrontControllerSetVariables')
@@ -86,9 +84,7 @@ public function install()
8684

8785
public function enable($force_all = false)
8886
{
89-
$result = $this->enableInParent((bool) $force_all)
90-
&& $this->initializeCheckoutProcessProviderConfiguration();
91-
87+
$result = $this->enableInParent((bool) $force_all);
9288
if ($result) {
9389
Analytics::trackEvent(Analytics::EVENT_MODULE_ENABLED, [], (string) $this->version);
9490
}
@@ -106,7 +102,6 @@ public function enable($force_all = false)
106102
public function disable($force_all = false)
107103
{
108104
$result = $this->disableOnePageCheckoutConfigurationForCurrentContext()
109-
&& $this->clearCheckoutProcessProviderConfigurationForCurrentContext()
110105
&& $this->disableInParent((bool) $force_all);
111106

112107
if ($result) {
@@ -119,7 +114,6 @@ public function disable($force_all = false)
119114
public function uninstall()
120115
{
121116
$result = $this->uninstallOnePageCheckoutConfiguration()
122-
&& $this->clearCheckoutProcessProviderConfigurationForCurrentModule()
123117
&& $this->uninstallInParent();
124118

125119
if ($result) {
@@ -152,9 +146,9 @@ public function getBackOfficeConfigurationContent(): string
152146
return $this->getBackOfficeConfigurationForm()->renderBackOfficeConfiguration();
153147
}
154148

155-
public function hookActionCheckoutBuildProcess(array $params): ?CheckoutProcess
149+
public function hookActionCheckoutBuildProcess(array $params = []): CheckoutProcessProviderInterface
156150
{
157-
return $this->buildCheckoutProcessFromHookParams($params);
151+
return new OnePageCheckoutProcessProvider($this->context, $this);
158152
}
159153

160154
public function hookActionFrontControllerSetMedia(): void
@@ -339,13 +333,7 @@ public function hookActionFrontControllerSetVariables(array $params): void
339333

340334
public function isOnePageCheckoutEnabled(): bool
341335
{
342-
if (!$this->isCurrentShopCheckoutProvider()) {
343-
return false;
344-
}
345-
346-
$availability = new OnePageCheckoutAvailability(self::CONFIG_ONE_PAGE_CHECKOUT_ENABLED);
347-
348-
return $availability->isEnabled();
336+
return (new OnePageCheckoutAvailability(self::CONFIG_ONE_PAGE_CHECKOUT_ENABLED))->isEnabled();
349337
}
350338

351339
protected function registerOpcJavascriptAssets(): void
@@ -405,46 +393,6 @@ protected function addOpcJavascriptDefinition(array $javascriptDefinition): void
405393
Media::addJsDef($javascriptDefinition);
406394
}
407395

408-
protected function createCheckoutProcessBuilder(): OnePageCheckoutProcessBuilder
409-
{
410-
return new OnePageCheckoutProcessBuilder($this->context, $this);
411-
}
412-
413-
protected function buildCheckoutProcessFromHookParams(array $params): ?CheckoutProcess
414-
{
415-
if (!$this->isOnePageCheckoutEnabled()) {
416-
return null;
417-
}
418-
419-
if (!isset($params['checkoutSession']) || !$params['checkoutSession'] instanceof CheckoutSession) {
420-
return null;
421-
}
422-
423-
if (!isset($params['translator']) || !$params['translator'] instanceof TranslatorInterface) {
424-
return null;
425-
}
426-
427-
try {
428-
$checkoutProcessBuilder = $this->createCheckoutProcessBuilder();
429-
$moduleCheckoutProcess = $checkoutProcessBuilder->build($params['checkoutSession'], $params['translator']);
430-
431-
return $moduleCheckoutProcess instanceof CheckoutProcess
432-
? $moduleCheckoutProcess
433-
: null;
434-
} catch (Throwable $exception) {
435-
PrestaShopLogger::addLog(
436-
sprintf('ps_onepagecheckout: unable to build module checkout process (%s)', $exception->getMessage()),
437-
3,
438-
null,
439-
'Module',
440-
(int) $this->id,
441-
true
442-
);
443-
}
444-
445-
return null;
446-
}
447-
448396
protected function installInParent(): bool
449397
{
450398
return parent::install();
@@ -487,42 +435,6 @@ protected function disableOnePageCheckoutConfigurationForCurrentContext(): bool
487435
return Configuration::updateValue(self::CONFIG_ONE_PAGE_CHECKOUT_ENABLED, 0, false);
488436
}
489437

490-
protected function initializeCheckoutProcessProviderConfiguration(): bool
491-
{
492-
return Configuration::updateValue(self::CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE, $this->name, false);
493-
}
494-
495-
protected function isCurrentShopCheckoutProvider(): bool
496-
{
497-
return trim((string) Configuration::get(self::CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE)) === $this->name;
498-
}
499-
500-
protected function clearCheckoutProcessProviderConfigurationForCurrentContext(): bool
501-
{
502-
$configuredProvider = trim((string) Configuration::get(self::CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE));
503-
if ($configuredProvider !== $this->name) {
504-
return true;
505-
}
506-
507-
return Configuration::updateValue(self::CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE, '', false);
508-
}
509-
510-
protected function clearCheckoutProcessProviderConfigurationForCurrentModule(): bool
511-
{
512-
return Db::getInstance()->update(
513-
'configuration',
514-
[
515-
'value' => '',
516-
'date_upd' => date('Y-m-d H:i:s'),
517-
],
518-
sprintf(
519-
'`name` = "%s" AND `value` = "%s"',
520-
pSQL(self::CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE),
521-
pSQL($this->name)
522-
)
523-
);
524-
}
525-
526438
protected function uninstallOnePageCheckoutConfiguration(): bool
527439
{
528440
return Configuration::deleteByName(self::CONFIG_ONE_PAGE_CHECKOUT_ENABLED);

src/Checkout/OnePageCheckoutProcessBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public function __construct(
4141
* @param \CheckoutSession $checkoutSession
4242
* @param TranslatorInterface $translator
4343
*
44-
* @return \CheckoutProcess|null
44+
* @return \CheckoutProcess
4545
*/
46-
public function build(\CheckoutSession $checkoutSession, TranslatorInterface $translator): ?\CheckoutProcess
46+
public function build(\CheckoutSession $checkoutSession, TranslatorInterface $translator): \CheckoutProcess
4747
{
4848
$checkoutProcess = new OnePageCheckoutProcess(
4949
$this->context,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace PrestaShop\Module\PsOnePageCheckout\Checkout;
4+
5+
use PrestaShop\PrestaShop\Adapter\Order\Checkout\CheckoutProcessProviderInterface;
6+
use PrestaShopBundle\Translation\TranslatorComponent;
7+
8+
class OnePageCheckoutProcessProvider implements CheckoutProcessProviderInterface
9+
{
10+
private readonly OnePageCheckoutAvailability $availability;
11+
private readonly OnePageCheckoutProcessBuilder $checkoutProcessBuilder;
12+
13+
public function __construct(
14+
\Context $context,
15+
\Ps_Onepagecheckout $module,
16+
) {
17+
$this->availability = new OnePageCheckoutAvailability(\Ps_Onepagecheckout::CONFIG_ONE_PAGE_CHECKOUT_ENABLED);
18+
$this->checkoutProcessBuilder = new OnePageCheckoutProcessBuilder($context, $module);
19+
}
20+
21+
public function isEnabled(): bool
22+
{
23+
return $this->availability->isEnabled();
24+
}
25+
26+
public function buildCheckoutProcess(
27+
\CheckoutSession $session,
28+
TranslatorComponent $translator,
29+
): \CheckoutProcess {
30+
return $this->checkoutProcessBuilder->build($session, $translator);
31+
}
32+
}

tests/php/Integration/Module/ModuleLifecycleIntegrationTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class ModuleLifecycleIntegrationTest extends TestCase
2525

2626
private const MODULE_NAME = 'ps_onepagecheckout';
2727
private const CONFIG_ONE_PAGE_CHECKOUT_ENABLED = 'PS_ONE_PAGE_CHECKOUT_ENABLED';
28-
private const CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE = 'PS_CHECKOUT_PROCESS_PROVIDER_MODULE';
2928

3029
protected function setUp(): void
3130
{
@@ -49,30 +48,27 @@ protected function setUp(): void
4948
\Module::updateTranslationsAfterInstall(false);
5049
}
5150

52-
public function testInstallDisableEnableAndUninstallKeepCheckoutProviderStateConsistent(): void
51+
public function testInstallDisableEnableAndUninstallKeepOpcFlagStateConsistent(): void
5352
{
5453
$module = $this->buildModule();
5554

5655
self::assertTrue($module->install());
5756
self::assertTrue(\Module::isInstalled(self::MODULE_NAME));
5857
self::assertTrue(\Module::isEnabled(self::MODULE_NAME));
5958
self::assertSame('0', (string) \Configuration::get(self::CONFIG_ONE_PAGE_CHECKOUT_ENABLED));
60-
self::assertSame(self::MODULE_NAME, trim((string) \Configuration::get(self::CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE)));
6159

6260
$module = $this->buildModule();
6361
self::assertTrue($module->disable());
6462
\Configuration::loadConfiguration();
6563

6664
self::assertFalse(\Module::isEnabled(self::MODULE_NAME));
6765
self::assertSame('0', (string) \Configuration::get(self::CONFIG_ONE_PAGE_CHECKOUT_ENABLED));
68-
self::assertSame('', trim((string) \Configuration::get(self::CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE)));
6966

7067
$module = $this->buildModule();
7168
self::assertTrue($module->enable());
7269
\Configuration::loadConfiguration();
7370

7471
self::assertTrue(\Module::isEnabled(self::MODULE_NAME));
75-
self::assertSame(self::MODULE_NAME, trim((string) \Configuration::get(self::CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE)));
7672
self::assertSame('0', (string) \Configuration::get(self::CONFIG_ONE_PAGE_CHECKOUT_ENABLED));
7773

7874
$module = $this->buildModule();
@@ -81,7 +77,6 @@ public function testInstallDisableEnableAndUninstallKeepCheckoutProviderStateCon
8177

8278
self::assertFalse(\Module::isInstalled(self::MODULE_NAME));
8379
self::assertSame('0', (string) \Configuration::get(self::CONFIG_ONE_PAGE_CHECKOUT_ENABLED));
84-
self::assertSame('', trim((string) \Configuration::get(self::CONFIG_CHECKOUT_PROCESS_PROVIDER_MODULE)));
8580
}
8681

8782
private function buildModule(): \Ps_Onepagecheckout

0 commit comments

Comments
 (0)