Test Failed
Push — develop ( 5f6097...889b97 )
by Remco
04:40
created

src/CustomerDataStore.php (2 issues)

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
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
103
				customer_id,
104
				user_id
105
			)
106
			SELECT
107
				mollie_customer.id AS mollie_customer_id,
108
				wp_user.ID AS wp_user_id
109
			FROM
110
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
111
					JOIN
112
				$wpdb->users AS wp_user
113
			WHERE
114
				mollie_customer.mollie_id = %s
115
					AND
116
				wp_user.ID = %d
117
			;
118
			",
119
			$customer->get_id(),
120
			$user->ID
121
		);
122
echo $query;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...
123
		$result = $wpdb->query( $query );
0 ignored issues
show
$result = $wpdb->query($query) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

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