Completed
Pull Request — master (#1832)
by Devin
04:50
created

Give_Payment_Stats::get_earnings()   C

Complexity

Conditions 10
Paths 26

Size

Total Lines 71
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10.3183

Importance

Changes 0
Metric Value
cc 10
eloc 36
nc 26
nop 4
dl 0
loc 71
ccs 29
cts 34
cp 0.8529
crap 10.3183
rs 5.9513
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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 );
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.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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 );
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
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
		if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) {
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;
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
				}
148 2
149
			}
150 2
151 2
			// Cache the results for one hour
152 2
			Give_Cache::set( $key, $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.
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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 );
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
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
		if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) {
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
238
239
		$give_forms = $wpdb->get_results( $wpdb->prepare(
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
}