Test Failed
Push — issues/2912 ( 47fdf9...1e5343 )
by Ravinder
09:11
created

Give_Seq_Donation_Number::get_donation_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
// Exit if access directly.
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
class Give_Seq_Donation_Number {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
8
	/**
9
	 * Instance.
10
	 *
11
	 * @since  2.1.0
12
	 * @access private
13
	 * @var
14
	 */
15
	static private $instance;
16
17
	/**
18
	 * Singleton pattern.
19
	 *
20
	 * @since  2.1.0
21
	 * @access private
22
	 */
23
	private function __construct() {
24
	}
25
26
27
	/**
28
	 * Get instance.
29
	 *
30
	 * @since  2.1.0
31
	 * @access static
32
	 * @return Give_Seq_Donation_Number
33
	 */
34 View Code Duplication
	public static function get_instance() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
		if ( null === static::$instance ) {
36
			self::$instance = new static();
37
38
			self::$instance->init();
39
		}
40
41
		return self::$instance;
42
	}
43
44
	/**
45
	 * Initialize the plugin, bailing if any required conditions are not met,
46
	 * including minimum WooCommerce version
47
	 *
48
	 * @since 2.1.0
49
	 */
50
	public function init() {
51
		if ( give_is_setting_enabled( give_get_option( 'sequential-donation_status', 'disabled' ) ) ) {
52
			add_action( 'wp_insert_post', array( $this, '__save_donation_title' ), 10, 3 );
53
		}
54
	}
55
56
	/**
57
	 * Set serialize donation number as donation title.
58
	 *
59
	 * @since  2.1.0
60
	 * @access public
61
	 *
62
	 * @param int     $donation_id
63
	 * @param WP_Post $donation_post_data
64
	 * @param bool    $existing_donation_updated
65
	 *
66
	 * @return void
67
	 */
68
	public function __save_donation_title( $donation_id, $donation_post_data, $existing_donation_updated ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Seq_Donation_Number::__save_donation_title" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
69
		// Bailout
70
		if ( $existing_donation_updated ) {
71
			return;
72
		}
73
74
		$serial_number = $this->set_donation_number( $donation_id );
75
76
		$serial_code = $this->__set_number_padding( $serial_number );
77
78
		// Add prefix.
79
		if ( $prefix = give_get_option( 'sequential-donation_number_prefix', '' ) ) {
80
			$serial_code = $prefix . $serial_code;
81
		}
82
83
		// Add suffix.
84
		if ( $suffix = give_get_option( 'sequential-donation_number_suffix', '' ) ) {
85
			$serial_code = $serial_code . $suffix;
86
		}
87
88
		$serial_code = give_time_do_tags( $serial_code );
89
90
		try {
91
			/* @var WP_Error $wp_error */
92
			$wp_error = wp_update_post(
93
				array(
94
					'ID'         => $donation_id,
95
					'post_title' => $serial_code
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
96
				)
97
			);
98
99
			if ( is_wp_error( $wp_error ) ) {
100
				throw new Exception( $wp_error->get_error_message() );
101
			}
102
		} catch ( Exception $e ) {
103
			error_log( "Give caught exception: {$e->getMessage()}" );
104
		}
105
	}
106
107
	/**
108
	 * Set donation number
109
	 *
110
	 * @since  2.1.0
111
	 * @access public
112
	 *
113
	 * @param int $donation_id
114
	 *
115
	 * @return int
116
	 */
117
	public function set_donation_number( $donation_id ) {
118
		return Give()->sequential_donation_db->insert( array(
119
			'payment_id' => $donation_id
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
120
		) );
121
	}
122
123
	/**
124
	 * Set number padding in serial code.
125
	 *
126
	 * @since
127
	 * @access private
128
	 *
129
	 * @param $serial_code
130
	 *
131
	 * @return string
132
	 */
133
	private function __set_number_padding( $serial_code ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Seq_Donation_Number::__set_number_padding" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
134
		if ( $number_padding = give_get_option( 'sequential-donation_number_padding', 0 ) ) {
135
			$current_str_length = strlen( $serial_code );
136
			$serial_code        = $number_padding > $current_str_length ?
137
				substr( '0000000000', 0, $number_padding - $current_str_length ) . $serial_code :
138
				$serial_code;
139
		}
140
141
		return $serial_code;
142
	}
143
144
	/**
145
	 * Get donation number serial code
146
	 *
147
	 * @since  2.1.0
148
	 * @access public
149
	 *
150
	 * @param int|Give_Payment $donation
151
	 * @param array            $args
152
	 *
153
	 * @return string
154
	 */
155
	public function get_serial_code( $donation, $args = array() ) {
156
		$donation = $donation instanceof Give_Payment ? $donation : new Give_Payment( $donation );
157
158
		// Bailout.
159
		if ( empty( $donation->ID ) ) {
160
			return '';
161
		}
162
163
		// Set default params.
164
		$args = wp_parse_args(
165
			$args,
166
			array(
167
				'with_hash' => false,
168
				'default'   => true
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
169
			)
170
		);
171
172
		$serial_code = $args['default'] ? $donation->ID : '';
173
174
		if ( $donation_number = $this->get_serial_number( $donation->ID ) ) {
175
			$serial_code = get_the_title( $donation->ID );
176
		}
177
178
		$serial_code = $args['with_hash'] ? "#{$serial_code}" : $serial_code;
179
180
		/**
181
		 * Filter the donation serial code
182
		 *
183
		 * @since 2.1.0
184
		 */
185
		return apply_filters( 'give_get_donation_serial_code', $serial_code, $donation, $args, $donation_number );
186
	}
187
188
	/**
189
	 * Get serial number
190
	 *
191
	 * @since  2.1.0
192
	 * @access public
193
	 *
194
	 * @param int $donation_id
195
	 *
196
	 * @return string
197
	 */
198
	public function get_serial_number( $donation_id ) {
199
		return Give()->sequential_donation_db->get_column_by( 'id', 'payment_id', $donation_id );
200
	}
201
202
203
	/**
204
	 * Get donation id with donation number or serial code
205
	 *
206
	 * @since  2.1.0
207
	 * @access public
208
	 *
209
	 * @param string $donation_number_or_serial_code\
0 ignored issues
show
Documentation introduced by
There is no parameter named $donation_number_or_serial_code\. Did you maybe mean $donation_number_or_serial_code?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
210
	 */
211
	public function get_donation_id( $donation_number_or_serial_code ) {}
0 ignored issues
show
Unused Code introduced by
The parameter $donation_number_or_serial_code is not used and could be removed.

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

Loading history...
212
}
213
214
// @todo: add post_title support in Give_Payment
215
// @todo: resolve caching issue: donation listing is not updating when updating donation
216