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

CustomerDataStore::insert_customer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 22
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 37
rs 9.568
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
Best Practice introduced by
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 );
0 ignored issues
show
Unused Code introduced by
$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...
126
127
		if ( false === $result ) {
128
			\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...
129
				sprintf(
130
					'Database error: %s.',
131
					$wpdb->last_error
132
				)
133
			);
134
		}
135
136
		return $result;
137
	}
138
}
139