1414
1515use PrestaShop \Module \PsOnePageCheckout \Analytics \Analytics ;
1616use PrestaShop \Module \PsOnePageCheckout \Checkout \OnePageCheckoutAvailability ;
17- use PrestaShop \Module \PsOnePageCheckout \Checkout \OnePageCheckoutProcessBuilder ;
17+ use PrestaShop \Module \PsOnePageCheckout \Checkout \OnePageCheckoutProcessProvider ;
1818use PrestaShop \Module \PsOnePageCheckout \Form \BackOfficeConfigurationForm ;
1919use PrestaShop \Module \PsOnePageCheckout \Translation \ModuleTranslation ;
20- use Symfony \ Contracts \ Translation \ TranslatorInterface ;
20+ use PrestaShop \ PrestaShop \ Adapter \ Order \ Checkout \ CheckoutProcessProviderInterface ;
2121
2222class 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 );
0 commit comments