♻️ Small adjustments and improvements

This commit is contained in:
Philipp Stracker 2025-02-18 12:34:41 +01:00
parent 2d40768e83
commit c732f53acb
No known key found for this signature in database
2 changed files with 50 additions and 8 deletions

View file

@ -52,6 +52,14 @@ class ConnectionState {
* @param bool $is_sandbox Whether to connect to a sandbox environment.
*/
public function connect( bool $is_sandbox = false ) : void {
if ( ! $this->is_connected ) {
/**
* Action that fires before the connection status changes from
* disconnected to connected.
*/
do_action( 'woocommerce_paypal_payments_merchant_connection_change', true );
}
$this->is_connected = true;
$this->environment->set_environment( $is_sandbox );
}
@ -60,6 +68,14 @@ class ConnectionState {
* Set connection status to "not connected to PayPal" (start onboarding).
*/
public function disconnect() : void {
if ( $this->is_connected ) {
/**
* Action that fires before the connection status changes from
* connected to disconnected.
*/
do_action( 'woocommerce_paypal_payments_merchant_connection_change', false );
}
$this->is_connected = false;
}

View file

@ -29,7 +29,7 @@ class Environment {
*
* @var string
*/
private string $environment_name = '';
private string $environment_name;
/**
* Environment constructor.
@ -37,20 +37,46 @@ class Environment {
* @param bool $is_sandbox Whether this instance represents a sandbox environment.
*/
public function __construct( bool $is_sandbox = false ) {
$this->set_environment( $is_sandbox );
$this->environment_name = $this->prepare_environment_name( $is_sandbox );
}
/**
* Sets the current environment.
* Returns a valid environment name based on the provided argument.
*
* @param bool $is_sandbox Whether this instance represents a sandbox environment.
* @return string The environment name.
*/
private function prepare_environment_name( bool $is_sandbox ) : string {
if ( $is_sandbox ) {
return self::SANDBOX;
}
return self::PRODUCTION;
}
/**
* Updates the current environment.
*
* @param bool $is_sandbox Whether this instance represents a sandbox environment.
*/
public function set_environment( bool $is_sandbox ) : void {
if ( $is_sandbox ) {
$this->environment_name = self::SANDBOX;
} else {
$this->environment_name = self::PRODUCTION;
$new_environment = $this->prepare_environment_name( $is_sandbox );
if ( $new_environment !== $this->environment_name ) {
/**
* Action that fires before the environment status changes.
*
* @param string $new_environment The new environment name.
* @param string $old_environment The previous environment name.
*/
do_action(
'woocommerce_paypal_payments_merchant_environment_change',
$new_environment,
$this->environment_name
);
}
$this->environment_name = $new_environment;
}
/**
@ -65,7 +91,7 @@ class Environment {
/**
* Detect whether the current environment equals $environment
*
* @deprecated Use the is_sandbox() and is_production() methods instead.
* @deprecated 3.0.0 - Use the is_sandbox() and is_production() methods instead.
* These methods provide better encapsulation, are less error-prone,
* and improve code readability by removing the need to pass environment constants.
* @param string $environment The value to check against.