apply coding standards to session module

This commit is contained in:
David Remer 2020-09-01 10:12:54 +03:00
parent 4cf58e613f
commit 4519c3f70f
13 changed files with 270 additions and 149 deletions

View file

@ -127,7 +127,7 @@ class ApproveOrderEndpoint implements EndpointInterface {
) )
); );
} }
$this->session_handler->replaceOrder( $order ); $this->session_handler->replace_order( $order );
wp_send_json_success( $order ); wp_send_json_success( $order );
} }
@ -141,7 +141,7 @@ class ApproveOrderEndpoint implements EndpointInterface {
); );
} }
$this->session_handler->replaceOrder( $order ); $this->session_handler->replace_order( $order );
wp_send_json_success( $order ); wp_send_json_success( $order );
return true; return true;
} catch ( \RuntimeException $error ) { } catch ( \RuntimeException $error ) {

View file

@ -145,7 +145,7 @@ class CreateOrderEndpoint implements EndpointInterface {
} }
$bn_code = isset( $data['bn_code'] ) ? (string) $data['bn_code'] : ''; $bn_code = isset( $data['bn_code'] ) ? (string) $data['bn_code'] : '';
if ( $bn_code ) { if ( $bn_code ) {
$this->session_handler->replaceBnCode( $bn_code ); $this->session_handler->replace_bn_code( $bn_code );
$this->api_endpoint->with_bn_code( $bn_code ); $this->api_endpoint->with_bn_code( $bn_code );
} }
$payee_preferred = $this->settings->has( 'payee_preferred' ) $payee_preferred = $this->settings->has( 'payee_preferred' )

View file

@ -1,8 +1,12 @@
<?php <?php
/**
* The extensions of the session module.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1); declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session; namespace Inpsyde\PayPalCommerce\Session;
return [ return array();
];

View file

@ -1,4 +1,10 @@
<?php <?php
/**
* The session module.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1); declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session; namespace Inpsyde\PayPalCommerce\Session;
@ -6,5 +12,5 @@ namespace Inpsyde\PayPalCommerce\Session;
use Dhii\Modular\Module\ModuleInterface; use Dhii\Modular\Module\ModuleInterface;
return function (): ModuleInterface { return function (): ModuleInterface {
return new SessionModule(); return new SessionModule();
}; };

View file

@ -1,4 +1,10 @@
<?php <?php
/**
* The services of the session module.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1); declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session; namespace Inpsyde\PayPalCommerce\Session;
@ -7,27 +13,27 @@ use Dhii\Data\Container\ContainerInterface;
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelController; use Inpsyde\PayPalCommerce\Session\Cancellation\CancelController;
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelView; use Inpsyde\PayPalCommerce\Session\Cancellation\CancelView;
return [ return array(
'session.handler' => function (ContainerInterface $container) : SessionHandler { 'session.handler' => function ( ContainerInterface $container ) : SessionHandler {
if (is_null(WC()->session)) { if ( is_null( WC()->session ) ) {
return new SessionHandler(); return new SessionHandler();
} }
$result = WC()->session->get(SessionHandler::ID); $result = WC()->session->get( SessionHandler::ID );
if (is_a($result, SessionHandler::class)) { if ( is_a( $result, SessionHandler::class ) ) {
return $result; return $result;
} }
$sessionHandler = new SessionHandler(); $session_handler = new SessionHandler();
WC()->session->set(SessionHandler::ID, $sessionHandler); WC()->session->set( SessionHandler::ID, $session_handler );
return $sessionHandler; return $session_handler;
}, },
'session.cancellation.view' => function (ContainerInterface $container) : CancelView { 'session.cancellation.view' => function ( ContainerInterface $container ) : CancelView {
return new CancelView(); return new CancelView();
}, },
'session.cancellation.controller' => function (ContainerInterface $container) : CancelController { 'session.cancellation.controller' => function ( ContainerInterface $container ) : CancelController {
return new CancelController( return new CancelController(
$container->get('session.handler'), $container->get( 'session.handler' ),
$container->get('session.cancellation.view') $container->get( 'session.cancellation.view' )
); );
}, },
]; );

View file

@ -1,46 +1,74 @@
<?php <?php
/**
* Controlls the cancel mechanism to step out of the PayPal order session.
*
* @package Inpsyde\PayPalCommerce\Session\Cancellation
*/
declare(strict_types=1); declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session\Cancellation; namespace Inpsyde\PayPalCommerce\Session\Cancellation;
use Inpsyde\PayPalCommerce\Session\SessionHandler; use Inpsyde\PayPalCommerce\Session\SessionHandler;
class CancelController /**
{ * Class CancelController
*/
class CancelController {
private $sessionHandler; /**
private $view; * The Session handler.
public function __construct( *
SessionHandler $sessionHandler, * @var SessionHandler
CancelView $view */
) { private $session_handler;
$this->view = $view; /**
$this->sessionHandler = $sessionHandler; * The view.
} *
* @var CancelView
*/
private $view;
public function run() /**
{ * CancelController constructor.
$paramName = 'ppcp-cancel'; *
$nonce = 'ppcp-cancel-' . get_current_user_id(); * @param SessionHandler $session_handler The session handler.
if (isset($_GET[$paramName]) && // Input var ok. * @param CancelView $view The view object.
wp_verify_nonce( */
sanitize_text_field(wp_unslash($_GET[$paramName])), // Input var ok. public function __construct(
$nonce SessionHandler $session_handler,
) CancelView $view
) { // Input var ok. ) {
$this->sessionHandler->destroySessionData();
}
if (! $this->sessionHandler->order()) {
return;
}
$url = add_query_arg([$paramName => wp_create_nonce($nonce)], wc_get_checkout_url()); $this->view = $view;
add_action( $this->session_handler = $session_handler;
'woocommerce_review_order_after_submit', }
function () use ($url) {
$this->view->renderSessionCancelation($url); /**
} * Runs the controller.
); */
} public function run() {
$param_name = 'ppcp-cancel';
$nonce = 'ppcp-cancel-' . get_current_user_id();
if ( isset( $_GET[ $param_name ] ) && // Input var ok.
wp_verify_nonce(
sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ), // Input var ok.
$nonce
)
) { // Input var ok.
$this->session_handler->destroy_session_data();
}
if ( ! $this->session_handler->order() ) {
return;
}
$url = add_query_arg( array( $param_name => wp_create_nonce( $nonce ) ), wc_get_checkout_url() );
add_action(
'woocommerce_review_order_after_submit',
function () use ( $url ) {
$this->view->render_session_cancellation( $url );
}
);
}
} }

View file

@ -1,30 +1,42 @@
<?php <?php
/**
* Renders the cancel view for the order on the checkout.
*
* @package Inpsyde\PayPalCommerce\Session\Cancellation
*/
declare(strict_types=1); declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session\Cancellation; namespace Inpsyde\PayPalCommerce\Session\Cancellation;
class CancelView /**
{ * Class CancelView
*/
class CancelView {
public function renderSessionCancelation(string $url) /**
{ * Renders the cancel link.
?> *
<p id="ppcp-cancel" * @param string $url The URL.
class="has-text-align-center ppcp-cancel" */
> public function render_session_cancellation( string $url ) {
<?php ?>
printf( <p id="ppcp-cancel"
// translators: the placeholders are html tags for a link class="has-text-align-center ppcp-cancel"
esc_html__( >
'You are currently paying with PayPal. If you want to cancel <?php
printf(
// translators: the placeholders are html tags for a link.
esc_html__(
'You are currently paying with PayPal. If you want to cancel
this process, please click %1$shere%2$s.', this process, please click %1$shere%2$s.',
'woocommerce-paypal-commerce-gateway' 'woocommerce-paypal-commerce-gateway'
), ),
'<a href="'. esc_url($url) . '">', '<a href="' . esc_url( $url ) . '">',
'</a>' '</a>'
); );
?> ?>
</p> </p>
<?php <?php
} }
} }

View file

@ -1,50 +1,97 @@
<?php <?php
/**
* The Session Handler.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1); declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session; namespace Inpsyde\PayPalCommerce\Session;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order; use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
class SessionHandler /**
{ * Class SessionHandler
const ID = 'ppcp'; */
class SessionHandler {
private $order; const ID = 'ppcp';
private $bnCode = '';
public function order() : ?Order
{
return $this->order;
}
public function replaceOrder(Order $order) : SessionHandler /**
{ * The Order.
$this->order = $order; *
$this->storeSession(); * @var Order|null
return $this; */
} private $order;
public function bnCode() : string /**
{ * The BN Code.
return $this->bnCode; *
} * @var string
*/
private $bn_code = '';
public function replaceBnCode(string $bnCode) : SessionHandler /**
{ * Returns the order.
$this->bnCode = $bnCode; *
$this->storeSession(); * @return Order|null
return $this; */
} public function order() : ?Order {
return $this->order;
}
public function destroySessionData() : SessionHandler /**
{ * Replaces the current order.
$this->order = null; *
$this->bnCode = ''; * @param Order $order The new order.
$this->storeSession(); *
return $this; * @return SessionHandler
} */
public function replace_order( Order $order ) : SessionHandler {
$this->order = $order;
$this->store_session();
return $this;
}
private function storeSession() /**
{ * Returns the BN Code.
WC()->session->set(self::ID, $this); *
} * @return string
*/
public function bn_code() : string {
return $this->bn_code;
}
/**
* Replaces the BN Code.
*
* @param string $bn_code The new BN Code.
*
* @return SessionHandler
*/
public function replace_bn_code( string $bn_code ) : SessionHandler {
$this->bn_code = $bn_code;
$this->store_session();
return $this;
}
/**
* Destroys the session data.
*
* @return SessionHandler
*/
public function destroy_session_data() : SessionHandler {
$this->order = null;
$this->bn_code = '';
$this->store_session();
return $this;
}
/**
* Stores the session.
*/
private function store_session() {
WC()->session->set( self::ID, $this );
}
} }

View file

@ -1,4 +1,10 @@
<?php <?php
/**
* The session module.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1); declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session; namespace Inpsyde\PayPalCommerce\Session;
@ -9,28 +15,40 @@ use Inpsyde\PayPalCommerce\Session\Cancellation\CancelController;
use Interop\Container\ServiceProviderInterface; use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
class SessionModule implements ModuleInterface /**
{ * Class SessionModule
*/
class SessionModule implements ModuleInterface {
public function setup(): ServiceProviderInterface /**
{ * Sets up the module.
return new ServiceProvider( *
require __DIR__.'/../services.php', * @return ServiceProviderInterface
require __DIR__.'/../extensions.php' */
); public function setup(): ServiceProviderInterface {
} return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
public function run(ContainerInterface $container) /**
{ * Run the module.
add_action( *
'woocommerce_init', * @param ContainerInterface $container The container.
function () use ($container) { */
$controller = $container->get('session.cancellation.controller'); public function run( ContainerInterface $container ) {
/** add_action(
* @var CancelController $controller 'woocommerce_init',
*/ function () use ( $container ) {
$controller->run(); $controller = $container->get( 'session.cancellation.controller' );
} /**
); * The Cancel controller.
} *
* @var CancelController $controller
*/
$controller->run();
}
);
}
} }

View file

@ -54,7 +54,7 @@ return array(
* @var SessionHandler $session_handler * @var SessionHandler $session_handler
*/ */
$session_handler = $container->get( 'session.handler' ); $session_handler = $container->get( 'session.handler' );
$bn_code = $session_handler->bnCode(); $bn_code = $session_handler->bn_code();
/** /**
* The settings. * The settings.

View file

@ -210,7 +210,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
); );
} }
$this->session_handler->destroySessionData(); $this->session_handler->destroy_session_data();
} }
wc_add_notice( wc_add_notice(
$this->order_processor->last_error(), $this->order_processor->last_error(),

View file

@ -179,7 +179,7 @@ class OrderProcessor {
$wc_order->update_status( 'processing' ); $wc_order->update_status( 'processing' );
} }
$woocommerce->cart->empty_cart(); $woocommerce->cart->empty_cart();
$this->session_handler->destroySessionData(); $this->session_handler->destroy_session_data();
$this->last_error = ''; $this->last_error = '';
return true; return true;
} }

View file

@ -49,7 +49,7 @@ class OrderProcessorTest extends TestCase
->expects('order') ->expects('order')
->andReturn($currentOrder); ->andReturn($currentOrder);
$sessionHandler $sessionHandler
->expects('destroySessionData'); ->expects('destroy_session_data');
$cartRepository = Mockery::mock(CartRepository::class); $cartRepository = Mockery::mock(CartRepository::class);
$orderEndpoint = Mockery::mock(OrderEndpoint::class); $orderEndpoint = Mockery::mock(OrderEndpoint::class);
$orderEndpoint $orderEndpoint
@ -142,7 +142,7 @@ class OrderProcessorTest extends TestCase
->expects('order') ->expects('order')
->andReturn($currentOrder); ->andReturn($currentOrder);
$sessionHandler $sessionHandler
->expects('destroySessionData'); ->expects('destroy_session_data');
$cartRepository = Mockery::mock(CartRepository::class); $cartRepository = Mockery::mock(CartRepository::class);
$orderEndpoint = Mockery::mock(OrderEndpoint::class); $orderEndpoint = Mockery::mock(OrderEndpoint::class);
$orderEndpoint $orderEndpoint