Refactor session handling

This commit is contained in:
Alex P 2023-05-22 10:42:33 +03:00
parent 2df970a74a
commit 6aa75e62d4
No known key found for this signature in database
GPG key ID: 54487A734A204D71
2 changed files with 73 additions and 32 deletions

View file

@ -15,17 +15,7 @@ use WooCommerce\PayPalCommerce\Session\Cancellation\CancelView;
return array( return array(
'session.handler' => function ( ContainerInterface $container ) : SessionHandler { 'session.handler' => function ( ContainerInterface $container ) : SessionHandler {
return new SessionHandler();
if ( is_null( WC()->session ) ) {
return new SessionHandler();
}
$result = WC()->session->get( SessionHandler::ID );
if ( is_a( $result, SessionHandler::class ) ) {
return $result;
}
$session_handler = new SessionHandler();
WC()->session->set( SessionHandler::ID, $session_handler );
return $session_handler;
}, },
'session.cancellation.view' => function ( ContainerInterface $container ) : CancelView { 'session.cancellation.view' => function ( ContainerInterface $container ) : CancelView {
return new CancelView( return new CancelView(

View file

@ -16,7 +16,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
*/ */
class SessionHandler { class SessionHandler {
const ID = 'ppcp'; private const SESSION_KEY = 'ppcp';
/** /**
* The Order. * The Order.
@ -33,7 +33,7 @@ class SessionHandler {
private $bn_code = ''; private $bn_code = '';
/** /**
* If PayPal respondes with INSTRUMENT_DECLINED, we only * If PayPal responds with INSTRUMENT_DECLINED, we only
* want to go max. three times through the process of trying again. * want to go max. three times through the process of trying again.
* *
* @var int * @var int
@ -53,6 +53,8 @@ class SessionHandler {
* @return Order|null * @return Order|null
*/ */
public function order() { public function order() {
$this->load_session();
return $this->order; return $this->order;
} }
@ -60,13 +62,13 @@ class SessionHandler {
* Replaces the current order. * Replaces the current order.
* *
* @param Order $order The new order. * @param Order $order The new order.
*
* @return SessionHandler
*/ */
public function replace_order( Order $order ) : SessionHandler { public function replace_order( Order $order ): void {
$this->load_session();
$this->order = $order; $this->order = $order;
$this->store_session(); $this->store_session();
return $this;
} }
/** /**
@ -75,6 +77,8 @@ class SessionHandler {
* @return string * @return string
*/ */
public function bn_code() : string { public function bn_code() : string {
$this->load_session();
return $this->bn_code; return $this->bn_code;
} }
@ -82,13 +86,13 @@ class SessionHandler {
* Replaces the BN Code. * Replaces the BN Code.
* *
* @param string $bn_code The new BN Code. * @param string $bn_code The new BN Code.
*
* @return SessionHandler
*/ */
public function replace_bn_code( string $bn_code ) : SessionHandler { public function replace_bn_code( string $bn_code ) : void {
$this->load_session();
$this->bn_code = $bn_code; $this->bn_code = $bn_code;
$this->store_session(); $this->store_session();
return $this;
} }
/** /**
@ -97,6 +101,8 @@ class SessionHandler {
* @return string|null * @return string|null
*/ */
public function funding_source(): ?string { public function funding_source(): ?string {
$this->load_session();
return $this->funding_source; return $this->funding_source;
} }
@ -104,13 +110,13 @@ class SessionHandler {
* Replaces the funding source of the current checkout. * Replaces the funding source of the current checkout.
* *
* @param string|null $funding_source The funding source. * @param string|null $funding_source The funding source.
*
* @return SessionHandler
*/ */
public function replace_funding_source( ?string $funding_source ): SessionHandler { public function replace_funding_source( ?string $funding_source ): void {
$this->load_session();
$this->funding_source = $funding_source; $this->funding_source = $funding_source;
$this->store_session(); $this->store_session();
return $this;
} }
/** /**
@ -119,18 +125,20 @@ class SessionHandler {
* @return int * @return int
*/ */
public function insufficient_funding_tries() : int { public function insufficient_funding_tries() : int {
$this->load_session();
return $this->insufficient_funding_tries; return $this->insufficient_funding_tries;
} }
/** /**
* Increments the number of tries, the customer has done in this session. * Increments the number of tries, the customer has done in this session.
*
* @return SessionHandler
*/ */
public function increment_insufficient_funding_tries() : SessionHandler { public function increment_insufficient_funding_tries(): void {
$this->load_session();
$this->insufficient_funding_tries++; $this->insufficient_funding_tries++;
$this->store_session(); $this->store_session();
return $this;
} }
/** /**
@ -148,9 +156,52 @@ class SessionHandler {
} }
/** /**
* Stores the session. * Stores the data into the WC session.
*/ */
private function store_session() { private function store_session(): void {
WC()->session->set( self::ID, $this ); WC()->session->set( self::SESSION_KEY, self::make_array( $this ) );
}
/**
* Loads the data from the session.
*/
private function load_session(): void {
if ( isset( WC()->session ) ) {
$data = WC()->session->get( self::SESSION_KEY );
} else {
$data = array();
}
if ( $data instanceof SessionHandler ) {
$data = self::make_array( $data );
} elseif ( ! is_array( $data ) ) {
$data = array();
}
$this->order = $data['order'] ?? null;
if ( ! $this->order instanceof Order ) {
$this->order = null;
}
$this->bn_code = (string) ( $data['bn_code'] ?? '' );
$this->insufficient_funding_tries = (int) ( $data['insufficient_funding_tries'] ?? '' );
$this->funding_source = $data['funding_source'] ?? null;
if ( ! is_string( $this->funding_source ) ) {
$this->funding_source = null;
}
}
/**
* Converts given SessionHandler object into an array.
*
* @param SessionHandler $obj The object to convert.
* @return array
*/
private static function make_array( SessionHandler $obj ): array {
return array(
'order' => $obj->order,
'bn_code' => $obj->bn_code,
'insufficient_funding_tries' => $obj->insufficient_funding_tries,
'funding_source' => $obj->funding_source,
);
} }
} }