Completed
Branch BUG-9464-payment-methods-myste... (4799f5)
by
unknown
351:53 queued 337:20
created

Datetime   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 117
Duplicated Lines 53.85 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 11
Bugs 2 Features 3
Metric Value
wmc 13
c 11
b 2
f 3
lcom 1
cbo 5
dl 63
loc 117
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A spaces_remaining_considering_tickets() 18 18 4
A registrations_checked_in_count() 12 12 3
A registrations_checked_out_count() 12 12 3
A spots_taken_pending_payment() 21 21 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace EventEspresso\core\libraries\rest_api\calculations;
3
4
use EventEspresso\core\libraries\rest_api\calculations\Base as Calculations_Base;
5
use EventEspresso\core\libraries\rest_api\controllers\model\Base as Controller_Base;
6
/**
7
 *
8
 * Class Datetime
9
 *
10
 * Calculations relating to datetimes
11
 *
12
 * @package         Event Espresso
13
 * @subpackage
14
 * @author				Mike Nelson
15
 * @since		 	   $VID:$
16
 *
17
 */
18
if( !defined( 'EVENT_ESPRESSO_VERSION' ) ) {
19
	exit( 'No direct script access allowed' );
20
}
21
22
class Datetime extends Calculations_Base {
23
24
	/**
25
	 * Calculates the total spaces available on the datetime, taking into account
26
	 * ticket limits too.
27
	 *
28
	 * @see EE_Datetime::spaces_remaining( true )
29
	 * @param array            $wpdb_row
30
	 * @param \WP_REST_Request $request
31
	 * @param Controller_Base  $controller
32
	 * @return int
33
	 * @throws \EE_Error
34
	 */
35 View Code Duplication
	public static function spaces_remaining_considering_tickets( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $controller 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...
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...
36
		if( is_array( $wpdb_row ) && isset( $wpdb_row[ 'Datetime.DTT_ID' ] ) ) {
37
			$dtt_obj = \EEM_Datetime::instance()->get_one_by_ID( $wpdb_row[ 'Datetime.DTT_ID' ] );
38
		} else {
39
			$dtt_obj = null;
40
		}
41
		if( $dtt_obj instanceof \EE_Datetime ) {
42
			return $dtt_obj->spaces_remaining( true );
43
		} else {
44
			throw new \EE_Error(
45
				sprintf(
46
					__( 'Cannot calculate spaces_remaining_considering_tickets because the datetime with ID %1$s (from database row %2$s) was not found', 'event_espresso' ),
47
					$wpdb_row[ 'Datetime.DTT_ID' ],
48
					print_r( $wpdb_row, true )
49
				)
50
			);
51
		}
52
	}
53
54
55
56
	/**
57
	 * Counts registrations who have checked into this datetime
58
	 *
59
	 * @param array            $wpdb_row
60
	 * @param \WP_REST_Request $request
61
	 * @param Controller_Base  $controller
62
	 * @return int
63
	 * @throws \EE_Error
64
	 * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception
65
	 */
66 View Code Duplication
	public static function registrations_checked_in_count( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $controller 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...
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...
67
		if( ! is_array( $wpdb_row ) || ! isset( $wpdb_row[ 'Datetime.DTT_ID' ] ) ) {
68
			throw new \EE_Error(
69
				sprintf(
70
					__( 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', 'event_espresso' ),
71
					print_r( $wpdb_row, true )
72
				)
73
			);
74
		}
75
		self::_verify_current_user_can( 'ee_read_checkins', 'registrations_checked_in_count' );
76
		return \EEM_Registration::instance()->count_registrations_checked_into_datetime( $wpdb_row[ 'Datetime.DTT_ID' ], true );
77
	}
78
79
80
81
	/**
82
	 * Counts registrations who have checked out of this datetime
83
	 *
84
	 * @param array            $wpdb_row
85
	 * @param \WP_REST_Request $request
86
	 * @param Controller_Base  $controller
87
	 * @return int
88
	 * @throws \EE_Error
89
	 * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception
90
	 */
91 View Code Duplication
	public static function registrations_checked_out_count( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $controller 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...
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...
92
		if( ! is_array( $wpdb_row ) || ! isset( $wpdb_row[ 'Datetime.DTT_ID' ] ) ) {
93
			throw new \EE_Error(
94
				sprintf(
95
					__( 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', 'event_espresso' ),
96
					print_r( $wpdb_row, true )
97
				)
98
			);
99
		}
100
		self::_verify_current_user_can( 'ee_read_checkins', 'registrations_checked_out_count' );
101
		return \EEM_Registration::instance()->count_registrations_checked_into_datetime( $wpdb_row[ 'Datetime.DTT_ID' ], false );
102
	}
103
104
105
106
	/**
107
	 * Counts the number of pending-payment registrations for this event (regardless
108
	 * of how many datetimes each registrations' ticket purchase is for)
109
	 *
110
	 * @param array            $wpdb_row
111
	 * @param \WP_REST_Request $request
112
	 * @param Controller_Base  $controller
113
	 * @return int
114
	 * @throws \EE_Error
115
	 * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception
116
	 */
117 View Code Duplication
	public static function spots_taken_pending_payment( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $controller 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...
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...
118
		if( ! is_array( $wpdb_row ) || ! isset( $wpdb_row[ 'Datetime.DTT_ID' ] ) ) {
119
			throw new \EE_Error(
120
				sprintf(
121
					__( 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Datetime.DTT_ID"', 'event_espresso' ),
122
					print_r( $wpdb_row, true )
123
				)
124
			);
125
		}
126
		self::_verify_current_user_can( 'ee_read_registrations', 'spots_taken_pending_payment' );
127
		return \EEM_Registration::instance()->count(
128
			array(
129
				array(
130
					'Ticket.Datetime.DTT_ID' => $wpdb_row[ 'Datetime.DTT_ID' ],
131
					'STS_ID' => \EEM_Registration::status_id_pending_payment
132
				)
133
			),
134
			'REG_ID',
135
			true
136
		);
137
	}
138
}
139