Completed
Push — master ( b6238c...1b9a38 )
by Gerhard
23s queued 10s
created

WC_Payment_Tokens::get_token_classname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
ccs 2
cts 2
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * WooCommerce Payment Tokens
4
 *
5
 * An API for storing and managing tokens for gateways and customers.
6
 *
7
 * @package WooCommerce/Classes
8
 * @version 3.0.0
9
 * @since   2.6.0
10
 */
11
12
defined( 'ABSPATH' ) || exit;
13
14
/**
15
 * Payment tokens class.
16
 */
17
class WC_Payment_Tokens {
18
19
	/**
20
	 * Gets valid tokens from the database based on user defined criteria.
21
	 *
22
	 * @since  2.6.0
23
	 * @param  array $args Query argyments {
24
	 *     Array of query parameters.
25
	 *
26
	 *     @type string $token_id   Token ID.
27
	 *     @type string $user_id    User ID.
28
	 *     @type string $gateway_id Gateway ID.
29
	 *     @type string $type       Token type.
30
	 * }
31
	 * @return WC_Payment_Token[]
32
	 */
33 10
	public static function get_tokens( $args ) {
34 10
		$args = wp_parse_args(
35 10
			$args,
36
			array(
37 10
				'token_id'   => '',
38
				'user_id'    => '',
39
				'gateway_id' => '',
40
				'type'       => '',
41
			)
42
		);
43
44 10
		$data_store    = WC_Data_Store::load( 'payment-token' );
45 10
		$token_results = $data_store->get_tokens( $args );
0 ignored issues
show
Documentation Bug introduced by
The method get_tokens does not exist on object<WC_Data_Store>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
46 10
		$tokens        = array();
47
48 10
		if ( ! empty( $token_results ) ) {
49 7
			foreach ( $token_results as $token_result ) {
50 7
				$_token = self::get( $token_result->token_id, $token_result );
51 7
				if ( ! empty( $_token ) ) {
52 7
					$tokens[ $token_result->token_id ] = $_token;
53
				}
54
			}
55
		}
56
57 10
		return $tokens;
58
	}
59
60
	/**
61
	 * Returns an array of payment token objects associated with the passed customer ID.
62
	 *
63
	 * @since 2.6.0
64
	 * @param  int    $customer_id Customer ID.
65
	 * @param  string $gateway_id  Optional Gateway ID for getting tokens for a specific gateway.
66
	 * @return WC_Payment_Token[]  Array of token objects.
67
	 */
68 9
	public static function get_customer_tokens( $customer_id, $gateway_id = '' ) {
69 9
		if ( $customer_id < 1 ) {
70
			return array();
71
		}
72
73 9
		$tokens = self::get_tokens(
74
			array(
75 9
				'user_id'    => $customer_id,
76 9
				'gateway_id' => $gateway_id,
77
			)
78
		);
79
80 9
		return apply_filters( 'woocommerce_get_customer_payment_tokens', $tokens, $customer_id, $gateway_id );
81
	}
82
83
	/**
84
	 * Returns a customers default token or NULL if there is no default token.
85
	 *
86
	 * @since  2.6.0
87
	 * @param  int $customer_id Customer ID.
88
	 * @return WC_Payment_Token|null
89
	 */
90 4
	public static function get_customer_default_token( $customer_id ) {
91 4
		if ( $customer_id < 1 ) {
92
			return null;
93
		}
94
95 4
		$data_store = WC_Data_Store::load( 'payment-token' );
96 4
		$token      = $data_store->get_users_default_token( $customer_id );
0 ignored issues
show
Documentation Bug introduced by
The method get_users_default_token does not exist on object<WC_Data_Store>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
97
98 4
		if ( $token ) {
99 3
			return self::get( $token->token_id, $token );
100
		} else {
101 3
			return null;
102
		}
103
	}
104
105
	/**
106
	 * Returns an array of payment token objects associated with the passed order ID.
107
	 *
108
	 * @since 2.6.0
109
	 * @param int $order_id       Order ID.
110
	 * @return WC_Payment_Token[] Array of token objects.
111
	 */
112 1
	public static function get_order_tokens( $order_id ) {
113 1
		$order = wc_get_order( $order_id );
114
115 1
		if ( ! $order ) {
116
			return array();
117
		}
118
119 1
		$token_ids = $order->get_payment_tokens();
120
121 1
		if ( empty( $token_ids ) ) {
122 1
			return array();
123
		}
124
125 1
		$tokens = self::get_tokens(
126
			array(
127 1
				'token_id' => $token_ids,
128
			)
129
		);
130
131 1
		return apply_filters( 'woocommerce_get_order_payment_tokens', $tokens, $order_id );
132
	}
133
134
	/**
135
	 * Get a token object by ID.
136
	 *
137
	 * @since 2.6.0
138
	 *
139
	 * @param int    $token_id Token ID.
140
	 * @param object $token_result Token result.
141
	 * @return null|WC_Payment_Token Returns a valid payment token or null if no token can be found.
142
	 */
143 10
	public static function get( $token_id, $token_result = null ) {
144 10
		$data_store = WC_Data_Store::load( 'payment-token' );
145
146 10
		if ( is_null( $token_result ) ) {
147 3
			$token_result = $data_store->get_token_by_id( $token_id );
0 ignored issues
show
Documentation Bug introduced by
The method get_token_by_id does not exist on object<WC_Data_Store>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
148
			// Still empty? Token doesn't exist? Don't continue.
149 3
			if ( empty( $token_result ) ) {
150 2
				return null;
151
			}
152
		}
153
154 8
		$token_class = self::get_token_classname( $token_result->type );
155
156 8
		if ( class_exists( $token_class ) ) {
157 8
			$meta        = $data_store->get_metadata( $token_id );
0 ignored issues
show
Documentation Bug introduced by
The method get_metadata does not exist on object<WC_Data_Store>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
158 8
			$passed_meta = array();
159 8
			if ( ! empty( $meta ) ) {
160 8
				foreach ( $meta as $meta_key => $meta_value ) {
161 8
					$passed_meta[ $meta_key ] = $meta_value[0];
162
				}
163
			}
164 8
			return new $token_class( $token_id, (array) $token_result, $passed_meta );
165
		}
166
167
		return null;
168
	}
169
170
	/**
171
	 * Remove a payment token from the database by ID.
172
	 *
173
	 * @since 2.6.0
174
	 * @param int $token_id Token ID.
175
	 */
176 1
	public static function delete( $token_id ) {
177 1
		$type = self::get_token_type_by_id( $token_id );
178 1
		if ( ! empty( $type ) ) {
179 1
			$class = self::get_token_classname( $type );
180 1
			$token = new $class( $token_id );
181 1
			$token->delete();
182
		}
183
	}
184
185
	/**
186
	 * Loops through all of a users payment tokens and sets is_default to false for all but a specific token.
187
	 *
188
	 * @since 2.6.0
189
	 * @param int $user_id  User to set a default for.
190
	 * @param int $token_id The ID of the token that should be default.
191
	 */
192 4
	public static function set_users_default( $user_id, $token_id ) {
193 4
		$data_store   = WC_Data_Store::load( 'payment-token' );
194 4
		$users_tokens = self::get_customer_tokens( $user_id );
195 4
		foreach ( $users_tokens as $token ) {
196 4
			if ( $token_id === $token->get_id() ) {
197 4
				$data_store->set_default_status( $token->get_id(), true );
0 ignored issues
show
Documentation Bug introduced by
The method set_default_status does not exist on object<WC_Data_Store>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
198 4
				do_action( 'woocommerce_payment_token_set_default', $token_id, $token );
199
			} else {
200 2
				$data_store->set_default_status( $token->get_id(), false );
0 ignored issues
show
Documentation Bug introduced by
The method set_default_status does not exist on object<WC_Data_Store>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
201
			}
202
		}
203
	}
204
205
	/**
206
	 * Returns what type (credit card, echeck, etc) of token a token is by ID.
207
	 *
208
	 * @since  2.6.0
209
	 * @param  int $token_id Token ID.
210
	 * @return string        Type.
211
	 */
212 2
	public static function get_token_type_by_id( $token_id ) {
213 2
		$data_store = WC_Data_Store::load( 'payment-token' );
214 2
		return $data_store->get_token_type_by_id( $token_id );
0 ignored issues
show
Documentation Bug introduced by
The method get_token_type_by_id does not exist on object<WC_Data_Store>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
215
	}
216
217
	/**
218
	 * Get classname based on token type.
219
	 *
220
	 * @since 3.8.0
221
	 * @param string $type Token type.
222
	 * @return string
223
	 */
224 9
	protected static function get_token_classname( $type ) {
225
		/**
226
		 * Filter payment token class per type.
227
		 *
228
		 * @since 3.8.0
229
		 * @param string $class Payment token class.
230
		 * @param string $type Token type.
231
		 */
232 9
		return apply_filters( 'woocommerce_payment_token_class', 'WC_Payment_Token_' . $type, $type );
233
	}
234
}
235