Total Complexity | 76 |
Total Lines | 613 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GatewayService 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 GatewayService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class GatewayService extends GatewayAbstract |
||
31 | { |
||
32 | /** |
||
33 | * Gateway hostname. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | public $rocketGateHost; |
||
38 | |||
39 | /** |
||
40 | * Message protocol. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | public $rocketGateProtocol; |
||
45 | |||
46 | /** |
||
47 | * Network connection port. |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | public $rocketGatePortNo; |
||
52 | |||
53 | /** |
||
54 | * Destination servlet. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | public $rocketGateServlet; |
||
59 | |||
60 | /** |
||
61 | * Timeout for network connection. |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | public $rocketGateConnectTimeout; |
||
66 | |||
67 | /** |
||
68 | * Timeout for network read. |
||
69 | * |
||
70 | * @var int |
||
71 | */ |
||
72 | public $rocketGateReadTimeout; |
||
73 | |||
74 | /** |
||
75 | * Set the standard production destinations for the service. |
||
76 | * |
||
77 | * @param bool $testMode |
||
78 | */ |
||
79 | public function __construct(bool $testMode = true) |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Perform an auth-only transaction. |
||
89 | * |
||
90 | * @param GatewayRequest $request |
||
91 | * @param GatewayResponse $response |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function performAuthOnly(GatewayRequest $request, GatewayResponse $response) |
||
96 | { |
||
97 | $request->set(GatewayRequest::transactionType(), 'CC_AUTH'); |
||
98 | if (!empty($request->get(GatewayRequest::referenceGuid()))) { |
||
99 | if ($this->performTargetedTransaction($request, $response) === false) { |
||
100 | return false; |
||
101 | } |
||
102 | } elseif ($this->performTransaction($request, $response) === false) { |
||
103 | return false; |
||
104 | } |
||
105 | |||
106 | return $this->performConfirmation($request, $response); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Perform a ticket operation for a previous auth-only transaction. |
||
111 | * |
||
112 | * @param GatewayRequest $request |
||
113 | * @param GatewayResponse $response |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function performTicket(GatewayRequest $request, GatewayResponse $response) |
||
118 | { |
||
119 | $request->set(GatewayRequest::transactionType(), 'CC_TICKET'); |
||
120 | |||
121 | return $this->performTargetedTransaction($request, $response); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Perform a complete purchase transaction using the information contained in a request. |
||
126 | * |
||
127 | * @param GatewayRequest $request |
||
128 | * @param GatewayResponse $response |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function performPurchase(GatewayRequest $request, GatewayResponse $response) |
||
133 | { |
||
134 | $request->set(GatewayRequest::transactionType(), 'CC_PURCHASE'); |
||
135 | if (!empty($request->get(GatewayRequest::referenceGuid()))) { |
||
136 | if ($this->performTargetedTransaction($request, $response) === false) { |
||
137 | return false; |
||
138 | } |
||
139 | } elseif ($this->performTransaction($request, $response) === false) { |
||
140 | return false; |
||
141 | } |
||
142 | |||
143 | return $this->performConfirmation($request, $response); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Perform a credit operation for a previously completed transaction. |
||
148 | * |
||
149 | * @param GatewayRequest $request |
||
150 | * @param GatewayResponse $response |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function performCredit(GatewayRequest $request, GatewayResponse $response) |
||
155 | { |
||
156 | $request->set(GatewayRequest::transactionType(), 'CC_CREDIT'); |
||
157 | |||
158 | // If the credit references a previous transaction, we |
||
159 | // need to send it back to the origination site. Otherwise, |
||
160 | // it can be sent to any server. |
||
161 | |||
162 | if (!empty($request->get(GatewayRequest::referenceGuid()))) { |
||
163 | return $this->performTargetedTransaction($request, $response); |
||
164 | } |
||
165 | |||
166 | return $this->performTransaction($request, $response); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Perform a void operation for a previously completed transaction. |
||
171 | * |
||
172 | * @param GatewayRequest $request |
||
173 | * @param GatewayResponse $response |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function performVoid(GatewayRequest $request, GatewayResponse $response) |
||
178 | { |
||
179 | $request->set(GatewayRequest::transactionType(), 'CC_VOID'); |
||
180 | |||
181 | return $this->performTargetedTransaction($request, $response); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Perform scrubbing on a card/customer. |
||
186 | * |
||
187 | * @param GatewayRequest $request |
||
188 | * @param GatewayResponse $response |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function performCardScrub(GatewayRequest $request, GatewayResponse $response) |
||
193 | { |
||
194 | $request->set(GatewayRequest::transactionType(), 'CARDSCRUB'); |
||
195 | |||
196 | return $this->performTransaction($request, $response); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Schedule cancellation of rebilling. |
||
201 | * |
||
202 | * @param GatewayRequest $request |
||
203 | * @param GatewayResponse $response |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function performRebillCancel(GatewayRequest $request, GatewayResponse $response) |
||
208 | { |
||
209 | $request->set(GatewayRequest::transactionType(), 'REBILL_CANCEL'); |
||
210 | |||
211 | return $this->performTransaction($request, $response); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Update terms of rebilling. |
||
216 | * |
||
217 | * @param GatewayRequest $request |
||
218 | * @param GatewayResponse $response |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function performRebillUpdate(GatewayRequest $request, GatewayResponse $response) |
||
223 | { |
||
224 | $request->set(GatewayRequest::transactionType(), 'REBILL_UPDATE'); |
||
225 | |||
226 | $amount = $request->get(GatewayRequest::amount()); |
||
227 | if (empty($amount) || $amount <= 0) { |
||
228 | return $this->performTransaction($request, $response); |
||
229 | } |
||
230 | |||
231 | if ($this->performTransaction($request, $response) === false) { |
||
232 | return false; |
||
233 | } |
||
234 | |||
235 | return $this->performConfirmation($request, $response); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Lookup previous transaction. |
||
240 | * |
||
241 | * @param GatewayRequest $request |
||
242 | * @param GatewayResponse $response |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function performLookup(GatewayRequest $request, GatewayResponse $response) |
||
247 | { |
||
248 | $request->set(GatewayRequest::transactionType(), 'LOOKUP'); |
||
249 | |||
250 | if (!empty($request->get(GatewayRequest::referenceGuid()))) { |
||
251 | return $this->performTargetedTransaction($request, $response); |
||
252 | } |
||
253 | |||
254 | return $this->performTransaction($request, $response); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Upload card data to the servers. |
||
259 | * |
||
260 | * @param GatewayRequest $request |
||
261 | * @param GatewayResponse $response |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | public function performCardUpload(GatewayRequest $request, GatewayResponse $response) |
||
266 | { |
||
267 | $request->set(GatewayRequest::transactionType(), 'CARDUPLOAD'); |
||
268 | |||
269 | return $this->performTransaction($request, $response); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Add an entry to the XsellQueue. |
||
274 | * |
||
275 | * @param GatewayRequest $request |
||
276 | * @param GatewayResponse $response |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | public function generateXsell(GatewayRequest $request, GatewayResponse $response) |
||
281 | { |
||
282 | $request->set(GatewayRequest::transactionType(), 'GENERATEXSELL'); |
||
283 | $request->set(GatewayRequest::referenceGuid(), $request->get(GatewayRequest::xsellReferenceXact())); |
||
284 | |||
285 | if (!empty($request->get(GatewayRequest::referenceGuid()))) { |
||
286 | return $this->performTargetedTransaction($request, $response); |
||
287 | } |
||
288 | |||
289 | return $this->performTransaction($request, $response); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Set the communications parameters for production or test mode. |
||
294 | * |
||
295 | * @param bool $testFlag |
||
296 | */ |
||
297 | public function setTestMode(bool $testFlag) |
||
298 | { |
||
299 | if ($testFlag) { |
||
300 | $this->rocketGateHost = 'dev-gateway.rocketgate.com'; |
||
301 | $this->rocketGateProtocol = 'https'; |
||
302 | $this->rocketGatePortNo = 443; |
||
303 | } else { |
||
304 | $this->rocketGateHost = 'gateway.rocketgate.com'; |
||
305 | $this->rocketGateProtocol = 'https'; |
||
306 | $this->rocketGatePortNo = 443; |
||
307 | } |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Set the host used by the service. |
||
312 | * |
||
313 | * @param string $hostname |
||
314 | */ |
||
315 | public function setHost(string $hostname) |
||
316 | { |
||
317 | $this->rocketGateHost = $hostname; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Set the communications protocol used by the service. |
||
322 | * |
||
323 | * @param string $protocol |
||
324 | */ |
||
325 | public function setProtocol(string $protocol) |
||
326 | { |
||
327 | $this->rocketGateProtocol = $protocol; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Set the port number used by the service. |
||
332 | * |
||
333 | * @param int $portNo |
||
334 | */ |
||
335 | public function setPortNo(int $portNo) |
||
336 | { |
||
337 | $this->rocketGatePortNo = $portNo; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Set the servlet used by the service. |
||
342 | * |
||
343 | * @param string $servlet |
||
344 | */ |
||
345 | public function setServlet(string $servlet) |
||
346 | { |
||
347 | $this->rocketGateServlet = $servlet; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Set the timeout used during connection to the servlet. |
||
352 | * |
||
353 | * @param int $timeout |
||
354 | */ |
||
355 | public function setConnectTimeout(int $timeout) |
||
356 | { |
||
357 | $this->rocketGateConnectTimeout = $timeout; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Set the timeout used while waiting for the servlet to answer. |
||
362 | * |
||
363 | * @param int $timeout |
||
364 | */ |
||
365 | public function setReadTimeout(int $timeout) |
||
366 | { |
||
367 | $this->rocketGateReadTimeout = $timeout; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Perform the transaction outlined in a GatewayRequest. |
||
372 | * |
||
373 | * @param GatewayRequest $request |
||
374 | * @param GatewayResponse $response |
||
375 | * |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function performTransaction(GatewayRequest $request, GatewayResponse $response) |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Send a transaction to a server based upon the reference GUID. |
||
436 | * |
||
437 | * @param GatewayRequest $request |
||
438 | * @param GatewayResponse $response |
||
439 | * |
||
440 | * @return bool |
||
441 | */ |
||
442 | public function performTargetedTransaction(GatewayRequest $request, GatewayResponse $response) |
||
443 | { |
||
444 | $this->prepareRequest($request); |
||
445 | |||
446 | $referenceGUID = $request->get(GatewayRequest::referenceGuid()); |
||
447 | if (empty($referenceGUID)) { |
||
448 | $response->setResults(self::RESPONSE_REQUEST_ERROR, self::REASON_INVALID_REFGUID); |
||
449 | |||
450 | return false; |
||
451 | } |
||
452 | |||
453 | if (strlen($referenceGUID) > 15) { |
||
454 | $siteNo = substr($referenceGUID, 0, 2); |
||
455 | } else { |
||
456 | $siteNo = substr($referenceGUID, 0, 1); |
||
457 | } |
||
458 | $siteNo = hexdec($siteNo); |
||
459 | |||
460 | $serverName = $request->get('gatewayServer'); |
||
461 | if (empty($serverName)) { |
||
462 | $serverName = $this->rocketGateHost; |
||
463 | if (($separator = strpos($serverName, '.')) > 0) { |
||
464 | $prefix = substr($serverName, 0, $separator); |
||
465 | $serverName = substr($serverName, $separator); |
||
466 | $serverName = $prefix.'-'.$siteNo.$serverName; |
||
467 | } |
||
468 | } |
||
469 | |||
470 | $results = $this->performCURLTransaction($serverName, $request, $response); |
||
471 | if ((int) $results === self::RESPONSE_SUCCESS) { |
||
472 | return true; |
||
473 | } |
||
474 | |||
475 | return false; |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * @param GatewayRequest $request |
||
480 | */ |
||
481 | private function prepareRequest(GatewayRequest &$request) |
||
501 | } |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Perform the confirmation pass that tells the server we have received transaction reply. |
||
506 | * |
||
507 | * @param GatewayRequest $request |
||
508 | * @param GatewayResponse $response |
||
509 | * |
||
510 | * @return bool |
||
511 | */ |
||
512 | public function performConfirmation(GatewayRequest $request, GatewayResponse $response) |
||
513 | { |
||
514 | $confirmGUID = $response->get(GatewayResponse::transactId()); |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * Perform a transaction exchange with a given host. |
||
547 | * |
||
548 | * @param string $host |
||
549 | * @param GatewayRequest $request |
||
550 | * @param GatewayResponse $response |
||
551 | * |
||
552 | * @return int |
||
553 | */ |
||
554 | public function performCURLTransaction(string $host, GatewayRequest $request, GatewayResponse $response) |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * @param string $host |
||
629 | * @param GatewayRequest $request |
||
630 | * |
||
631 | * @return string |
||
632 | */ |
||
633 | private function getGatewayUrl(string $host, GatewayRequest $request) |
||
645 |