Passed
Pull Request — master (#404)
by Brian
05:01
created

GetPaid_Subscription_Data_Store   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 85
c 1
b 0
f 0
dl 0
loc 186
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 40 5
A create() 0 26 3
A delete() 0 18 1
A read() 0 41 5
1
<?php
2
3
/**
4
 * GetPaid_Subscription_Data_Store class file.
5
 *
6
 */
7
if ( ! defined( 'ABSPATH' ) ) {
8
	exit;
9
}
10
11
/**
12
 * Subscription Data Store: Stored in a custom table.
13
 *
14
 * @version  1.0.19
15
 */
16
class GetPaid_Subscription_Data_Store {
17
18
	/**
19
	 * A map of database fields to data types.
20
	 *
21
	 * @since 1.0.19
22
	 * @var array
23
	 */
24
	protected $database_fields_to_data_type = array(
25
		'id'                => '%d',
26
		'customer_id'       => '%d',
27
		'frequency'         => '%d',
28
		'period'            => '%s',
29
		'initial_amount'    => '%s',
30
		'recurring_amount'  => '%s',
31
		'bill_times'        => '%d',
32
		'transaction_id'    => '%s',
33
		'parent_payment_id' => '%d',
34
		'product_id'        => '%d',
35
		'created'           => '%s',
36
		'expiration'        => '%s',
37
		'trial_period'      => '%s',
38
		'status'            => '%s',
39
		'profile_id'        => '%s',
40
	);
41
42
	/*
43
	|--------------------------------------------------------------------------
44
	| CRUD Methods
45
	|--------------------------------------------------------------------------
46
	*/
47
48
	/**
49
	 * Method to create a new subscription in the database.
50
	 *
51
	 * @param WPInv_Subscription $subscription Subscription object.
52
	 */
53
	public function create( &$subscription ) {
54
		global $wpdb;
55
56
		$values  = array();
57
		$formats = array();
58
59
		$fields = $this->database_fields_to_data_type;
60
		unset( $fields['id'] );
61
62
		foreach ( $fields as $key => $format ) {
63
			$method     = "get_$key";
64
			$values[]   = $subscription->$method( 'edit' );
65
			$formats[]  = $format;
66
		}
67
68
		$result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $fields, $formats );
69
70
		if ( $result ) {
71
			$subscription->set_id( $wpdb->insert_id );
72
			$subscription->apply_changes();
73
			$subscription->clear_cache();
74
			do_action( 'getpaid_new_subscription', $subscription );
75
			return true;
76
		}
77
78
		return false;
79
	}
80
81
	/**
82
	 * Method to read a subscription from the database.
83
	 *
84
	 * @param WPInv_Subscription $subscription Subscription object.
85
	 *
86
	 */
87
	public function read( &$subscription ) {
88
		global $wpdb;
89
90
		$subscription->set_defaults();
91
92
		if ( ! $subscription->get_id() ) {
93
			$subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' );
94
			$subscription->set_id( 0 );
95
			return false;
96
		}
97
98
		// Maybe retrieve from the cache.
99
		$raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' );
100
101
		// If not found, retrieve from the db.
102
		if ( false === $raw_subscription ) {
103
104
			$raw_subscription = $wpdb->get_row(
105
				$wpdb->prepare(
106
					"SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d",
107
					$subscription->get_id()
108
				)
109
			);
110
111
			// Update the cache with our data
112
			wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' );
113
114
		}
115
116
		if ( ! $raw_subscription ) {
117
			$subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' );
118
			return false;
119
		}
120
121
		foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) {
122
			$method     = "set_$key";
123
			$subscription->$method( $raw_subscription->$key );
124
		}
125
126
		$subscription->set_object_read( true );
127
		do_action( 'getpaid_read_subscription', $subscription );
128
129
	}
130
131
	/**
132
	 * Method to update a subscription in the database.
133
	 *
134
	 * @param WPInv_Subscription $subscription Subscription object.
135
	 */
136
	public function update( &$subscription ) {
137
		global $wpdb;
138
139
		if ( null === $subscription->get_date_created( 'edit' ) ) {
0 ignored issues
show
introduced by
The condition null === $subscription->get_date_created('edit') is always false.
Loading history...
140
			$subscription->set_date_created(  current_time('mysql') );
141
		}
142
143
		$changes = $subscription->get_changes();
144
		$values  = array();
145
		$format  = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $format is dead and can be removed.
Loading history...
146
147
		foreach ( $this->database_fields_to_data_type as $key => $format ) {
148
			if ( array_key_exists( $key, $changes ) ) {
149
				$method     = "get_$key";
150
				$values[]   = $subscription->$method( 'edit' );
151
				$formats[]  = $format;
152
			}
153
		}
154
155
		if ( empty( $values ) ) {
156
			return;
157
		}
158
159
		$wpdb->update(
160
			$wpdb->prefix . 'wpinv_subscriptions',
161
			$values,
162
			array(
163
				'id' => $subscription->get_id(),
164
			),
165
			$formats
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $formats does not seem to be defined for all execution paths leading up to this point.
Loading history...
166
		);
167
168
		// Apply the changes.
169
		$subscription->apply_changes();
170
171
		// Delete cache.
172
		$subscription->clear_cache();
173
174
		// Fire a hook.
175
		do_action( 'getpaid_update_subscription', $subscription->get_id(), $subscription );
176
177
	}
178
179
	/**
180
	 * Method to delete a subscription from the database.
181
	 *
182
	 * @param WPInv_Subscription $subscription
183
	 */
184
	public function delete( &$subscription ) {
185
		global $wpdb;
186
187
		$wpdb->query(
188
			$wpdb->prepare(
189
				"DELETE FROM {$wpdb->prefix}getpaid_subscriptions
190
				WHERE id = %d",
191
				$subscription->get_id()
192
			)
193
		);
194
195
		// Delete cache.
196
		$subscription->clear_cache();
197
198
		// Fire a hook.
199
		do_action( 'getpaid_delete_subscription', $subscription->get_id(), $subscription );
200
201
		$subscription->set_id( 0 );
202
	}
203
204
	/*
205
	|--------------------------------------------------------------------------
206
	| Additional Methods
207
	|--------------------------------------------------------------------------
208
	*/
209
}
210