Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Client 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
51 | class Client |
||
52 | { |
||
53 | /** |
||
54 | * Amadeus SOAP header version 1 |
||
55 | */ |
||
56 | const HEADER_V1 = "1"; |
||
57 | /** |
||
58 | * Amadeus SOAP header version 2 |
||
59 | */ |
||
60 | const HEADER_V2 = "2"; |
||
61 | /** |
||
62 | * Amadeus SOAP header version 4 |
||
63 | */ |
||
64 | const HEADER_V4 = "4"; |
||
65 | |||
66 | /** |
||
67 | * Version string |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | const version = "0.0.1dev"; |
||
72 | /** |
||
73 | * An identifier string for the library (to be used in Received From entries) |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | const receivedFromIdentifier = "amabnl-amadeus-ws-client"; |
||
78 | |||
79 | /** |
||
80 | * Session Handler will be sending all the messages and handling all session-related things. |
||
81 | * |
||
82 | * @var HandlerInterface |
||
83 | */ |
||
84 | protected $sessionHandler; |
||
85 | |||
86 | /** |
||
87 | * Request Creator is will create the correct message structure to send to the SOAP server. |
||
88 | * |
||
89 | * @var RequestCreatorInterface |
||
90 | */ |
||
91 | protected $requestCreator; |
||
92 | |||
93 | /** |
||
94 | * Response Handler will check the received response for errors. |
||
95 | * |
||
96 | * @var ResponseHandlerInterface |
||
97 | */ |
||
98 | protected $responseHandler; |
||
99 | |||
100 | /** |
||
101 | * Authentication parameters |
||
102 | * |
||
103 | * @var Params\AuthParams |
||
104 | */ |
||
105 | protected $authParams; |
||
106 | |||
107 | /** |
||
108 | * Set the session as stateful (true) or stateless (false) |
||
109 | * |
||
110 | * @param bool $newStateful |
||
111 | */ |
||
112 | public function setStateful($newStateful) |
||
116 | |||
117 | /** |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function isStateful() |
||
124 | |||
125 | /** |
||
126 | * Get the last raw XML message that was sent out |
||
127 | * |
||
128 | * @return string|null |
||
129 | */ |
||
130 | public function getLastRequest() |
||
134 | |||
135 | /** |
||
136 | * Get the last raw XML message that was received |
||
137 | * |
||
138 | * @return string|null |
||
139 | */ |
||
140 | public function getLastResponse() |
||
144 | |||
145 | /** |
||
146 | * Get session information for authenticated session |
||
147 | * |
||
148 | * - sessionId |
||
149 | * - sequenceNr |
||
150 | * - securityToken |
||
151 | * |
||
152 | * @return array|null |
||
153 | */ |
||
154 | public function getSessionData() |
||
158 | |||
159 | /** |
||
160 | * Restore a previously used session |
||
161 | * |
||
162 | * To be used when implementing your own session pooling system on legacy Soap Header 2 applications. |
||
163 | * |
||
164 | * @param array $sessionData |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function setSessionData(array $sessionData) |
||
171 | |||
172 | /** |
||
173 | * Construct Amadeus Web Services client |
||
174 | * |
||
175 | * @param Params $params |
||
176 | */ |
||
177 | public function __construct($params) |
||
178 | { |
||
179 | if ($params->authParams instanceof Params\AuthParams) { |
||
180 | $this->authParams = $params->authParams; |
||
181 | if (isset($params->sessionHandlerParams) && $params->sessionHandlerParams instanceof Params\SessionHandlerParams) { |
||
|
|||
182 | $params->sessionHandlerParams->authParams = $this->authParams; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $this->sessionHandler = $this->loadSessionHandler( |
||
187 | $params->sessionHandler, |
||
188 | $params->sessionHandlerParams |
||
189 | ); |
||
190 | |||
191 | $this->requestCreator = $this->loadRequestCreator( |
||
192 | $params->requestCreator, |
||
193 | $params->requestCreatorParams, |
||
194 | self::receivedFromIdentifier . "-" .self::version, |
||
195 | $this->sessionHandler->getOriginatorOffice(), |
||
196 | $this->sessionHandler->getMessagesAndVersions() |
||
197 | ); |
||
198 | |||
199 | $this->responseHandler = $this->loadResponseHandler( |
||
200 | $params->responseHandler |
||
201 | ); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Authenticate. |
||
206 | * |
||
207 | * Parameters were provided at construction time (sessionhandlerparams) |
||
208 | * |
||
209 | * @return Result |
||
210 | * @throws Exception |
||
211 | */ |
||
212 | View Code Duplication | public function securityAuthenticate() |
|
233 | |||
234 | /** |
||
235 | * Terminate a session - only applicable to non-stateless mode. |
||
236 | * |
||
237 | * @return Result |
||
238 | * @throws Exception |
||
239 | */ |
||
240 | View Code Duplication | public function securitySignOut() |
|
259 | |||
260 | /** |
||
261 | * PNR_Retrieve - Retrieve an Amadeus PNR by record locator |
||
262 | * |
||
263 | * By default, the result will be the PNR_Reply XML as string. |
||
264 | * That way you can easily parse the PNR's contents with XPath. |
||
265 | * |
||
266 | * https://webservices.amadeus.com/extranet/viewService.do?id=27&flavourId=1&menuId=functional |
||
267 | * |
||
268 | * @param RequestOptions\PnrRetrieveOptions $options |
||
269 | * @param array $messageOptions (OPTIONAL) Set ['asString'] = 'false' to get PNR_Reply as a PHP object. |
||
270 | * @return Result |
||
271 | * @throws Exception |
||
272 | */ |
||
273 | View Code Duplication | public function pnrRetrieve(RequestOptions\PnrRetrieveOptions $options, $messageOptions = []) |
|
292 | |||
293 | /** |
||
294 | * Create a PNR using PNR_AddMultiElements |
||
295 | * |
||
296 | * @param RequestOptions\PnrCreatePnrOptions $options |
||
297 | * @param array $messageOptions |
||
298 | * @return Result |
||
299 | */ |
||
300 | View Code Duplication | public function pnrCreatePnr(RequestOptions\PnrCreatePnrOptions $options, $messageOptions = []) |
|
319 | |||
320 | /** |
||
321 | * PNR_AddMultiElements - Create a new PNR or update an existing PNR. |
||
322 | * |
||
323 | * https://webservices.amadeus.com/extranet/viewService.do?id=25&flavourId=1&menuId=functional |
||
324 | * |
||
325 | * @todo implement message creation - maybe split up in separate Create & Modify PNR? |
||
326 | * @param RequestOptions\PnrAddMultiElementsOptions $options |
||
327 | * @param array $messageOptions |
||
328 | * @return Result |
||
329 | */ |
||
330 | View Code Duplication | public function pnrAddMultiElements(RequestOptions\PnrAddMultiElementsOptions $options, $messageOptions = []) |
|
349 | |||
350 | /** |
||
351 | * PNR_RetrieveAndDisplay - Retrieve an Amadeus PNR by record locator including extra info |
||
352 | * |
||
353 | * This extra info is info you cannot see in the regular PNR, like Offers. |
||
354 | * |
||
355 | * By default, the result will be the PNR_RetrieveAndDisplayReply XML as string. |
||
356 | * That way you can easily parse the PNR's contents with XPath. |
||
357 | * |
||
358 | * Set $messageOptions['asString'] = FALSE to get the response as a PHP object. |
||
359 | * |
||
360 | * https://webservices.amadeus.com/extranet/viewService.do?id=1922&flavourId=1&menuId=functional |
||
361 | * |
||
362 | * @param RequestOptions\PnrRetrieveAndDisplayOptions $options Amadeus Record Locator for PNR |
||
363 | * @param array $messageOptions (OPTIONAL) Set ['asString'] = 'false' to get PNR_RetrieveAndDisplayReply as a PHP object. |
||
364 | * @return Result |
||
365 | * @throws Exception |
||
366 | **/ |
||
367 | View Code Duplication | public function pnrRetrieveAndDisplay(RequestOptions\PnrRetrieveAndDisplayOptions $options, $messageOptions = []) |
|
386 | |||
387 | /** |
||
388 | * PNR_Cancel |
||
389 | * |
||
390 | * @param RequestOptions\PnrCancelOptions $options |
||
391 | * @param array $messageOptions |
||
392 | * @return Result |
||
393 | */ |
||
394 | View Code Duplication | public function pnrCancel(RequestOptions\PnrCancelOptions $options, $messageOptions = []) |
|
413 | |||
414 | /** |
||
415 | * Queue_List - get a list of all PNR's on a given queue |
||
416 | * |
||
417 | * https://webservices.amadeus.com/extranet/viewService.do?id=52&flavourId=1&menuId=functional |
||
418 | * |
||
419 | * @param RequestOptions\QueueListOptions $options |
||
420 | * @param array $messageOptions |
||
421 | * @return Result |
||
422 | */ |
||
423 | View Code Duplication | public function queueList(RequestOptions\QueueListOptions $options, $messageOptions = []) |
|
442 | |||
443 | /** |
||
444 | * Queue_PlacePNR - Place a PNR on a given queue |
||
445 | * |
||
446 | * @param RequestOptions\QueuePlacePnrOptions $options |
||
447 | * @param array $messageOptions |
||
448 | * @return Result |
||
449 | */ |
||
450 | View Code Duplication | public function queuePlacePnr(RequestOptions\QueuePlacePnrOptions $options, $messageOptions = []) |
|
469 | |||
470 | /** |
||
471 | * Queue_RemoveItem - remove an item (a PNR) from a given queue |
||
472 | * |
||
473 | * @param RequestOptions\QueueRemoveItemOptions $options |
||
474 | * @param array $messageOptions |
||
475 | * @return Result |
||
476 | */ |
||
477 | View Code Duplication | public function queueRemoveItem(RequestOptions\QueueRemoveItemOptions $options, $messageOptions = []) |
|
496 | |||
497 | /** |
||
498 | * Queue_MoveItem - move an item (a PNR) from one queue to another. |
||
499 | * |
||
500 | * @param RequestOptions\QueueMoveItemOptions $options |
||
501 | * @param array $messageOptions |
||
502 | * @return Result |
||
503 | */ |
||
504 | View Code Duplication | public function queueMoveItem(RequestOptions\QueueMoveItemOptions $options, $messageOptions = []) |
|
523 | |||
524 | /** |
||
525 | * Offer_VerifyOffer |
||
526 | * |
||
527 | * To be called in the context of an open PNR |
||
528 | * |
||
529 | * @param RequestOptions\OfferVerifyOptions $options |
||
530 | * @param array $messageOptions |
||
531 | * @return Result |
||
532 | */ |
||
533 | View Code Duplication | public function offerVerify(RequestOptions\OfferVerifyOptions $options, $messageOptions = []) |
|
552 | |||
553 | /** |
||
554 | * Offer_ConfirmAirOffer |
||
555 | * |
||
556 | * @param RequestOptions\OfferConfirmAirOptions $options |
||
557 | * @param array $messageOptions |
||
558 | * @return Result |
||
559 | */ |
||
560 | View Code Duplication | public function offerConfirmAir(RequestOptions\OfferConfirmAirOptions $options, $messageOptions = []) |
|
579 | |||
580 | /** |
||
581 | * Offer_ConfirmHotelOffer |
||
582 | * |
||
583 | * @param RequestOptions\OfferConfirmHotelOptions $options |
||
584 | * @param array $messageOptions |
||
585 | * @return Result |
||
586 | */ |
||
587 | View Code Duplication | public function offerConfirmHotel(RequestOptions\OfferConfirmHotelOptions $options, $messageOptions = []) |
|
606 | |||
607 | /** |
||
608 | * Offer_ConfirmCarOffer |
||
609 | * |
||
610 | * @param RequestOptions\OfferConfirmCarOptions $options |
||
611 | * @param array $messageOptions |
||
612 | * @return Result |
||
613 | */ |
||
614 | View Code Duplication | public function offerConfirmCar(RequestOptions\OfferConfirmCarOptions $options, $messageOptions = []) |
|
633 | |||
634 | /** |
||
635 | * Fare_MasterPricerTravelBoardSearch |
||
636 | * |
||
637 | * @param RequestOptions\FareMasterPricerTbSearch $options |
||
638 | * @param array $messageOptions |
||
639 | * @return Result |
||
640 | */ |
||
641 | View Code Duplication | public function fareMasterPricerTravelBoardSearch(RequestOptions\FareMasterPricerTbSearch $options, $messageOptions = []) |
|
660 | |||
661 | /** |
||
662 | * Fare_PricePnrWithBookingClass |
||
663 | * |
||
664 | * @param RequestOptions\FarePricePnrWithBookingClassOptions $options |
||
665 | * @param array $messageOptions |
||
666 | * @return Result |
||
667 | */ |
||
668 | View Code Duplication | public function farePricePnrWithBookingClass(RequestOptions\FarePricePnrWithBookingClassOptions $options, $messageOptions = []) |
|
687 | |||
688 | /** |
||
689 | * Fare_CheckRules |
||
690 | * |
||
691 | * @param RequestOptions\FareCheckRulesOptions $options |
||
692 | * @param array $messageOptions |
||
693 | * @return Result |
||
694 | */ |
||
695 | View Code Duplication | public function fareCheckRules(RequestOptions\FareCheckRulesOptions $options, $messageOptions = []) |
|
714 | |||
715 | /** |
||
716 | * Fare_ConvertCurrency |
||
717 | * |
||
718 | * @param RequestOptions\FareConvertCurrencyOptions $options |
||
719 | * @param array $messageOptions |
||
720 | * @return Result |
||
721 | */ |
||
722 | View Code Duplication | public function fareConvertCurrency(RequestOptions\FareConvertCurrencyOptions $options, $messageOptions = []) |
|
741 | |||
742 | /** |
||
743 | * Air_SellFromRecommendation |
||
744 | * |
||
745 | * @param RequestOptions\AirSellFromRecommendationOptions $options |
||
746 | * @param array $messageOptions |
||
747 | * @return Result |
||
748 | */ |
||
749 | View Code Duplication | public function airSellFromRecommendation(RequestOptions\AirSellFromRecommendationOptions $options, $messageOptions = []) |
|
768 | |||
769 | /** |
||
770 | * Air_FlightInfo |
||
771 | * |
||
772 | * @param RequestOptions\AirFlightInfoOptions $options |
||
773 | * @param array $messageOptions |
||
774 | * @return Result |
||
775 | */ |
||
776 | View Code Duplication | public function airFlightInfo(RequestOptions\AirFlightInfoOptions $options, $messageOptions = []) |
|
795 | |||
796 | /** |
||
797 | * Air_RetrieveSeatMap |
||
798 | * |
||
799 | * @param RequestOptions\AirRetrieveSeatMapOptions $options |
||
800 | * @param array $messageOptions |
||
801 | * @return Result |
||
802 | */ |
||
803 | View Code Duplication | public function airRetrieveSeatMap(RequestOptions\AirRetrieveSeatMapOptions $options, $messageOptions = []) |
|
822 | |||
823 | /** |
||
824 | * Command_Cryptic |
||
825 | * |
||
826 | * @param RequestOptions\CommandCrypticOptions $options |
||
827 | * @param array $messageOptions |
||
828 | * @return Result |
||
829 | */ |
||
830 | View Code Duplication | public function commandCryptic(RequestOptions\CommandCrypticOptions $options, $messageOptions = []) |
|
849 | |||
850 | /** |
||
851 | * MiniRule_GetFromPricingRec |
||
852 | * |
||
853 | * @param RequestOptions\MiniRuleGetFromPricingRecOptions $options |
||
854 | * @param array $messageOptions |
||
855 | * @return Result |
||
856 | */ |
||
857 | View Code Duplication | public function miniRuleGetFromPricingRec(RequestOptions\MiniRuleGetFromPricingRecOptions $options, $messageOptions = []) |
|
876 | |||
877 | /** |
||
878 | * Info_EncodeDecodeCity |
||
879 | * |
||
880 | * @param RequestOptions\InfoEncodeDecodeCityOptions $options |
||
881 | * @param array $messageOptions |
||
882 | * @return Result |
||
883 | */ |
||
884 | View Code Duplication | public function infoEncodeDecodeCity(RequestOptions\InfoEncodeDecodeCityOptions $options, $messageOptions = []) |
|
903 | |||
904 | |||
905 | /** |
||
906 | * Ticket_CreateTSTFromPricing |
||
907 | * |
||
908 | * @param RequestOptions\TicketCreateTstFromPricingOptions $options |
||
909 | * @param array $messageOptions |
||
910 | * @return Result |
||
911 | */ |
||
912 | View Code Duplication | public function ticketCreateTSTFromPricing(RequestOptions\TicketCreateTstFromPricingOptions $options, $messageOptions = []) |
|
931 | |||
932 | /** |
||
933 | * DocIssuance_IssueTicket |
||
934 | * |
||
935 | * @param RequestOptions\DocIssuanceIssueTicketOptions $options |
||
936 | * @param array $messageOptions |
||
937 | * @return Result |
||
938 | */ |
||
939 | View Code Duplication | public function docIssuanceIssueTicket(RequestOptions\DocIssuanceIssueTicketOptions $options, $messageOptions = []) |
|
958 | |||
959 | /** |
||
960 | * PriceXplorer_ExtremeSearch |
||
961 | * |
||
962 | * @param RequestOptions\PriceXplorerExtremeSearchOptions $options |
||
963 | * @param array $messageOptions |
||
964 | * @return Result |
||
965 | */ |
||
966 | View Code Duplication | public function priceXplorerExtremeSearch(RequestOptions\PriceXplorerExtremeSearchOptions $options, $messageOptions = []) |
|
985 | |||
986 | /** |
||
987 | * Make message options |
||
988 | * |
||
989 | * Message options are meta options when sending a message to the amadeus web services |
||
990 | * - (if stateful) should we end the current session after sending this call? |
||
991 | * - do you want the response as a PHP object or as a string? |
||
992 | * - ... ? |
||
993 | * |
||
994 | * @param array $incoming The Message options chosen by the caller - if any. |
||
995 | * @param bool $endSession Switch if you want to terminate the current session after making the call. |
||
996 | * @return array |
||
997 | */ |
||
998 | protected function makeMessageOptions(array $incoming, $endSession = false) |
||
1010 | |||
1011 | /** |
||
1012 | * Load the session handler |
||
1013 | * |
||
1014 | * Either load the provided session handler or create one depending on incoming parameters. |
||
1015 | * |
||
1016 | * @param HandlerInterface|null $sessionHandler |
||
1017 | * @param Params\SessionHandlerParams $params |
||
1018 | * @return HandlerInterface |
||
1019 | */ |
||
1020 | protected function loadSessionHandler($sessionHandler, $params) |
||
1032 | |||
1033 | /** |
||
1034 | * Load a request creator |
||
1035 | * |
||
1036 | * A request creator is responsible for generating the correct request to send. |
||
1037 | * |
||
1038 | * @param RequestCreatorInterface|null $requestCreator |
||
1039 | * @param Params\RequestCreatorParams $params |
||
1040 | * @param string $libIdentifier Library identifier & version string (for Received From) |
||
1041 | * @param string $originatorOffice The Office we are signed in with. |
||
1042 | * @param array $mesVer Messages & Versions array of active messages in the WSDL |
||
1043 | * @return RequestCreatorInterface |
||
1044 | * @throws \RuntimeException |
||
1045 | */ |
||
1046 | protected function loadRequestCreator($requestCreator, $params, $libIdentifier, $originatorOffice, $mesVer) |
||
1064 | |||
1065 | /** |
||
1066 | * Load a response handler |
||
1067 | * |
||
1068 | * @param ResponseHandlerInterface|null $responseHandler |
||
1069 | * |
||
1070 | * @return ResponseHandlerInterface |
||
1071 | * @throws \RuntimeException |
||
1072 | */ |
||
1073 | protected function loadResponseHandler($responseHandler) |
||
1085 | } |
||
1086 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.