diff --git a/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php b/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php index 0b4f2eda3..7b650bc79 100644 --- a/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php +++ b/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php @@ -89,6 +89,11 @@ class SaveConfig { } } + /** + * Saves the config into the old settings. + * + * @param array $config The configurator config. + */ private function save_config( array $config ): void { $this->settings->set( 'pay_later_enable_styling_per_messaging_location', true ); @@ -109,6 +114,12 @@ class SaveConfig { $this->settings->persist(); } + /** + * Saves the config for a location into the old settings. + * + * @param array $config The configurator config for a location. + * @param string $location The location name in the old settings. + */ private function save_config_for_location( array $config, string $location ): void { $this->set_value_if_present( $config, 'layout', "pay_later_{$location}_message_layout" ); @@ -121,12 +132,24 @@ class SaveConfig { $this->set_value_if_present( $config, 'text-size', "pay_later_{$location}_message_text_size" ); } + /** + * Sets the value in the settings if it is available in the config. + * + * @param array $config The configurator config. + * @param string $key The key in the config. + * @param string $settings_key The key in the settings. + */ private function set_value_if_present( array $config, string $key, string $settings_key ): void { if ( isset( $config[ $key ] ) ) { $this->settings->set( $settings_key, $config[ $key ] ); } } + /** + * Converts the configurator placement into location in the old settings. + * + * @param string $placement The configurator placement. + */ private function configurator_placement_to_location( string $placement ): string { switch ( $placement ) { case 'cart': diff --git a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php index 19209fb67..cb272984d 100644 --- a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php +++ b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php @@ -15,6 +15,11 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; * Class ConfigFactory. */ class ConfigFactory { + /** + * Returns the configurator config for the given old settings. + * + * @param Settings $settings The settings. + */ public function from_settings( Settings $settings ): array { return array( $this->location_to_configurator_placement( 'cart' ) => $this->for_location( $settings, 'cart' ), @@ -25,11 +30,17 @@ class ConfigFactory { ); } + /** + * Returns the configurator config for a location. + * + * @param Settings $settings The settings. + * @param string $location The location name in the old settings. + */ private function for_location( Settings $settings, string $location ): array { $selected_locations = $settings->has( 'pay_later_messaging_locations' ) ? $settings->get( 'pay_later_messaging_locations' ) : array(); $placement = $this->location_to_configurator_placement( $location ); - if ( in_array( $placement, array( 'category', 'homepage' ) ) ) { + if ( in_array( $placement, array( 'category', 'homepage' ), true ) ) { $config = array( 'layout' => 'flex', 'color' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_color", 'black', array( 'black', 'blue', 'white', 'white-no-border' ) ), @@ -48,13 +59,18 @@ class ConfigFactory { return array_merge( array( - 'status' => in_array( $location, $selected_locations ) ? 'enabled' : 'disabled', + 'status' => in_array( $location, $selected_locations, true ) ? 'enabled' : 'disabled', 'placement' => $placement, ), $config ); } + /** + * Converts the location name from the old settings into the configurator placement. + * + * @param string $location The location name in the old settings. + */ private function location_to_configurator_placement( string $location ): string { switch ( $location ) { case 'cart': @@ -70,6 +86,15 @@ class ConfigFactory { } } + /** + * Returns the settings value or default, if does not exist or not allowed value. + * + * @param Settings $settings The settings. + * @param string $key The key. + * @param mixed $default The default value. + * @param array|null $allowed_values The list of allowed values, or null if all values are allowed. + * @return mixed + */ private function get_or_default( Settings $settings, string $key, $default, ?array $allowed_values = null ) { if ( $settings->has( $key ) ) { $value = $settings->get( $key );