Test Failed
Push — develop ( 9201b4...1ea451 )
by Remco
04:30
created

CustomerDataStore::get_or_insert_customer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
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_or_insert_customer( Customer $customer ) {
25
		$data = $this->get_customer( $customer );
26
27
		if ( null !== $data ) {
28
			return $data->id;
29
		}
30
31
		return $this->insert_customer( $customer );
32
	}
33
34
	/**
35
	 * Get customer data for specified Mollie customer.
36
	 *
37
	 * @param Customer $customer Mollie customer.
38
	 * @return object|null
39
	 */
40
	public function get_customer( Customer $customer ) {
41
		global $wpdb;
42
43
		$id = $customer->get_id();
44
45
		if ( null === $id ) {
46
			return null;
47
		}
48
49
		$query = $wpdb->prepare( "SELECT * FROM $wpdb->pronamic_pay_mollie_customers WHERE mollie_id = %s LIMIT 1;", $id );
50
51
		$data = $wpdb->get_row( $query );
52
53
		return $data;
54
	}
55
56
	/**
57
	 * Insert Mollie customer.
58
	 *
59
	 * @param Customer $customer Customer.
60
	 * @param array    $data   Data.
61
	 * @param array    $format Format.
62
	 */
63
	public function insert_customer( Customer $customer, $data = array(), $format = array() ) {
64
		global $wpdb;
65
66
		$mollie_id = $customer->get_id();
67
68
		if ( empty( $mollie_id ) ) {
69
			throw new \Exception( 'Can not insert Mollie customer with empty ID.' );
70
		}
71
72
		$data['mollie_id']   = $mollie_id;
73
		$format['mollie_id'] = '%s';
74
75
		$data['test_mode']   = ( 'test' === $customer->get_mode() );
76
		$format['test_mode'] = '%d';
77
78
		$data['email']   = $customer->get_email();
79
		$format['email'] = '%s';
80
81
		$result = $wpdb->insert(
82
			$wpdb->pronamic_pay_mollie_customers,
83
			$data,
84
			$format
85
		);
86
87
		if ( false === $result ) {
88
			throw new \Exception(
89
				sprintf(
90
					'Could not insert Mollie customer ID: %s, error: %s.',
91
					$mollie_id,
92
					$wpdb->last_error
93
				)
94
			);
95
		}
96
97
		$id = $wpdb->insert_id;
98
99
		return $id;
100
	}
101
102
	/**
103
	 * Connect Mollie customer to WordPress user.
104
	 *
105
	 * @param Customer $customer Mollie customer.
106
	 * @param \WP_User $user     WordPress user.
107
	 */
108
	public function connect_mollie_customer_to_wp_user( $customer, \WP_User $user ) {
109
		global $wpdb;
110
111
		$query = $wpdb->prepare(
112
			"
113
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
114
				customer_id,
115
				user_id
116
			)
117
			SELECT
118
				mollie_customer.id AS mollie_customer_id,
119
				wp_user.ID AS wp_user_id
120
			FROM
121
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
122
					JOIN
123
				$wpdb->users AS wp_user
124
			WHERE
125
				mollie_customer.mollie_id = %s
126
					AND
127
				wp_user.ID = %d
128
			;
129
			",
130
			$customer->get_id(),
131
			$user->ID
132
		);
133
134
		$result = $wpdb->query( $query );
135
136
		if ( false === $result ) {
137
			\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...
138
				sprintf(
139
					'Database error: %s.',
140
					$wpdb->last_error
141
				)
142
			);
143
		}
144
145
		return $result;
146
	}
147
}
148