Test Failed
Push — develop ( f2e903...5f6097 )
by Remco
04:49
created

CustomerDataStore::get_customer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 10
1
<?php
2
/**
3
 * Mollie customer data store.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
/**
14
 * Title: Mollie customer data store
15
 * Description:
16
 * Copyright: 2005-2020 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 3.0.0
21
 * @since   3.0.0
22
 */
23
class CustomerDataStore {
24
	public function get_customer( $customer ) {
25
		global $wpdb;
26
27
		$id = $customer->get_id();
28
29
		if ( null === $id ) {
30
			return null;
31
		}
32
33
		$query = $wpdb->prepare( "SELECT * FROM $wpdb->pronamic_pay_mollie_customers WHERE mollie_id = %s LIMIT 1;", $id );
34
35
		$data = $wpdb->get_row( $query );
36
37
		return $data;
38
	}
39
40
	public function insert_customer( $customer, $data = array(), $format = array()  ) {
41
		global $wpdb;
42
43
		$mollie_id = $customer->get_id();
44
45
		if ( empty( $mollie_id ) ) {
46
			throw new \Exception( 'Can not insert Mollie customer with empty ID.' );
47
		}
48
49
		$data['mollie_id'] = $mollie_id;
50
		$format['mollie_id'] = '%s';
51
52
		$data['test_mode'] = ( 'test' === $customer->get_mode() );
53
		$format['test_mode'] = '%d';
54
55
		$data['email']     = $customer->get_email();
56
		$format['email']     = '%s';
57
58
		$result = $wpdb->insert(
59
			$wpdb->pronamic_pay_mollie_customers,
60
			$data,
61
			$format
62
		);
63
64
		if ( false === $result ) {
65
			throw new \Exception(
66
				sprintf(
67
					'Could not insert Mollie customer ID: %s, error: %s.',
68
					$mollie_id,
69
					$wpdb->last_error
70
				)
71
			);
72
		}
73
74
		$id = $wpdb->insert_id;
75
76
		return $id;
77
	}
78
79
	/**
80
	 * Connect Mollie customer to WordPress user.
81
	 *
82
	 * @param Customer $customer Mollie customer.
83
	 * @param \WP_User $user     WordPress user.
84
	 */
85
	public function connect_mollie_customer_to_wp_user( $customer, \WP_User $user ) {
86
		global $wpdb;
87
88
		$query = $wpdb->prepare( "
89
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
90
				customer_id,
91
				user_id
92
			)
93
			SELECT
94
				mollie_customer.id AS mollie_customer_id,
95
				wp_user.ID AS wp_user_id
96
			FROM
97
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
98
					JOIN
99
				$wpdb->users AS wp_user
100
			WHERE
101
				mollie_customer.mollie_id = %s
102
					AND
103
				wp_user.ID = %d
104
			;
105
			",
106
			$customer->get_id(),
107
			$user->ID
108
		);
109
110
		$result = $wpdb->query( $query );
111
112
		if ( false === $result ) {
113
			\WP_CLI::error(
0 ignored issues
show
Bug introduced by
The type WP_CLI was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
114
				sprintf(
115
					'Database error: %s.',
116
					$wpdb->last_error
117
				)
118
			);
119
		}
120
121
		return $result;
122
	}
123
}
124