Completed
Push — develop ( f29211...ed262e )
by Remco
10:29 queued 57s
created

PaymentsDataStoreCPT::update_meta_status()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payments Data Store Custom Post Type
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Payments
9
 */
10
11
namespace Pronamic\WordPress\Pay\Payments;
12
13
use DateTimeZone;
14
use Pronamic\WordPress\Pay\AbstractDataStoreCPT;
15
use Pronamic\WordPress\Pay\DateTime;
16
use Pronamic\WordPress\Pay\Core\Statuses;
17
18
/**
19
 * Title: Payments data store CPT
20
 * Description:
21
 * Copyright: Copyright (c) 2005 - 2018
22
 * Company: Pronamic
23
 *
24
 * @see https://woocommerce.com/2017/04/woocommerce-3-0-release/
25
 * @see https://woocommerce.wordpress.com/2016/10/27/the-new-crud-classes-in-woocommerce-2-7/
26
 * @author Remco Tolsma
27
 * @version 3.7.0
28
 * @since 3.7.0
29
 */
30
class PaymentsDataStoreCPT extends AbstractDataStoreCPT {
31
	/**
32
	 * Construct payments data store CPT object.
33
	 */
34
	public function __construct() {
35
		$this->meta_key_prefix = '_pronamic_payment_';
36
	}
37
38
	/**
39
	 * Create payment.
40
	 *
41
	 * @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/abstract-wc-order-data-store-cpt.php#L47-L76
42
	 *
43
	 * @param Payment $payment The payment to create in this data store.
44
	 *
45
	 * @return bool
46
	 */
47
	public function create( Payment $payment ) {
48
		$title = $payment->title;
49
50
		if ( empty( $title ) ) {
51
			$title = sprintf(
52
				'Payment – %s',
53
				date_i18n( _x( '@todo', 'Payment title date format parsed by `date_i18n`.', 'pronamic_ideal' ) )
54
			);
55
		}
56
57
		$result = wp_insert_post(
58
			array(
59
				'post_type'     => 'pronamic_payment',
60
				'post_date_gmt' => $payment->date->format( 'Y-m-d H:i:s' ),
61
				'post_title'    => $title,
62
				'post_status'   => $this->get_post_status( $payment ),
63
				'post_author'   => $payment->user_id,
0 ignored issues
show
Bug introduced by
The property user_id does not seem to exist on Pronamic\WordPress\Pay\Payments\Payment.
Loading history...
64
			), true
65
		);
66
67
		if ( is_wp_error( $result ) ) {
68
			return false;
69
		}
70
71
		$payment->set_id( $result );
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type WP_Error; however, parameter $id of Pronamic\WordPress\Pay\Payments\Payment::set_id() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
		$payment->set_id( /** @scrutinizer ignore-type */ $result );
Loading history...
72
		$payment->post = get_post( $result );
0 ignored issues
show
Documentation Bug introduced by
It seems like get_post($result) can also be of type array. However, the property $post is declared as type WP_Post. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Bug introduced by
It seems like $result can also be of type WP_Error; however, parameter $post of get_post() does only seem to accept null|integer|WP_Post, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
		$payment->post = get_post( /** @scrutinizer ignore-type */ $result );
Loading history...
73
74
		$this->update_post_meta( $payment );
75
76
		do_action( 'pronamic_pay_new_payment', $payment );
77
78
		return true;
79
	}
80
81
	/**
82
	 * Read payment.
83
	 *
84
	 * @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/abstracts/abstract-wc-order.php#L85-L111
85
	 * @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/abstract-wc-order-data-store-cpt.php#L78-L111
86
	 * @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/class-wc-order-data-store-cpt.php#L81-L136
87
	 * @see https://developer.wordpress.org/reference/functions/get_post/
88
	 * @see https://developer.wordpress.org/reference/classes/wp_post/
89
	 *
90
	 * @param Payment $payment The payment to read from this data store.
91
	 */
92
	public function read( Payment $payment ) {
93
		$payment->post    = get_post( $payment->get_id() );
0 ignored issues
show
Documentation Bug introduced by
It seems like get_post($payment->get_id()) can also be of type array. However, the property $post is declared as type WP_Post. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Bug introduced by
$payment->get_id() of type string is incompatible with the type null|integer|WP_Post expected by parameter $post of get_post(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
		$payment->post    = get_post( /** @scrutinizer ignore-type */ $payment->get_id() );
Loading history...
94
		$payment->title   = get_the_title( $payment->get_id() );
0 ignored issues
show
Bug introduced by
$payment->get_id() of type string is incompatible with the type integer|WP_Post expected by parameter $post of get_the_title(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
		$payment->title   = get_the_title( /** @scrutinizer ignore-type */ $payment->get_id() );
Loading history...
95
		$payment->date    = new DateTime( get_post_field( 'post_date_gmt', $payment->get_id(), 'raw' ), new DateTimeZone( 'UTC' ) );
0 ignored issues
show
Bug Best Practice introduced by
The property date does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
$payment->get_id() of type string is incompatible with the type integer|WP_Post expected by parameter $post of get_post_field(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
		$payment->date    = new DateTime( get_post_field( 'post_date_gmt', /** @scrutinizer ignore-type */ $payment->get_id(), 'raw' ), new DateTimeZone( 'UTC' ) );
Loading history...
96
		$payment->user_id = get_post_field( 'post_author', $payment->get_id(), 'raw' );
0 ignored issues
show
Bug introduced by
The property user_id does not seem to exist on Pronamic\WordPress\Pay\Payments\Payment.
Loading history...
97
98
		$this->read_post_meta( $payment );
99
	}
100
101
	/**
102
	 * Update payment.
103
	 *
104
	 * @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/abstract-wc-order-data-store-cpt.php#L113-L154
105
	 * @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/class-wc-order-data-store-cpt.php#L154-L257
106
	 * @param Payment $payment The payment to update in this data store.
107
	 */
108
	public function update( Payment $payment ) {
109
		$data = array(
110
			'ID' => $payment->get_id(),
111
		);
112
113
		$post_status = $this->get_post_status( $payment, null );
114
115
		if ( null !== $post_status ) {
0 ignored issues
show
introduced by
The condition null !== $post_status is always true.
Loading history...
116
			$data['post_status'] = $post_status;
117
		}
118
119
		wp_update_post( $data );
120
121
		$this->update_post_meta( $payment );
122
	}
123
124
	/**
125
	 * Get post status.
126
	 *
127
	 * @param Payment $payment The payment to get a WordPress post status for.
128
	 * @param string  $default The default WordPress post status to return.
129
	 *
130
	 * @return string
131
	 */
132
	private function get_post_status( $payment, $default = 'payment_pending' ) {
133
		switch ( $payment->status ) {
134
			case Statuses::CANCELLED:
135
				return 'payment_cancelled';
136
			case Statuses::EXPIRED:
137
				return 'payment_expired';
138
			case Statuses::FAILURE:
139
				return 'payment_failed';
140
			case Statuses::SUCCESS:
141
				return 'payment_completed';
142
			case Statuses::OPEN:
143
				return 'payment_pending';
144
			default:
145
				return $default;
146
		}
147
	}
148
149
	/**
150
	 * Read post meta.
151
	 *
152
	 * @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/abstracts/abstract-wc-data.php#L462-L507
153
	 * @param Payment $payment The payment to read.
154
	 */
155
	private function read_post_meta( $payment ) {
156
		$id = $payment->get_id();
157
158
		$payment->config_id           = $this->get_meta( $id, 'config_id' );
0 ignored issues
show
Documentation Bug introduced by
The property $config_id was declared of type integer, but $this->get_meta($id, 'config_id') is of type string. 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...
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of Pronamic\WordPress\Pay\A...ataStoreCPT::get_meta(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
		$payment->config_id           = $this->get_meta( /** @scrutinizer ignore-type */ $id, 'config_id' );
Loading history...
159
		$payment->key                 = $this->get_meta( $id, 'key' );
160
		$payment->amount              = (float) $this->get_meta( $id, 'amount' );
161
		$payment->currency            = $this->get_meta( $id, 'currency' );
162
		$payment->method              = $this->get_meta( $id, 'method' );
163
		$payment->issuer              = $this->get_meta( $id, 'issuer' );
164
		$payment->order_id            = $this->get_meta( $id, 'order_id' );
165
		$payment->transaction_id      = $this->get_meta( $id, 'transaction_id' );
166
		$payment->entrance_code       = $this->get_meta( $id, 'entrance_code' );
167
		$payment->action_url          = $this->get_meta( $id, 'action_url' );
168
		$payment->source              = $this->get_meta( $id, 'source' );
169
		$payment->source_id           = $this->get_meta( $id, 'source_id' );
170
		$payment->description         = $this->get_meta( $id, 'description' );
171
		$payment->language            = $this->get_meta( $id, 'language' );
172
		$payment->locale              = $this->get_meta( $id, 'locale' );
173
		$payment->email               = $this->get_meta( $id, 'email' );
174
		$payment->status              = $this->get_meta( $id, 'status' );
175
		$payment->customer_name       = $this->get_meta( $id, 'customer_name' );
176
		$payment->address             = $this->get_meta( $id, 'address' );
177
		$payment->zip                 = $this->get_meta( $id, 'zip' );
178
		$payment->city                = $this->get_meta( $id, 'city' );
179
		$payment->country             = $this->get_meta( $id, 'country' );
180
		$payment->telephone_number    = $this->get_meta( $id, 'telephone_number' );
181
		$payment->analytics_client_id = $this->get_meta( $id, 'analytics_client_id' );
182
		$payment->subscription_id     = $this->get_meta( $id, 'subscription_id' );
183
		$payment->recurring_type      = $this->get_meta( $id, 'recurring_type' );
184
		$payment->recurring           = $this->get_meta( $id, 'recurring' );
0 ignored issues
show
Documentation Bug introduced by
The property $recurring was declared of type boolean, but $this->get_meta($id, 'recurring') is of type string. 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...
185
		$payment->start_date          = $this->get_meta_date( $id, 'start_date' );
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of Pronamic\WordPress\Pay\A...oreCPT::get_meta_date(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

185
		$payment->start_date          = $this->get_meta_date( /** @scrutinizer ignore-type */ $id, 'start_date' );
Loading history...
186
		$payment->end_date            = $this->get_meta_date( $id, 'end_date' );
187
	}
188
189
	/**
190
	 * Update payment post meta.
191
	 *
192
	 * @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/class-wc-order-data-store-cpt.php#L154-L257
193
	 * @param Payment $payment The payment to update.
194
	 */
195
	private function update_post_meta( $payment ) {
196
		$id = $payment->get_id();
197
198
		$this->update_meta( $id, 'config_id', $payment->config_id );
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of Pronamic\WordPress\Pay\A...StoreCPT::update_meta(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
		$this->update_meta( /** @scrutinizer ignore-type */ $id, 'config_id', $payment->config_id );
Loading history...
199
		$this->update_meta( $id, 'key', $payment->key );
200
		$this->update_meta( $id, 'order_id', $payment->order_id );
201
		$this->update_meta( $id, 'currency', $payment->currency );
202
		$this->update_meta( $id, 'amount', $payment->amount );
203
		$this->update_meta( $id, 'method', $payment->method );
204
		$this->update_meta( $id, 'issuer', $payment->issuer );
205
		$this->update_meta( $id, 'expiration_period', null );
206
		$this->update_meta( $id, 'language', $payment->language );
207
		$this->update_meta( $id, 'locale', $payment->locale );
208
		$this->update_meta( $id, 'entrance_code', $payment->entrance_code );
209
		$this->update_meta( $id, 'description', $payment->description );
210
		$this->update_meta( $id, 'first_name', $payment->first_name );
211
		$this->update_meta( $id, 'last_name', $payment->last_name );
212
		$this->update_meta( $id, 'consumer_name', $payment->consumer_name );
213
		$this->update_meta( $id, 'consumer_account_number', $payment->consumer_account_number );
214
		$this->update_meta( $id, 'consumer_iban', $payment->consumer_iban );
215
		$this->update_meta( $id, 'consumer_bic', $payment->consumer_bic );
216
		$this->update_meta( $id, 'consumer_city', $payment->consumer_city );
217
		$this->update_meta( $id, 'source', $payment->source );
218
		$this->update_meta( $id, 'source_id', $payment->source_id );
219
		$this->update_meta( $id, 'email', $payment->email );
220
		$this->update_meta( $id, 'customer_name', $payment->customer_name );
221
		$this->update_meta( $id, 'address', $payment->address );
222
		$this->update_meta( $id, 'zip', $payment->zip );
223
		$this->update_meta( $id, 'city', $payment->city );
224
		$this->update_meta( $id, 'country', $payment->country );
225
		$this->update_meta( $id, 'telephone_number', $payment->telephone_number );
226
		$this->update_meta( $id, 'analytics_client_id', $payment->analytics_client_id );
227
		$this->update_meta( $id, 'subscription_id', $payment->subscription_id );
228
		$this->update_meta( $id, 'recurring_type', $payment->recurring_type );
229
		$this->update_meta( $id, 'recurring', $payment->recurring );
230
		$this->update_meta( $id, 'transaction_id', $payment->get_transaction_id() );
231
		$this->update_meta( $id, 'action_url', $payment->get_action_url() );
232
		$this->update_meta( $id, 'start_date', $payment->start_date );
233
		$this->update_meta( $id, 'end_date', $payment->end_date );
234
235
		$this->update_meta_status( $payment );
236
	}
237
238
	/**
239
	 * Update meta status.
240
	 *
241
	 * @param Payment $payment The payment to update the status for.
242
	 */
243
	public function update_meta_status( $payment ) {
244
		$id = $payment->get_id();
245
246
		$previous_status = $this->get_meta( $id, 'status' );
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of Pronamic\WordPress\Pay\A...ataStoreCPT::get_meta(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

246
		$previous_status = $this->get_meta( /** @scrutinizer ignore-type */ $id, 'status' );
Loading history...
247
		$previous_status = strtolower( $previous_status );
248
		$previous_status = empty( $previous_status ) ? 'unknown' : $previous_status;
249
250
		$this->update_meta( $id, 'status', $payment->status );
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of Pronamic\WordPress\Pay\A...StoreCPT::update_meta(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

250
		$this->update_meta( /** @scrutinizer ignore-type */ $id, 'status', $payment->status );
Loading history...
251
252
		if ( $previous_status !== $payment->status ) {
253
			$can_redirect = false;
254
255
			do_action( 'pronamic_payment_status_update_' . $payment->source . '_' . $previous_status . '_to_' . $payment->status, $payment, $can_redirect );
256
			do_action( 'pronamic_payment_status_update_' . $payment->source, $payment, $can_redirect );
257
			do_action( 'pronamic_payment_status_update', $payment, $can_redirect );
258
		}
259
	}
260
}
261