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

ProfileDataStore::get_or_insert_profile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * Mollie profile 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 profile 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 ProfileDataStore {
24
	public function get_or_insert_profile( Profile $profile, $data = array(), $format = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

24
	public function get_or_insert_profile( Profile $profile, /** @scrutinizer ignore-unused */ $data = array(), $format = array() ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

24
	public function get_or_insert_profile( Profile $profile, $data = array(), /** @scrutinizer ignore-unused */ $format = array() ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
		$data = $this->get_profile( $profile );
26
27
		if ( null !== $data ) {
28
			return $data->id;
29
		}
30
31
		return $this->save_profile( $profile, $data = array(), $format = array() );
32
	}
33
34
	/**
35
	 * Get profile data for specified Mollie profile.
36
	 *
37
	 * @param Profile $profile Profile.
38
	 * @return object|null
39
	 */
40
	public function get_profile( Profile $profile ) {
41
		global $wpdb;
42
43
		$id = $profile->get_id();
44
45
		if ( null === $id ) {
46
			return null;
47
		}
48
49
		$data = $wpdb->get_row(
50
			$wpdb->prepare(
51
				"
52
				SELECT
53
					*
54
				FROM
55
					$wpdb->pronamic_pay_mollie_profiles
56
				WHERE
57
					mollie_id = %s
58
				LIMIT
59
					1
60
				;
61
				",
62
				$id
63
			)
64
		);
65
66
		return $data;
67
	}
68
69
	/**
70
	 * Save Mollie profile.
71
	 *
72
	 * @param Profile $profile Profile.
73
	 * @param array   $data   Data.
74
	 * @param array   $format Format.
75
	 * @return int
76
	 */
77
	public function save_profile( Profile $profile, $data = array(), $format = array() ) {
78
		global $wpdb;
79
80
		$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->pronamic_pay_mollie_profiles WHERE mollie_id = %s", $profile->get_id() ) );
81
82
		$data['email']   = $profile->get_email();
83
		$format['email'] = '%s';
84
85
		$data['name']   = $profile->get_name();
86
		$format['name'] = '%s';
87
88
		if ( null === $id ) {
89
			$data['mollie_id']   = $profile->get_id();
90
			$foramt['mollie_id'] = '%s';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$foramt was never initialized. Although not strictly required by PHP, it is generally a good practice to add $foramt = array(); before regardless.
Loading history...
91
92
			$result = $wpdb->insert(
93
				$wpdb->pronamic_pay_mollie_profiles,
94
				$data,
95
				$format
96
			);
97
98
			if ( false === $result ) {
99
				\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...
100
					sprintf(
101
						'Database error: %s.',
102
						$wpdb->last_error
103
					)
104
				);
105
			}
106
107
			$id = $wpdb->insert_id;
108
		} else {
109
			$result = $wpdb->update(
110
				$wpdb->pronamic_pay_mollie_profiles,
111
				$data,
112
				array(
113
					'id' => $id,
114
				),
115
				$format,
116
				array(
117
					'id' => '%d',
118
				)
119
			);
120
121
			if ( false === $result ) {
122
				\WP_CLI::error(
123
					sprintf(
124
						'Database error: %s.',
125
						$wpdb->last_error
126
					)
127
				);
128
			}
129
		}
130
131
		return $id;
132
	}
133
}
134