CustomerDataStore   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Test Coverage

Coverage 49.37%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 87
c 2
b 0
f 0
dl 0
loc 227
ccs 39
cts 79
cp 0.4937
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A connect_mollie_customer_to_wp_user() 0 38 2
A update_customer() 0 38 3
A save_customer() 0 14 3
A insert_customer() 0 37 3
A get_or_insert_customer() 0 12 3
A get_customer() 0 27 2
1
<?php
2
/**
3
 * Mollie customer data store.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 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-2022 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 2.1.0
21
 * @since   2.1.0
22
 */
23
class CustomerDataStore {
24
	/**
25
	 * Get or insert customer.
26
	 *
27
	 * @param Customer                  $customer Customer.
28
	 * @param array<string, int|string> $data     Data.
29
	 * @param array<string, string>     $format   Format.
30
	 * @return int
31
	 * @throws \Exception Throws exception if Mollie customer ID could not be retrieved from existing customer.
32
	 */
33 2
	public function get_or_insert_customer( Customer $customer, $data = array(), $format = array() ) {
34 2
		$customer_data = $this->get_customer( $customer );
35
36 2
		if ( null !== $customer_data ) {
37
			if ( ! \property_exists( $customer_data, 'id' ) ) {
38
				throw new \Exception( 'Unable to get Mollie customer ID for existing customer.' );
39
			}
40
41
			return $customer_data->id;
42
		}
43
44 2
		return $this->insert_customer( $customer, $data, $format );
45
	}
46
47
	/**
48
	 * Get customer data for specified Mollie customer.
49
	 *
50
	 * @param Customer $customer Mollie customer.
51
	 * @return object|null
52
	 */
53 2
	public function get_customer( Customer $customer ) {
54 2
		global $wpdb;
55
56 2
		$id = $customer->get_id();
57
58 2
		if ( null === $id ) {
59
			return null;
60
		}
61
62 2
		$data = $wpdb->get_row(
63 2
			$wpdb->prepare(
64 2
				"
65
				SELECT
66
					*
67
				FROM
68 2
					$wpdb->pronamic_pay_mollie_customers
69
				WHERE
70
					mollie_id = %s
71
				LIMIT
72
					1
73
				;
74
				",
75
				$id
76
			)
77
		);
78
79 2
		return $data;
80
	}
81
82
	/**
83
	 * Insert Mollie customer.
84
	 *
85
	 * @param Customer                  $customer Customer.
86
	 * @param array<string, int|string> $data     Data.
87
	 * @param array<string, string>     $format   Format.
88
	 * @return int
89
	 * @throws \Exception Throws exception on error.
90
	 */
91 2
	public function insert_customer( Customer $customer, $data = array(), $format = array() ) {
92 2
		global $wpdb;
93
94 2
		$mollie_id = $customer->get_id();
95
96 2
		if ( empty( $mollie_id ) ) {
97
			throw new \Exception( 'Can not insert Mollie customer with empty ID.' );
98
		}
99
100 2
		$data['mollie_id']   = $mollie_id;
101 2
		$format['mollie_id'] = '%s';
102
103 2
		$data['test_mode']   = ( 'test' === $customer->get_mode() );
104 2
		$format['test_mode'] = '%d';
105
106 2
		$data['email']   = $customer->get_email();
107 2
		$format['email'] = '%s';
108
109 2
		$result = $wpdb->insert(
110 2
			$wpdb->pronamic_pay_mollie_customers,
111
			$data,
112
			$format
113
		);
114
115 2
		if ( false === $result ) {
116
			throw new \Exception(
117
				sprintf(
118
					'Could not insert Mollie customer ID: %s, error: %s.',
119
					$mollie_id,
120
					$wpdb->last_error
121
				)
122
			);
123
		}
124
125 2
		$id = $wpdb->insert_id;
126
127 2
		return $id;
128
	}
129
130
	/**
131
	 * Update Mollie customer.
132
	 *
133
	 * @param Customer                  $customer Customer.
134
	 * @param array<string, int|string> $data     Data.
135
	 * @param array<string, string>     $format   Format.
136
	 * @return int The number of rows updated.
137
	 * @throws \Exception Throws exception on error.
138
	 */
139
	public function update_customer( Customer $customer, $data = array(), $format = array() ) {
140
		global $wpdb;
141
142
		$mollie_id = $customer->get_id();
143
144
		if ( empty( $mollie_id ) ) {
145
			throw new \Exception( 'Can not update Mollie customer with empty ID.' );
146
		}
147
148
		$data['test_mode']   = ( 'test' === $customer->get_mode() );
149
		$format['test_mode'] = '%d';
150
151
		$data['email']   = $customer->get_email();
152
		$format['email'] = '%s';
153
154
		$result = $wpdb->update(
155
			$wpdb->pronamic_pay_mollie_customers,
156
			$data,
157
			array(
158
				'mollie_id' => $mollie_id,
159
			),
160
			$format,
161
			array(
162
				'mollie_id' => '%s',
163
			)
164
		);
165
166
		if ( false === $result ) {
167
			throw new \Exception(
168
				sprintf(
169
					'Could not update Mollie customer ID: %s, error: %s.',
170
					$mollie_id,
171
					$wpdb->last_error
172
				)
173
			);
174
		}
175
176
		return $result;
177
	}
178
179
	/**
180
	 * Save Mollie customer.
181
	 *
182
	 * @param Customer                  $customer Customer.
183
	 * @param array<string, int|string> $data     Data.
184
	 * @param array<string, string>     $format   Format.
185
	 * @return int
186
	 * @throws \Exception Throws exception if unable to update existing customer.
187
	 */
188
	public function save_customer( Customer $customer, $data = array(), $format = array() ) {
189
		$customer_data = $this->get_customer( $customer );
190
191
		if ( null !== $customer_data ) {
192
			if ( ! \property_exists( $customer_data, 'id' ) ) {
193
				throw new \Exception( 'Can not update Mollie customer without ID.' );
194
			}
195
196
			$this->update_customer( $customer, $data, $format );
197
198
			return $customer_data->id;
199
		}
200
201
		return $this->insert_customer( $customer, $data, $format );
202
	}
203
204
	/**
205
	 * Connect Mollie customer to WordPress user.
206
	 *
207
	 * @param Customer $customer Mollie customer.
208
	 * @param \WP_User $user     WordPress user.
209
	 * @return int Number of rows affected.
210
	 * @throws \Exception Throws exception on error.
211
	 */
212 2
	public function connect_mollie_customer_to_wp_user( $customer, \WP_User $user ) {
213 2
		global $wpdb;
214
215 2
		$result = $wpdb->query(
216 2
			$wpdb->prepare(
217
				"
218 2
				INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
219
					customer_id,
220
					user_id
221
				)
222
				SELECT
223
					mollie_customer.id AS mollie_customer_id,
224
					wp_user.ID AS wp_user_id
225
				FROM
226 2
					$wpdb->pronamic_pay_mollie_customers AS mollie_customer
227
						JOIN
228 2
					$wpdb->users AS wp_user
229
				WHERE
230
					mollie_customer.mollie_id = %s
231
						AND
232
					wp_user.ID = %d
233
				;
234
				",
235 2
				$customer->get_id(),
236 2
				$user->ID
237
			)
238
		);
239
240 2
		if ( false === $result ) {
241
			throw new \Exception(
242
				sprintf(
243
					'Database error: %s.',
244
					$wpdb->last_error
245
				)
246
			);
247
		}
248
249 2
		return $result;
250
	}
251
}
252