Test Failed
Push — develop ( 889b97...9fde12 )
by Remco
04:47
created

src/CustomerDataStore.php (1 issue)

Severity
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
	/**
25
	 * Get customer data for specified Mollie customer.
26
	 *
27
	 * @param Customer $customer Mollie customer.
28
	 * @return object|null
29
	 */
30
	public function get_customer( Customer $customer ) {
31
		global $wpdb;
32
33
		$id = $customer->get_id();
34
35
		if ( null === $id ) {
36
			return null;
37
		}
38
39
		$query = $wpdb->prepare( "SELECT * FROM $wpdb->pronamic_pay_mollie_customers WHERE mollie_id = %s LIMIT 1;", $id );
40
41
		$data = $wpdb->get_row( $query );
42
43
		return $data;
44
	}
45
46
	/**
47
	 * Insert Mollie customer.
48
	 *
49
	 * @param Customer $customer Customer.
50
	 * @param array    $data   Data.
51
	 * @param array    $format Format.
52
	 */
53
	public function insert_customer( Customer $customer, $data = array(), $format = array() ) {
54
		global $wpdb;
55
56
		$mollie_id = $customer->get_id();
57
58
		if ( empty( $mollie_id ) ) {
59
			throw new \Exception( 'Can not insert Mollie customer with empty ID.' );
60
		}
61
62
		$data['mollie_id']   = $mollie_id;
63
		$format['mollie_id'] = '%s';
64
65
		$data['test_mode']   = ( 'test' === $customer->get_mode() );
66
		$format['test_mode'] = '%d';
67
68
		$data['email']   = $customer->get_email();
69
		$format['email'] = '%s';
70
71
		$result = $wpdb->insert(
72
			$wpdb->pronamic_pay_mollie_customers,
73
			$data,
74
			$format
75
		);
76
77
		if ( false === $result ) {
78
			throw new \Exception(
79
				sprintf(
80
					'Could not insert Mollie customer ID: %s, error: %s.',
81
					$mollie_id,
82
					$wpdb->last_error
83
				)
84
			);
85
		}
86
87
		$id = $wpdb->insert_id;
88
89
		return $id;
90
	}
91
92
	/**
93
	 * Connect Mollie customer to WordPress user.
94
	 *
95
	 * @param Customer $customer Mollie customer.
96
	 * @param \WP_User $user     WordPress user.
97
	 */
98
	public function connect_mollie_customer_to_wp_user( $customer, \WP_User $user ) {
99
		global $wpdb;
100
101
		$query = $wpdb->prepare(
102
			"
103
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
104
				customer_id,
105
				user_id
106
			)
107
			SELECT
108
				mollie_customer.id AS mollie_customer_id,
109
				wp_user.ID AS wp_user_id
110
			FROM
111
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
112
					JOIN
113
				$wpdb->users AS wp_user
114
			WHERE
115
				mollie_customer.mollie_id = %s
116
					AND
117
				wp_user.ID = %d
118
			;
119
			",
120
			$customer->get_id(),
121
			$user->ID
122
		);
123
		echo $query;
124
		exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
125
		$result = $wpdb->query( $query );
126
127
		if ( false === $result ) {
128
			\WP_CLI::error(
129
				sprintf(
130
					'Database error: %s.',
131
					$wpdb->last_error
132
				)
133
			);
134
		}
135
136
		return $result;
137
	}
138
}
139