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