Test Failed
Push — develop ( 48d69e...0a5a7a )
by Remco
04:35
created

Payment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 13
dl 0
loc 14
rs 9.8333

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Payment
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Mollie
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use DateTimeInterface;
14
15
/**
16
 * Payment
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.1.0
20
 * @since   2.1.0
21
 */
22
class Payment {
23
	/**
24
	 * The identifier uniquely referring to this payment.
25
	 *
26
	 * @var string
27
	 */
28
	private $id;
29
30
	/**
31
	 * The payment’s status.
32
	 *
33
	 * @var string
34
	 */
35
	private $status;
36
37
	/**
38
	 * The identifier referring to the profile this payment was created on.
39
	 *
40
	 * @var string
41
	 */
42
	private $profile_id;
43
44
	/**
45
	 * If a customer was specified upon payment creation, the customer’s token will be available here as well.
46
	 *
47
	 * @var string|null
48
	 */
49
	private $customer_id;
50
51
	/**
52
	 * If the payment is a first or recurring payment, this field will hold the ID of the mandate.
53
	 *
54
	 * @var string|null
55
	 */
56
	private $mandate_id;
57
58
	/**
59
	 * Construct payment.
60
	 *
61
	 * @param string            $id            Identifier.
62
	 * @param string            $mode          Mode.
63
	 * @param DateTimeInterface $created_at    Created at.
64
	 * @param string            $status        Status.
65
	 * @param Amount            $amount        Amount.
66
	 * @param string            $description   Description.
67
	 * @param string|null       $redirect_url  Redirect URL.
68
	 * @param string            $method        Method.
69
	 * @param string            $metadata      Metadata.
70
	 * @param string            $locale        Locale.
71
	 * @param string            $profile_id    Profile ID.
72
	 * @param string            $sequence_type Sequence type.
73
	 * @param object            $links         Links.
74
	 */
75
	public function __construct( $id, $mode, DateTimeInterface $created_at, $status, Amount $amount, $description, $redirect_url, $method, $metadata, $locale, $profile_id, $sequence_type, $links ) {
76
		$this->id            = $id;
77
		$this->mode          = $mode;
0 ignored issues
show
Bug Best Practice introduced by
The property mode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
		$this->created_at    = $created_at;
0 ignored issues
show
Bug Best Practice introduced by
The property created_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
		$this->status        = $status;
80
		$this->amount        = $amount;
0 ignored issues
show
Bug Best Practice introduced by
The property amount does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
		$this->description   = $description;
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
82
		$this->redirect_url  = $redirect_url;
0 ignored issues
show
Bug Best Practice introduced by
The property redirect_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83
		$this->method        = $method;
0 ignored issues
show
Bug Best Practice introduced by
The property method does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84
		$this->metadata      = $metadata;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
85
		$this->locale        = $locale;
0 ignored issues
show
Bug Best Practice introduced by
The property locale does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
		$this->profile_id    = $profile_id;
87
		$this->sequence_type = $sequence_type;
0 ignored issues
show
Bug Best Practice introduced by
The property sequence_type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
88
		$this->links         = $links;
0 ignored issues
show
Bug Best Practice introduced by
The property links does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
89
	}
90
91
	/**
92
	 * Get status.
93
	 *
94
	 * @return string
95
	 */
96
	public function get_status() {
97
		return $this->status;
98
	}
99
100
	/**
101
	 * Get profile ID.
102
	 *
103
	 * @return string
104
	 */
105
	public function get_profile_id() {
106
		return $this->profile_id;
107
	}
108
109
	/**
110
	 * Get customer ID.
111
	 *
112
	 * @return string|null
113
	 */
114
	public function get_customer_id() {
115
		return $this->customer_id;
116
	}
117
118
	/**
119
	 * Set customer ID.
120
	 *
121
	 * @param string|null $customer_id Customer ID.
122
	 */
123
	public function set_customer_id( $customer_id ) {
124
		$this->customer_id = $customer_id;
125
	}
126
127
	/**
128
	 * Get mandate ID.
129
	 *
130
	 * @return string|null
131
	 */
132
	public function get_mandate_id() {
133
		return $this->mandate_id;
134
	}
135
136
	/**
137
	 * Set mandate ID.
138
	 *
139
	 * @param string|null $mandate_id Mandate ID.
140
	 */
141
	public function set_mandate_id( $mandate_id ) {
142
		$this->mandate_id = $mandate_id;
143
	}
144
145
	/**
146
	 * Has chargebacks.
147
	 *
148
	 * @link https://github.com/mollie/mollie-api-php/blob/v2.24.0/src/Resources/Payment.php#L358-L366
149
	 * @return bool True if payment has chargebacks, false otherwise.
150
	 */
151
	public function has_chargebacks() {
152
		return ! empty( $this->links->chargebacks );
153
	}
154
155
	/**
156
	 * Create payment from JSON.
157
	 *
158
	 * @param object $json JSON object.
159
	 * @return Payment
160
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
161
	 */
162
	public static function from_json( $json ) {
163
		$validator = new \JsonSchema\Validator();
164
165
		$validator->validate(
166
			$json,
167
			(object) array(
168
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment.json' ),
169
			),
170
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
171
		);
172
173
		$payment = new Payment(
174
			$json->id,
175
			$json->mode,
176
			new \DateTimeImmutable( $json->createdAt ),
177
			$json->status,
178
			Amount::from_json( $json->amount ),
179
			$json->description,
180
			$json->redirectUrl,
181
			$json->method,
182
			$json->metadata,
183
			$json->locale,
184
			$json->profileId,
185
			$json->sequenceType,
186
			$json->_links
187
		);
188
189
		if ( \property_exists( $json, 'customerId' ) ) {
190
			$payment->set_customer_id( $json->customerId );
191
		}
192
193
		if ( \property_exists( $json, 'mandateId' ) ) {
194
			$payment->set_mandate_id( $json->mandateId );
195
		}
196
197
		return $payment;
198
	}
199
}
200