Total Complexity | 40 |
Total Lines | 260 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like CreatePaymentV2Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CreatePaymentV2Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
69 | class CreatePaymentV2Request extends RequestEntity |
||
70 | { |
||
71 | /** @var ChannelEntity $channel */ |
||
72 | protected $channel; |
||
73 | |||
74 | /** @var string $paymentMethod */ |
||
75 | protected $paymentMethod; |
||
76 | |||
77 | /** @var string|null */ |
||
78 | protected $variantId; |
||
79 | |||
80 | /** @var float $amount */ |
||
81 | protected $amount; |
||
82 | |||
83 | /** @var float $fee */ |
||
84 | protected $fee; |
||
85 | |||
86 | /** @var string $orderId */ |
||
87 | protected $orderId; |
||
88 | |||
89 | /** @var string $currency */ |
||
90 | protected $currency; |
||
91 | |||
92 | /** @var CartItemEntity[] $cart */ |
||
93 | protected $cart; |
||
94 | |||
95 | /** @var \DateTime|bool $birthdate */ |
||
96 | protected $birthdate; |
||
97 | |||
98 | /** @var string $phone */ |
||
99 | protected $phone; |
||
100 | |||
101 | /** @var string $email */ |
||
102 | protected $email; |
||
103 | |||
104 | /** @var string $locale */ |
||
105 | protected $locale; |
||
106 | |||
107 | /** @var string $successUrl */ |
||
108 | protected $successUrl; |
||
109 | |||
110 | /** @var string $failureUrl */ |
||
111 | protected $failureUrl; |
||
112 | |||
113 | /** @var string $cancelUrl */ |
||
114 | protected $cancelUrl; |
||
115 | |||
116 | /** @var string $noticeUrl */ |
||
117 | protected $noticeUrl; |
||
118 | |||
119 | /** @var string $pendingUrl */ |
||
120 | protected $pendingUrl; |
||
121 | |||
122 | /** @var string $customerRedirectUrl */ |
||
123 | protected $customerRedirectUrl; |
||
124 | |||
125 | /** @var string $xFrameHost */ |
||
126 | protected $xFrameHost; |
||
127 | |||
128 | /** @var string $pluginVersion */ |
||
129 | protected $pluginVersion; |
||
130 | |||
131 | /** @var CustomerAddressEntity $shippingAddress */ |
||
132 | protected $shippingAddress; |
||
133 | |||
134 | /** @var CustomerAddressEntity $billingAddress */ |
||
135 | protected $billingAddress; |
||
136 | |||
137 | /** @var PaymentDataEntity $paymentData */ |
||
138 | protected $paymentData; |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function getRequired() |
||
150 | ]; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
156 | */ |
||
157 | public function isValid() |
||
158 | { |
||
159 | if (is_array($this->cart)) { |
||
|
|||
160 | foreach ($this->cart as $item) { |
||
161 | if (!$item instanceof CartItemEntity || !$item->isValid()) { |
||
162 | return false; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | return parent::isValid() && |
||
168 | is_numeric($this->amount) && |
||
169 | is_array($this->cart) && |
||
170 | !empty($this->cart) && |
||
171 | (!$this->fee || is_numeric($this->fee)) && |
||
172 | (!$this->birthdate || $this->birthdate instanceof \DateTime); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Sets Cart |
||
177 | * |
||
178 | * @param array|string $cart |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function setCart($cart) |
||
183 | { |
||
184 | if (!$cart) { |
||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | if (is_string($cart)) { |
||
189 | $cart = json_decode($cart); |
||
190 | } |
||
191 | |||
192 | if (!is_array($cart)) { |
||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | $this->cart = []; |
||
197 | |||
198 | foreach ($cart as $item) { |
||
199 | $this->cart[] = new CartItemEntity($item); |
||
200 | } |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Sets shipping address |
||
207 | * |
||
208 | * @param CustomerAddressEntity|string $shippingAddress |
||
209 | * |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function setShippingAddress($shippingAddress) |
||
213 | { |
||
214 | if (!$shippingAddress) { |
||
215 | return $this; |
||
216 | } |
||
217 | |||
218 | if (is_string($shippingAddress)) { |
||
219 | $shippingAddress = json_decode($shippingAddress); |
||
220 | } |
||
221 | |||
222 | if (!is_array($shippingAddress) && !is_object($shippingAddress)) { |
||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | $this->shippingAddress = new CustomerAddressEntity($shippingAddress); |
||
227 | |||
228 | return $this; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Sets billing address |
||
233 | * |
||
234 | * @param CustomerAddressEntity|string $billingAddress |
||
235 | * |
||
236 | * @return $this |
||
237 | */ |
||
238 | public function setBillingAddress($billingAddress) |
||
239 | { |
||
240 | if (!$billingAddress) { |
||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | if (is_string($billingAddress)) { |
||
245 | $billingAddress = json_decode($billingAddress); |
||
246 | } |
||
247 | |||
248 | if (!is_array($billingAddress) && !is_object($billingAddress)) { |
||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | $this->billingAddress = new CustomerAddressEntity($billingAddress); |
||
253 | |||
254 | return $this; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Sets Birthdate |
||
259 | * |
||
260 | * @param string $birthdate |
||
261 | * |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function setBirthdate($birthdate) |
||
265 | { |
||
266 | if ($birthdate) { |
||
267 | $this->birthdate = date_create($birthdate); |
||
268 | } |
||
269 | |||
270 | return $this; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Sets payment data |
||
275 | * |
||
276 | * @param PaymentDataEntity|array|string $paymentData |
||
277 | * |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function setPaymentData($paymentData) |
||
281 | { |
||
282 | if (!$paymentData) { |
||
283 | return $this; |
||
284 | } |
||
285 | |||
286 | if ($paymentData instanceof PaymentDataEntity) { |
||
287 | $this->paymentData = $paymentData; |
||
288 | |||
289 | return $this; |
||
290 | } |
||
291 | |||
292 | if (is_string($paymentData)) { |
||
293 | $paymentData = json_decode($paymentData, true); |
||
294 | } |
||
295 | |||
296 | if (!is_array($paymentData)) { |
||
297 | return $this; |
||
298 | } |
||
299 | |||
300 | $this->paymentData = new PaymentDataEntity($paymentData); |
||
301 | |||
302 | return $this; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Sets Channel |
||
307 | * |
||
308 | * @param ChannelEntity|string $channel |
||
309 | * |
||
310 | * @return $this |
||
311 | */ |
||
312 | public function setChannel($channel) |
||
329 | } |
||
330 | } |
||
331 |