Refactoring

This commit is contained in:
dinamiko 2021-09-10 12:16:33 +02:00
parent 12f1a2e40b
commit 05dcf5c4f4
3 changed files with 89 additions and 35 deletions

View file

@ -107,13 +107,8 @@ class PaymentTokenRepository {
* @param PaymentToken[] $tokens The tokens.
* @return bool Whether tokens contains card or not.
*/
public function tokens_contains_card( $tokens ): bool {
foreach ( $tokens as $token ) {
if ( isset( $token->source()->card ) ) {
return true;
}
}
return false;
public function tokens_contains_card( array $tokens ): bool {
return $this->token_contains_source( $tokens, 'card' );
}
/**
@ -122,13 +117,8 @@ class PaymentTokenRepository {
* @param PaymentToken[] $tokens The tokens.
* @return bool Whether tokens contains card or not.
*/
public function tokens_contains_paypal( $tokens ): bool {
foreach ( $tokens as $token ) {
if ( isset( $token->source()->paypal ) ) {
return true;
}
}
return false;
public function tokens_contains_paypal( array $tokens ): bool {
return $this->token_contains_source( $tokens, 'paypal' );
}
/**
@ -145,4 +135,21 @@ class PaymentTokenRepository {
update_user_meta( $id, self::USER_META, $token_array );
return $token;
}
/**
* Checks if tokens has the given source.
*
* @param array $tokens Payment tokens.
* @param string $source_type Payment token source type.
* @return bool Whether tokens contains source or not.
*/
private function token_contains_source( array $tokens, string $source_type ): bool {
foreach ( $tokens as $token ) {
if ( isset( $token->source()->card ) && 'card' === $source_type || isset( $token->source()->paypal ) && 'paypal' === $source_type ) {
return true;
}
}
return false;
}
}