Total Complexity | 44 |
Total Lines | 332 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Client { |
||
20 | /** |
||
21 | * Mollie API endpoint URL |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | const API_URL = 'https://api.mollie.nl/v1/'; |
||
26 | |||
27 | /** |
||
28 | * Mollie API Key ID |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $api_key; |
||
33 | |||
34 | /** |
||
35 | * Mode |
||
36 | * |
||
37 | * @since 1.1.9 |
||
38 | * @var string |
||
39 | */ |
||
40 | private $mode; |
||
41 | |||
42 | /** |
||
43 | * Error |
||
44 | * |
||
45 | * @var WP_Error |
||
46 | */ |
||
47 | private $error; |
||
48 | |||
49 | /** |
||
50 | * Constructs and initializes an Mollie client object |
||
51 | * |
||
52 | * @param string $api_key |
||
53 | */ |
||
54 | public function __construct( $api_key ) { |
||
55 | $this->api_key = $api_key; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Set mode |
||
60 | * |
||
61 | * @since 1.1.9 |
||
62 | * @param string $mode |
||
63 | */ |
||
64 | public function set_mode( $mode ) { |
||
65 | $this->mode = $mode; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Error |
||
70 | * |
||
71 | * @return WP_Error |
||
72 | */ |
||
73 | public function get_error() { |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Send request with the specified action and parameters |
||
79 | * |
||
80 | * @param string $end_point |
||
81 | * @param string $method |
||
82 | * @param array $data |
||
83 | * @param int $expected_response_code |
||
84 | * |
||
85 | * @return bool|object |
||
86 | */ |
||
87 | private function send_request( $end_point, $method = 'GET', array $data = array(), $expected_response_code = 200 ) { |
||
88 | // Request |
||
89 | $url = self::API_URL . $end_point; |
||
90 | |||
91 | $response = wp_remote_request( $url, array( |
||
92 | 'method' => $method, |
||
93 | 'headers' => array( |
||
94 | 'Authorization' => 'Bearer ' . $this->api_key, |
||
95 | ), |
||
96 | 'body' => $data, |
||
97 | ) ); |
||
98 | |||
99 | // Response code |
||
100 | $response_code = wp_remote_retrieve_response_code( $response ); |
||
101 | |||
102 | if ( $expected_response_code != $response_code ) { // WPCS: loose comparison ok. |
||
103 | $this->error = new WP_Error( 'mollie_error', 'Unexpected response code.' ); |
||
104 | } |
||
105 | |||
106 | // Body |
||
107 | $body = wp_remote_retrieve_body( $response ); |
||
108 | |||
109 | $data = json_decode( $body ); |
||
110 | |||
111 | if ( ! is_object( $data ) ) { |
||
112 | $this->error = new WP_Error( 'mollie_error', 'Could not parse response.' ); |
||
113 | |||
114 | return false; |
||
115 | } |
||
116 | |||
117 | // Mollie error |
||
118 | if ( isset( $data->error, $data->error->message ) ) { |
||
119 | $this->error = new \WP_Error( 'mollie_error', $data->error->message, $data->error ); |
||
120 | |||
121 | return false; |
||
122 | } |
||
123 | |||
124 | return $data; |
||
125 | } |
||
126 | |||
127 | public function create_payment( PaymentRequest $request ) { |
||
128 | return $this->send_request( 'payments/', 'POST', $request->get_array(), 201 ); |
||
129 | } |
||
130 | |||
131 | public function get_payments() { |
||
132 | return $this->send_request( 'payments/', 'GET' ); |
||
133 | } |
||
134 | |||
135 | public function get_payment( $payment_id ) { |
||
136 | if ( empty( $payment_id ) ) { |
||
137 | return false; |
||
138 | } |
||
139 | |||
140 | return $this->send_request( 'payments/' . $payment_id, 'GET' ); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get issuers |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | public function get_issuers() { |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get payment methods |
||
173 | * |
||
174 | * @param string $recurring_type Recurring type. |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function get_payment_methods( $recurring_type = '' ) { |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Create customer. |
||
207 | * |
||
208 | * @since 1.1.6 |
||
209 | * |
||
210 | * @param string $email |
||
211 | * @param string $name |
||
212 | * |
||
213 | * @return array|bool |
||
214 | */ |
||
215 | public function create_customer( $email, $name ) { |
||
216 | if ( empty( $email ) || empty( $name ) ) { |
||
217 | return false; |
||
218 | } |
||
219 | |||
220 | $response = $this->send_request( 'customers/', 'POST', array( |
||
221 | 'name' => $name, |
||
222 | 'email' => $email, |
||
223 | ), 201 ); |
||
224 | |||
225 | if ( false === $response ) { |
||
226 | return false; |
||
227 | } |
||
228 | |||
229 | if ( ! isset( $response->id ) ) { |
||
230 | return false; |
||
231 | } |
||
232 | |||
233 | return $response->id; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Get customer. |
||
238 | * |
||
239 | * @param $customer_id |
||
240 | * |
||
241 | * @since unreleased |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | public function get_customer( $customer_id ) { |
||
246 | if ( empty( $customer_id ) ) { |
||
247 | return false; |
||
248 | } |
||
249 | |||
250 | $response = $this->send_request( 'customers/' . $customer_id, 'GET', array(), 200 ); |
||
251 | |||
252 | if ( false === $response ) { |
||
253 | return false; |
||
254 | } |
||
255 | |||
256 | if ( is_wp_error( $this->error ) ) { |
||
257 | return false; |
||
258 | } |
||
259 | |||
260 | return $response; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Get mandates for customer. |
||
265 | * |
||
266 | * @param $customer_id |
||
267 | * |
||
268 | * @return array |
||
269 | */ |
||
270 | public function get_mandates( $customer_id ) { |
||
271 | if ( '' === $customer_id ) { |
||
272 | return false; |
||
273 | } |
||
274 | |||
275 | return $this->send_request( 'customers/' . $customer_id . '/mandates?count=250', 'GET' ); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Is there a valid mandate for customer? |
||
280 | * |
||
281 | * @param $customer_id |
||
282 | * |
||
283 | * @return boolean |
||
284 | */ |
||
285 | public function has_valid_mandate( $customer_id, $payment_method = null ) { |
||
286 | $mandates = $this->get_mandates( $customer_id ); |
||
287 | |||
288 | if ( ! $mandates ) { |
||
289 | return false; |
||
290 | } |
||
291 | |||
292 | $mollie_method = Methods::transform( $payment_method ); |
||
293 | |||
294 | foreach ( $mandates->data as $mandate ) { |
||
295 | if ( $mollie_method !== $mandate->method ) { |
||
296 | continue; |
||
297 | } |
||
298 | |||
299 | if ( 'valid' === $mandate->status ) { |
||
300 | return true; |
||
301 | } |
||
302 | } |
||
303 | |||
304 | return false; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Get formatted date and time of first valid mandate. |
||
309 | * |
||
310 | * @param string $customer_id Mollie customer ID. |
||
311 | * @param string $payment_method Payment method. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function get_first_valid_mandate_datetime( $customer_id, $payment_method = null ) { |
||
351 | } |
||
352 | } |
||
353 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths