Fix phpcs errors

This commit is contained in:
dinamiko 2021-03-25 16:57:42 +01:00
parent f5a2fca5fb
commit a71c8b134a
9 changed files with 207 additions and 206 deletions

View file

@ -37,6 +37,8 @@ class PaymentToken {
private $type; private $type;
/** /**
* The payment source.
*
* @var \stdClass * @var \stdClass
*/ */
private $source; private $source;
@ -46,6 +48,7 @@ class PaymentToken {
* *
* @param string $id The Id. * @param string $id The Id.
* @param string $type The type. * @param string $type The type.
* @param \stdClass $source The source.
* @throws RuntimeException When the type is not valid. * @throws RuntimeException When the type is not valid.
*/ */
public function __construct( string $id, string $type = self::TYPE_PAYMENT_METHOD_TOKEN, \stdClass $source ) { public function __construct( string $id, string $type = self::TYPE_PAYMENT_METHOD_TOKEN, \stdClass $source ) {
@ -82,8 +85,7 @@ class PaymentToken {
* *
* @return \stdClass * @return \stdClass
*/ */
public function source(): \stdClass public function source(): \stdClass {
{
return $this->source; return $this->source;
} }

View file

@ -69,7 +69,7 @@ return array(
$subscription_helper = $container->get( 'subscription.helper' ); $subscription_helper = $container->get( 'subscription.helper' );
$messages_apply = $container->get( 'button.helper.messages-apply' ); $messages_apply = $container->get( 'button.helper.messages-apply' );
$environment = $container->get( 'onboarding.environment' ); $environment = $container->get( 'onboarding.environment' );
$payment_token_repository = $container->get('subscription.repository.payment-token'); $payment_token_repository = $container->get( 'subscription.repository.payment-token' );
return new SmartButton( return new SmartButton(
$container->get( 'button.url' ), $container->get( 'button.url' ),
$container->get( 'session.handler' ), $container->get( 'session.handler' ),

View file

@ -117,6 +117,8 @@ class SmartButton implements SmartButtonInterface {
private $environment; private $environment;
/** /**
* The payment token repository.
*
* @var PaymentTokenRepository * @var PaymentTokenRepository
*/ */
private $payment_token_repository; private $payment_token_repository;
@ -136,6 +138,7 @@ class SmartButton implements SmartButtonInterface {
* @param SubscriptionHelper $subscription_helper The subscription helper. * @param SubscriptionHelper $subscription_helper The subscription helper.
* @param MessagesApply $messages_apply The Messages apply helper. * @param MessagesApply $messages_apply The Messages apply helper.
* @param Environment $environment The environment object. * @param Environment $environment The environment object.
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
*/ */
public function __construct( public function __construct(
string $module_url, string $module_url,
@ -211,7 +214,7 @@ class SmartButton implements SmartButtonInterface {
$payment_token_repository = $this->payment_token_repository; $payment_token_repository = $this->payment_token_repository;
add_filter( add_filter(
'woocommerce_credit_card_form_fields', 'woocommerce_credit_card_form_fields',
function ( $default_fields, $id ) use( $payment_token_repository ) { function ( $default_fields, $id ) use ( $payment_token_repository ) {
if ( $this->can_save_credit_card() ) { if ( $this->can_save_credit_card() ) {
$default_fields['card-vault'] = sprintf( $default_fields['card-vault'] = sprintf(
'<p class="form-row form-row-wide"><label for="vault"><input class="ppcp-credit-card-vault" type="checkbox" id="ppcp-credit-card-vault" name="vault">%s</label></p>', '<p class="form-row form-row-wide"><label for="vault"><input class="ppcp-credit-card-vault" type="checkbox" id="ppcp-credit-card-vault" name="vault">%s</label></p>',
@ -219,14 +222,14 @@ class SmartButton implements SmartButtonInterface {
); );
$tokens = $payment_token_repository->all_for_user_id( 1 ); $tokens = $payment_token_repository->all_for_user_id( 1 );
if($tokens && $this->tokens_contains_card($tokens) ) { if ( $tokens && $this->tokens_contains_card( $tokens ) ) {
$output = sprintf( $output = sprintf(
'<select id="saved-credit-card" name="saved_credit_card"><option value="">%s</option>', '<select id="saved-credit-card" name="saved_credit_card"><option value="">%s</option>',
esc_html__( 'Choose a saved payment', 'woocommerce-paypal-payments' ) esc_html__( 'Choose a saved payment', 'woocommerce-paypal-payments' )
); );
foreach ($tokens as $token) { foreach ( $tokens as $token ) {
if(isset($token->source()->card)) { if ( isset( $token->source()->card ) ) {
$output .= sprintf( $output .= sprintf(
'<option value="%1$s">%2$s ...%3$s</option>', '<option value="%1$s">%2$s ...%3$s</option>',
$token->id(), $token->id(),
@ -1025,12 +1028,12 @@ class SmartButton implements SmartButtonInterface {
/** /**
* Check if tokens has card source. * Check if tokens has card source.
* *
* @param PaymentToken[] $tokens * @param PaymentToken[] $tokens The tokens.
* @return bool * @return bool Wether tokens contains card or not.
*/ */
protected function tokens_contains_card($tokens) { protected function tokens_contains_card( $tokens ) {
foreach ($tokens as $token) { foreach ( $tokens as $token ) {
if(isset($token->source()->card)) { if ( isset( $token->source()->card ) ) {
return true; return true;
} }
} }

View file

@ -73,22 +73,24 @@ class PaymentTokenRepository {
} }
/** /**
* @param int $id * Return all tokens for a user.
* @return array *
* @param int $id The user id.
* @return PaymentToken[]
*/ */
public function all_for_user_id( int $id ) { public function all_for_user_id( int $id ) {
$tokens = get_user_meta( $id, self::USER_META, true ); $tokens = get_user_meta( $id, self::USER_META, true );
if($tokens) { if ( $tokens ) {
return (array) $tokens; return (array) $tokens;
} }
$tokens_array = []; $tokens_array = array();
try { try {
$tokens = $this->endpoint->for_user( $id ); $tokens = $this->endpoint->for_user( $id );
update_user_meta( $id, self::USER_META, $tokens ); update_user_meta( $id, self::USER_META, $tokens );
return $tokens; return $tokens;
} catch (RuntimeException $exception) { } catch ( RuntimeException $exception ) {
return []; return array();
} }
} }

View file

@ -58,24 +58,6 @@ class SubscriptionModule implements ModuleInterface {
10, 10,
2 2
); );
/*
add_action('woocommerce_init', function () use($container) {
$api = $container->get('api.endpoint.payment-token' );
try {
$tokens = $api->for_user(1);
for($i = 0; $i < count($tokens); $i++) {
$api->delete_token($tokens[$i]);
}
$a = 1;
} catch (RuntimeException $exception) {
$a = 1;
}
});
*/
} }
/** /**

View file

@ -66,10 +66,10 @@ return array(
$session_handler = $container->get( 'session.handler' ); $session_handler = $container->get( 'session.handler' );
$refund_processor = $container->get( 'wcgateway.processor.refunds' ); $refund_processor = $container->get( 'wcgateway.processor.refunds' );
$state = $container->get( 'onboarding.state' ); $state = $container->get( 'onboarding.state' );
$payment_token_repository = $container->get('subscription.repository.payment-token'); $payment_token_repository = $container->get( 'subscription.repository.payment-token' );
$purchase_unit_factory = $container->get('api.factory.purchase-unit'); $purchase_unit_factory = $container->get( 'api.factory.purchase-unit' );
$payer_factory = $container->get('api.factory.payer'); $payer_factory = $container->get( 'api.factory.payer' );
$order_endpoint = $container->get('api.endpoint.order'); $order_endpoint = $container->get( 'api.endpoint.order' );
return new CreditCardGateway( return new CreditCardGateway(
$settings_renderer, $settings_renderer,
$order_processor, $order_processor,

View file

@ -46,21 +46,29 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
private $refund_processor; private $refund_processor;
/** /**
* The payment token repository.
*
* @var PaymentTokenRepository * @var PaymentTokenRepository
*/ */
private $payment_token_repository; private $payment_token_repository;
/** /**
* The purchase unit factory.
*
* @var PurchaseUnitFactory * @var PurchaseUnitFactory
*/ */
private $purchase_unit_factory; private $purchase_unit_factory;
/** /**
* The payer factory.
*
* @var PayerFactory * @var PayerFactory
*/ */
private $payer_factory; private $payer_factory;
/** /**
* The order endpoint.
*
* @var OrderEndpoint * @var OrderEndpoint
*/ */
private $order_endpoint; private $order_endpoint;
@ -77,6 +85,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
* @param SessionHandler $session_handler The Session Handler. * @param SessionHandler $session_handler The Session Handler.
* @param RefundProcessor $refund_processor The refund processor. * @param RefundProcessor $refund_processor The refund processor.
* @param State $state The state. * @param State $state The state.
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
* @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory.
* @param PayerFactory $payer_factory The payer factory.
* @param OrderEndpoint $order_endpoint The order endpoint.
*/ */
public function __construct( public function __construct(
SettingsRenderer $settings_renderer, SettingsRenderer $settings_renderer,

View file

@ -34,22 +34,22 @@ trait ProcessPaymentTrait {
/** /**
* If customer has chosed a saved credit card payment. * If customer has chosed a saved credit card payment.
*/ */
$saved_credit_card = filter_input(INPUT_POST, 'saved_credit_card', FILTER_SANITIZE_STRING); $saved_credit_card = filter_input( INPUT_POST, 'saved_credit_card', FILTER_SANITIZE_STRING );
if($saved_credit_card) { if ( $saved_credit_card ) {
$user_id = (int) $wc_order->get_customer_id(); $user_id = (int) $wc_order->get_customer_id();
$customer = new \WC_Customer( $user_id ); $customer = new \WC_Customer( $user_id );
$tokens = $this->payment_token_repository->all_for_user_id( (int) $customer->get_id() ); $tokens = $this->payment_token_repository->all_for_user_id( (int) $customer->get_id() );
$selected_token = null; $selected_token = null;
foreach ($tokens as $token) { foreach ( $tokens as $token ) {
if($token->id() === $saved_credit_card) { if ( $token->id() === $saved_credit_card ) {
$selected_token = $token; $selected_token = $token;
break; break;
} }
} }
if(!$selected_token) { if ( ! $selected_token ) {
return null; return null;
} }
@ -74,7 +74,7 @@ trait ProcessPaymentTrait {
'redirect' => $this->get_return_url( $wc_order ), 'redirect' => $this->get_return_url( $wc_order ),
); );
} }
} catch (RuntimeException $error) { } catch ( RuntimeException $error ) {
$this->session_handler->destroy_session_data(); $this->session_handler->destroy_session_data();
wc_add_notice( $error->getMessage(), 'error' ); wc_add_notice( $error->getMessage(), 'error' );
return null; return null;