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 | public function pnrRetrieve(RequestOptions\PnrRetrieveOptions $options, $messageOptions = []) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Create a PNR using PNR_AddMultiElements |
||
| 282 | * |
||
| 283 | * @param RequestOptions\PnrCreatePnrOptions $options |
||
| 284 | * @param array $messageOptions |
||
| 285 | * @return Result |
||
| 286 | */ |
||
| 287 | public function pnrCreatePnr(RequestOptions\PnrCreatePnrOptions $options, $messageOptions = []) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * PNR_AddMultiElements - Create a new PNR or update an existing PNR. |
||
| 296 | * |
||
| 297 | * https://webservices.amadeus.com/extranet/viewService.do?id=25&flavourId=1&menuId=functional |
||
| 298 | * |
||
| 299 | * @todo implement message creation - maybe split up in separate Create & Modify PNR? |
||
| 300 | * @param RequestOptions\PnrAddMultiElementsOptions $options |
||
| 301 | * @param array $messageOptions |
||
| 302 | * @return Result |
||
| 303 | */ |
||
| 304 | public function pnrAddMultiElements(RequestOptions\PnrAddMultiElementsOptions $options, $messageOptions = []) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * PNR_RetrieveAndDisplay - Retrieve an Amadeus PNR by record locator including extra info |
||
| 313 | * |
||
| 314 | * This extra info is info you cannot see in the regular PNR, like Offers. |
||
| 315 | * |
||
| 316 | * By default, the result will be the PNR_RetrieveAndDisplayReply XML as string. |
||
| 317 | * That way you can easily parse the PNR's contents with XPath. |
||
| 318 | * |
||
| 319 | * Set $messageOptions['asString'] = FALSE to get the response as a PHP object. |
||
| 320 | * |
||
| 321 | * https://webservices.amadeus.com/extranet/viewService.do?id=1922&flavourId=1&menuId=functional |
||
| 322 | * |
||
| 323 | * @param RequestOptions\PnrRetrieveAndDisplayOptions $options Amadeus Record Locator for PNR |
||
| 324 | * @param array $messageOptions (OPTIONAL) Set ['asString'] = 'false' to get PNR_RetrieveAndDisplayReply as a PHP object. |
||
| 325 | * @return Result |
||
| 326 | * @throws Exception |
||
| 327 | **/ |
||
| 328 | public function pnrRetrieveAndDisplay(RequestOptions\PnrRetrieveAndDisplayOptions $options, $messageOptions = []) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * PNR_Cancel |
||
| 337 | * |
||
| 338 | * @param RequestOptions\PnrCancelOptions $options |
||
| 339 | * @param array $messageOptions |
||
| 340 | * @return Result |
||
| 341 | */ |
||
| 342 | public function pnrCancel(RequestOptions\PnrCancelOptions $options, $messageOptions = []) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * PNR_DisplayHistory |
||
| 351 | * |
||
| 352 | * @param RequestOptions\PnrDisplayHistoryOptions $options |
||
| 353 | * @param array $messageOptions |
||
| 354 | * @return Result |
||
| 355 | */ |
||
| 356 | public function pnrDisplayHistory(RequestOptions\PnrDisplayHistoryOptions $options, $messageOptions = []) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Queue_List - get a list of all PNR's on a given queue |
||
| 365 | * |
||
| 366 | * https://webservices.amadeus.com/extranet/viewService.do?id=52&flavourId=1&menuId=functional |
||
| 367 | * |
||
| 368 | * @param RequestOptions\QueueListOptions $options |
||
| 369 | * @param array $messageOptions |
||
| 370 | * @return Result |
||
| 371 | */ |
||
| 372 | public function queueList(RequestOptions\QueueListOptions $options, $messageOptions = []) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Queue_PlacePNR - Place a PNR on a given queue |
||
| 381 | * |
||
| 382 | * @param RequestOptions\QueuePlacePnrOptions $options |
||
| 383 | * @param array $messageOptions |
||
| 384 | * @return Result |
||
| 385 | */ |
||
| 386 | public function queuePlacePnr(RequestOptions\QueuePlacePnrOptions $options, $messageOptions = []) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Queue_RemoveItem - remove an item (a PNR) from a given queue |
||
| 395 | * |
||
| 396 | * @param RequestOptions\QueueRemoveItemOptions $options |
||
| 397 | * @param array $messageOptions |
||
| 398 | * @return Result |
||
| 399 | */ |
||
| 400 | public function queueRemoveItem(RequestOptions\QueueRemoveItemOptions $options, $messageOptions = []) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Queue_MoveItem - move an item (a PNR) from one queue to another. |
||
| 409 | * |
||
| 410 | * @param RequestOptions\QueueMoveItemOptions $options |
||
| 411 | * @param array $messageOptions |
||
| 412 | * @return Result |
||
| 413 | */ |
||
| 414 | public function queueMoveItem(RequestOptions\QueueMoveItemOptions $options, $messageOptions = []) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Offer_VerifyOffer |
||
| 423 | * |
||
| 424 | * To be called in the context of an open PNR |
||
| 425 | * |
||
| 426 | * @param RequestOptions\OfferVerifyOptions $options |
||
| 427 | * @param array $messageOptions |
||
| 428 | * @return Result |
||
| 429 | */ |
||
| 430 | public function offerVerify(RequestOptions\OfferVerifyOptions $options, $messageOptions = []) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Offer_ConfirmAirOffer |
||
| 439 | * |
||
| 440 | * @param RequestOptions\OfferConfirmAirOptions $options |
||
| 441 | * @param array $messageOptions |
||
| 442 | * @return Result |
||
| 443 | */ |
||
| 444 | public function offerConfirmAir(RequestOptions\OfferConfirmAirOptions $options, $messageOptions = []) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Offer_ConfirmHotelOffer |
||
| 453 | * |
||
| 454 | * @param RequestOptions\OfferConfirmHotelOptions $options |
||
| 455 | * @param array $messageOptions |
||
| 456 | * @return Result |
||
| 457 | */ |
||
| 458 | public function offerConfirmHotel(RequestOptions\OfferConfirmHotelOptions $options, $messageOptions = []) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Offer_ConfirmCarOffer |
||
| 467 | * |
||
| 468 | * @param RequestOptions\OfferConfirmCarOptions $options |
||
| 469 | * @param array $messageOptions |
||
| 470 | * @return Result |
||
| 471 | */ |
||
| 472 | public function offerConfirmCar(RequestOptions\OfferConfirmCarOptions $options, $messageOptions = []) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Fare_MasterPricerTravelBoardSearch |
||
| 481 | * |
||
| 482 | * @param RequestOptions\FareMasterPricerTbSearch $options |
||
| 483 | * @param array $messageOptions |
||
| 484 | * @return Result |
||
| 485 | */ |
||
| 486 | public function fareMasterPricerTravelBoardSearch(RequestOptions\FareMasterPricerTbSearch $options, $messageOptions = []) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Fare_PricePnrWithBookingClass |
||
| 495 | * |
||
| 496 | * @param RequestOptions\FarePricePnrWithBookingClassOptions $options |
||
| 497 | * @param array $messageOptions |
||
| 498 | * @return Result |
||
| 499 | */ |
||
| 500 | public function farePricePnrWithBookingClass(RequestOptions\FarePricePnrWithBookingClassOptions $options, $messageOptions = []) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Fare_InformativePricingWithoutPNR |
||
| 509 | * |
||
| 510 | * @param RequestOptions\FareInformativePricingWithoutPnrOptions $options |
||
| 511 | * @param array $messageOptions |
||
| 512 | * @return Result |
||
| 513 | */ |
||
| 514 | public function fareInformativePricingWithoutPnr(RequestOptions\FareInformativePricingWithoutPnrOptions $options, $messageOptions = []) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Fare_CheckRules |
||
| 523 | * |
||
| 524 | * @param RequestOptions\FareCheckRulesOptions $options |
||
| 525 | * @param array $messageOptions |
||
| 526 | * @return Result |
||
| 527 | */ |
||
| 528 | public function fareCheckRules(RequestOptions\FareCheckRulesOptions $options, $messageOptions = []) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Fare_ConvertCurrency |
||
| 537 | * |
||
| 538 | * @param RequestOptions\FareConvertCurrencyOptions $options |
||
| 539 | * @param array $messageOptions |
||
| 540 | * @return Result |
||
| 541 | */ |
||
| 542 | public function fareConvertCurrency(RequestOptions\FareConvertCurrencyOptions $options, $messageOptions = []) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Air_SellFromRecommendation |
||
| 551 | * |
||
| 552 | * @param RequestOptions\AirSellFromRecommendationOptions $options |
||
| 553 | * @param array $messageOptions |
||
| 554 | * @return Result |
||
| 555 | */ |
||
| 556 | public function airSellFromRecommendation(RequestOptions\AirSellFromRecommendationOptions $options, $messageOptions = []) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Air_FlightInfo |
||
| 565 | * |
||
| 566 | * @param RequestOptions\AirFlightInfoOptions $options |
||
| 567 | * @param array $messageOptions |
||
| 568 | * @return Result |
||
| 569 | */ |
||
| 570 | public function airFlightInfo(RequestOptions\AirFlightInfoOptions $options, $messageOptions = []) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Air_RetrieveSeatMap |
||
| 579 | * |
||
| 580 | * @param RequestOptions\AirRetrieveSeatMapOptions $options |
||
| 581 | * @param array $messageOptions |
||
| 582 | * @return Result |
||
| 583 | */ |
||
| 584 | public function airRetrieveSeatMap(RequestOptions\AirRetrieveSeatMapOptions $options, $messageOptions = []) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Command_Cryptic |
||
| 593 | * |
||
| 594 | * @param RequestOptions\CommandCrypticOptions $options |
||
| 595 | * @param array $messageOptions |
||
| 596 | * @return Result |
||
| 597 | */ |
||
| 598 | public function commandCryptic(RequestOptions\CommandCrypticOptions $options, $messageOptions = []) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * MiniRule_GetFromPricingRec |
||
| 607 | * |
||
| 608 | * @param RequestOptions\MiniRuleGetFromPricingRecOptions $options |
||
| 609 | * @param array $messageOptions |
||
| 610 | * @return Result |
||
| 611 | */ |
||
| 612 | public function miniRuleGetFromPricingRec(RequestOptions\MiniRuleGetFromPricingRecOptions $options, $messageOptions = []) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Info_EncodeDecodeCity |
||
| 621 | * |
||
| 622 | * @param RequestOptions\InfoEncodeDecodeCityOptions $options |
||
| 623 | * @param array $messageOptions |
||
| 624 | * @return Result |
||
| 625 | */ |
||
| 626 | public function infoEncodeDecodeCity(RequestOptions\InfoEncodeDecodeCityOptions $options, $messageOptions = []) |
||
| 632 | |||
| 633 | |||
| 634 | /** |
||
| 635 | * Ticket_CreateTSTFromPricing |
||
| 636 | * |
||
| 637 | * @param RequestOptions\TicketCreateTstFromPricingOptions $options |
||
| 638 | * @param array $messageOptions |
||
| 639 | * @return Result |
||
| 640 | */ |
||
| 641 | public function ticketCreateTSTFromPricing(RequestOptions\TicketCreateTstFromPricingOptions $options, $messageOptions = []) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * DocIssuance_IssueTicket |
||
| 650 | * |
||
| 651 | * @param RequestOptions\DocIssuanceIssueTicketOptions $options |
||
| 652 | * @param array $messageOptions |
||
| 653 | * @return Result |
||
| 654 | */ |
||
| 655 | public function docIssuanceIssueTicket(RequestOptions\DocIssuanceIssueTicketOptions $options, $messageOptions = []) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * PriceXplorer_ExtremeSearch |
||
| 664 | * |
||
| 665 | * @param RequestOptions\PriceXplorerExtremeSearchOptions $options |
||
| 666 | * @param array $messageOptions |
||
| 667 | * @return Result |
||
| 668 | */ |
||
| 669 | public function priceXplorerExtremeSearch(RequestOptions\PriceXplorerExtremeSearchOptions $options, $messageOptions = []) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Call a message with the given parameters |
||
| 678 | * |
||
| 679 | * @param string $messageName |
||
| 680 | * @param RequestOptions\RequestOptionsInterface $options |
||
| 681 | * @param array $messageOptions |
||
| 682 | * @return Result |
||
| 683 | * @throws Client\Exception |
||
| 684 | * @throws Client\Struct\InvalidArgumentException |
||
| 685 | * @throws Client\InvalidMessageException |
||
| 686 | * @throws Client\RequestCreator\MessageVersionUnsupportedException |
||
| 687 | * @throws \RuntimeException |
||
| 688 | * @throws \InvalidArgumentException |
||
| 689 | * @throws \SoapFault |
||
| 690 | */ |
||
| 691 | protected function callMessage($messageName, $options, $messageOptions) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Make message options |
||
| 712 | * |
||
| 713 | * Message options are meta options when sending a message to the amadeus web services |
||
| 714 | * - (if stateful) should we end the current session after sending this call? |
||
| 715 | * - do you want the response as a PHP object or as a string? |
||
| 716 | * - ... ? |
||
| 717 | * |
||
| 718 | * @param array $incoming The Message options chosen by the caller - if any. |
||
| 719 | * @param bool $endSession Switch if you want to terminate the current session after making the call. |
||
| 720 | * @return array |
||
| 721 | */ |
||
| 722 | protected function makeMessageOptions(array $incoming, $endSession = false) |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Load the session handler |
||
| 737 | * |
||
| 738 | * Either load the provided session handler or create one depending on incoming parameters. |
||
| 739 | * |
||
| 740 | * @param HandlerInterface|null $sessionHandler |
||
| 741 | * @param Params\SessionHandlerParams $params |
||
| 742 | * @return HandlerInterface |
||
| 743 | */ |
||
| 744 | protected function loadSessionHandler($sessionHandler, $params) |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Load a request creator |
||
| 759 | * |
||
| 760 | * A request creator is responsible for generating the correct request to send. |
||
| 761 | * |
||
| 762 | * @param RequestCreatorInterface|null $requestCreator |
||
| 763 | * @param Params\RequestCreatorParams $params |
||
| 764 | * @param string $libIdentifier Library identifier & version string (for Received From) |
||
| 765 | * @param string $originatorOffice The Office we are signed in with. |
||
| 766 | * @param array $mesVer Messages & Versions array of active messages in the WSDL |
||
| 767 | * @return RequestCreatorInterface |
||
| 768 | * @throws \RuntimeException |
||
| 769 | */ |
||
| 770 | protected function loadRequestCreator($requestCreator, $params, $libIdentifier, $originatorOffice, $mesVer) |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Load a response handler |
||
| 791 | * |
||
| 792 | * @param ResponseHandlerInterface|null $responseHandler |
||
| 793 | * |
||
| 794 | * @return ResponseHandlerInterface |
||
| 795 | * @throws \RuntimeException |
||
| 796 | */ |
||
| 797 | protected function loadResponseHandler($responseHandler) |
||
| 809 | } |
||
| 810 |
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.