Test Failed
Push — release/1.8.12 ( b58a2f...d255b1 )
by Ravinder
375:09 queued 372:17
created

Give_Payment_Stats::get_earnings_cache_key()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 51
Code Lines 26

Duplication

Lines 3
Ratio 5.88 %

Code Coverage

Tests 3
CRAP Score 38.5597

Importance

Changes 0
Metric Value
cc 7
eloc 26
nc 10
nop 4
dl 3
loc 51
ccs 3
cts 22
cp 0.1364
crap 38.5597
rs 6.9743
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Earnings / Sales Stats
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Stats
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Give_Stats Class
19
 *
20
 * This class is for retrieving stats for earnings and sales.
21
 *
22
 * Stats can be retrieved for date ranges and pre-defined periods.
23
 *
24
 * @since 1.0
25
 */
26
class Give_Payment_Stats extends Give_Stats {
27
28
	/**
29
	 * Retrieve sale stats
30
	 *
31
	 * @since  1.0
32
	 * @access public
33
	 *
34
	 * @param  $form_id    int          The donation form to retrieve stats for. If false, gets stats for all forms
35
	 * @param  $start_date string|bool  The starting date for which we'd like to filter our sale stats. If false, we'll use the default start date of `this_month`
36
	 * @param  $end_date   string|bool  The end date for which we'd like to filter our sale stats. If false, we'll use the default end date of `this_month`
37
	 * @param  $status     string|array The sale status(es) to count. Only valid when retrieving global stats
38
	 *
39
	 * @return float|int                Total amount of donations based on the passed arguments.
40
	 */
41
	public function get_sales( $form_id = 0, $start_date = false, $end_date = false, $status = 'publish' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $status 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...
42 2
43
		$this->setup_dates( $start_date, $end_date );
0 ignored issues
show
Documentation introduced by
$start_date is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44 2
45
		// Make sure start date is valid
46
		if ( is_wp_error( $this->start_date ) ) {
47 2
			return $this->start_date;
48
		}
49
50
		// Make sure end date is valid
51
		if ( is_wp_error( $this->end_date ) ) {
52 2
			return $this->end_date;
53
		}
54
55
		$args = array(
56 2
			'status'     => 'publish',
57
			'start_date' => $this->start_date,
58
			'end_date'   => $this->end_date,
59 2
			'fields'     => 'ids',
60
			'number'     => - 1,
61 2
		);
62
63
		if ( ! empty( $form_id ) ) {
64
			$args['give_forms'] = $form_id;
65
		}
66
67 2
		/* @var Give_Payments_Query $payments */
68
		$payments = new Give_Payments_Query( $args );
69
		$payments = $payments->get_payments();
70 2
71
		return count( $payments );
72 2
	}
73
74
75
	/**
76
	 * Retrieve earning stats
77
	 *
78
	 * @since  1.0
79
	 * @access public
80
	 *
81
	 * @param  $form_id     int         The donation form to retrieve stats for. If false, gets stats for all forms.
82
	 * @param  $start_date  string|bool The starting date for which we'd like to filter our donation earnings stats. If false, method will use the default start date of `this_month`.
83
	 * @param  $end_date    string|bool The end date for which we'd like to filter the donations stats. If false, method will use the default end date of `this_month`.
84
	 * @param  $gateway_id  string|bool The gateway to get earnings for such as 'paypal' or 'stripe'.
85
	 *
86
	 * @return float|int                Total amount of donations based on the passed arguments.
87 2
	 */
88
	public function get_earnings( $form_id = 0, $start_date = false, $end_date = false, $gateway_id = false ) {
89
		$this->setup_dates( $start_date, $end_date );
0 ignored issues
show
Documentation introduced by
$start_date is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
91
		// Make sure start date is valid
92
		if ( is_wp_error( $this->start_date ) ) {
93
			return $this->start_date;
94
		}
95
96
		// Make sure end date is valid
97
		if ( is_wp_error( $this->end_date ) ) {
98
			return $this->end_date;
99
		}
100
101
		$args = array(
102
			'status'     => 'publish',
103
			'give_forms' => $form_id,
104
			'start_date' => $this->start_date,
105 2
			'end_date'   => $this->end_date,
106
			'fields'     => 'ids',
107 2
			'number'     => - 1,
108
		);
109 2
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
110
111
		// Filter by Gateway ID meta_key
112 2
		if ( $gateway_id ) {
113
			$args['meta_query'][] = array(
114
				'key'   => '_give_payment_gateway',
115
				'value' => $gateway_id,
116
			);
117 2
		}
118
119
		// Filter by Gateway ID meta_key
120
		if ( $form_id ) {
121 2
			$args['meta_query'][] = array(
122
				'key'   => '_give_payment_form_id',
123 2
				'value' => $form_id,
124
			);
125
		}
126
127 2 View Code Duplication
		if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128 2
			$args['meta_query']['relation'] = 'AND';
129 2
		}
130 2
131 2
		$args = apply_filters( 'give_stats_earnings_args', $args );
132 2
		$key  = Give_Cache::get_key( 'give_stats', $args );
133 2
134
		//Set transient for faster stats
135 2
		$earnings = Give_Cache::get( $key );
136 2
137
		if ( false === $earnings ) {
138 2
139
			$this->timestamp = false;
0 ignored issues
show
Documentation Bug introduced by
The property $timestamp was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
140
			$payments        = new Give_Payments_Query( $args );
141 2
			$payments        = $payments->get_payments();
142
			$earnings        = 0;
143
144
			if ( ! empty( $payments ) ) {
145
				foreach ( $payments as $payment ) {
146 2
					$earnings += give_get_payment_amount( $payment->ID );
147 2
				}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
148 2
149
			}
150 2
151 2
			// Cache the results for one hour
152 2
			Give_Cache::set( $key, give_sanitize_amount_for_db( $earnings ), 60 * 60 );
153 2
		}
154 2
155 2
		//return earnings
156 2
		return round( $earnings, give_currency_decimal_filter() );
157
158 2
	}
159 2
160
	/**
161 2
	 * Retrieve earning stat transient key
162
	 *
163
	 * @since  1.0
164
	 * @access public
165
	 *
166
	 * @param  $form_id     int         The donation form to retrieve stats for. If false, gets stats for all forms
167
	 * @param  $start_date  string|bool The starting date for which we'd like to filter our donation earnings stats. If false, we'll use the default start date of `this_month`
168
	 * @param  $end_date    string|bool The end date for which we'd like to filter our sale stats. If false, we'll use the default end date of `this_month`
169
	 * @param  $gateway_id  string|bool The gateway to get earnings for such as 'paypal' or 'stripe'
170
	 *
171
	 * @return float|int                Total amount of donations based on the passed arguments.
172
	 */
173
	public function get_earnings_cache_key( $form_id = 0, $start_date = false, $end_date = false, $gateway_id = false ) {
174
175
		$this->setup_dates( $start_date, $end_date );
0 ignored issues
show
Documentation introduced by
$start_date is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
176
177
		// Make sure start date is valid
178
		if ( is_wp_error( $this->start_date ) ) {
179
			return $this->start_date;
180
		}
181
182
		// Make sure end date is valid
183
		if ( is_wp_error( $this->end_date ) ) {
184
			return $this->end_date;
185
		}
186
187
		$args = array(
188
			'status'     => 'publish',
189
			'give_forms' => $form_id,
190
			'start_date' => $this->start_date,
191
			'end_date'   => $this->end_date,
192
			'fields'     => 'ids',
193
			'number'     => - 1,
194
		);
195
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
196
197
		// Filter by Gateway ID meta_key
198
		if ( $gateway_id ) {
199
			$args['meta_query'][] = array(
200
				'key'   => '_give_payment_gateway',
201
				'value' => $gateway_id,
202
			);
203
		}
204
205 2
		// Filter by Gateway ID meta_key
206
		if ( $form_id ) {
207
			$args['meta_query'][] = array(
208 2
				'key'   => '_give_payment_form_id',
209
				'value' => $form_id,
210
			);
211
		}
212
213 View Code Duplication
		if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
214
			$args['meta_query']['relation'] = 'AND';
215
		}
216
217
		$args = apply_filters( 'give_stats_earnings_args', $args );
218
		$key  = Give_Cache::get_key( 'give_stats', $args );
219
220
		//return earnings
221
		return $key;
222 1
223
	}
224 1
225
	/**
226 1
	 * Get the best selling forms
227
	 *
228 1
	 * @since  1.0
229
	 * @access public
230 1
	 *
231 1
	 * @param  $number int The number of results to retrieve with the default set to 10.
232
	 *
233 1
	 * @return array       Best selling forms
234
	 */
235
	public function get_best_selling( $number = 10 ) {
236
237
		global $wpdb;
238
239
		$give_forms = $wpdb->get_results( $wpdb->prepare(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
240
			"SELECT post_id as form_id, max(meta_value) as sales
241
				FROM $wpdb->postmeta WHERE meta_key='_give_form_sales' AND meta_value > 0
242
				GROUP BY meta_value+0
243
				DESC LIMIT %d;", $number
244
		) );
245
246
		return $give_forms;
247
	}
248
249
}