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 Base 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 Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | abstract class Base implements HandlerInterface, LoggerAwareInterface |
||
| 45 | { |
||
| 46 | use LoggerAwareTrait; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * XPATH query to retrieve all operations from a WSDL |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | const XPATH_ALL_OPERATIONS = '/wsdl:definitions/wsdl:portType/wsdl:operation/@name'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * XPATH query to retrieve WSDL imports from a WSDL. |
||
| 57 | */ |
||
| 58 | const XPATH_IMPORTS = '/wsdl:definitions/wsdl:import/@location'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * XPATH query to retrieve the full operation name + version from a WSDL for a given operation. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | const XPATH_VERSION_FOR_OPERATION = "string(/wsdl:definitions/wsdl:message[contains(./@name, '%s_')]/@name)"; |
||
| 66 | |||
| 67 | const XPATH_ALT_VERSION_FOR_OPERATION = "string(//wsdl:operation[contains(./@name, '%s')]/wsdl:input/@message)"; |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * Status variable to know wether the given session is in a certain context. |
||
| 72 | * |
||
| 73 | * @todo implement this feature - currently the application using the client must know the context itself. |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $hasContext = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The context of the currently active session |
||
| 80 | * |
||
| 81 | * @todo implement this feature - currently the application using the client must know the context itself. |
||
| 82 | * @var mixed |
||
| 83 | */ |
||
| 84 | protected $context; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Session information: |
||
| 88 | * - session ID |
||
| 89 | * - sequence number |
||
| 90 | * - security Token |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | protected $sessionData = [ |
||
| 95 | 'sessionId' => null, |
||
| 96 | 'sequenceNumber' => null, |
||
| 97 | 'securityToken' => null |
||
| 98 | ]; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Status variable to know if the session is currently logged in |
||
| 102 | * |
||
| 103 | * @var bool |
||
| 104 | */ |
||
| 105 | protected $isAuthenticated = false; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var array[string]\SoapClient |
||
| 109 | */ |
||
| 110 | protected $soapClients = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Default SoapClient options used during initialisation |
||
| 114 | * |
||
| 115 | * Can be overridden by providing custom options in |
||
| 116 | * Amadeus\Client\Params\SessionHandlerParams::$soapClientOptions |
||
| 117 | * |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | protected $soapClientOptions = [ |
||
| 121 | 'trace' => 1, |
||
| 122 | 'exceptions' => 1, |
||
| 123 | 'soap_version' => SOAP_1_1 |
||
| 124 | ]; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var SessionHandlerParams |
||
| 128 | */ |
||
| 129 | protected $params; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Dom Document array where the WSDL's contents will be loaded |
||
| 133 | * |
||
| 134 | * format: |
||
| 135 | * [ |
||
| 136 | * '7d36c7b8' => \DOMDocument, |
||
| 137 | * '7e84f2537' => \DOMDocument |
||
| 138 | * ] |
||
| 139 | * |
||
| 140 | * @var array |
||
| 141 | */ |
||
| 142 | protected $wsdlDomDoc; |
||
| 143 | /** |
||
| 144 | * To query the WSDL contents |
||
| 145 | * |
||
| 146 | * format: |
||
| 147 | * [ |
||
| 148 | * '7d36c7b8' => \DOMXpath, |
||
| 149 | * '7e84f2537' => \DOMXpath |
||
| 150 | * ] |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | protected $wsdlDomXpath; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * A map of which messages are available in the provided WSDL's |
||
| 158 | * |
||
| 159 | * format: |
||
| 160 | * [ |
||
| 161 | * 'PNR_Retrieve' => [ |
||
| 162 | * 'version' => '14.1', |
||
| 163 | * 'wsdl' => '7d36c7b8' |
||
| 164 | * ], |
||
| 165 | * 'Media_GetMedia' => [ |
||
| 166 | * 'version' => '14.1', |
||
| 167 | * 'wsdl' => '7e84f2537' |
||
| 168 | * ] |
||
| 169 | * ] |
||
| 170 | * |
||
| 171 | * @var array |
||
| 172 | */ |
||
| 173 | protected $messagesAndVersions = []; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * List of WSDL ID's and their path |
||
| 177 | * |
||
| 178 | * format: |
||
| 179 | * [ |
||
| 180 | * '7d36c7b8' => '/path/to/wsdl.wsdl' |
||
| 181 | * ] |
||
| 182 | * |
||
| 183 | * @var array |
||
| 184 | */ |
||
| 185 | protected $wsdlIds = []; |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the session parameters of the active session |
||
| 190 | * |
||
| 191 | * @return array|null |
||
| 192 | */ |
||
| 193 | public function getSessionData() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Set the session data to continue a previously started session. |
||
| 200 | * |
||
| 201 | * @param array $sessionData |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function setSessionData(array $sessionData) |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * @param SessionHandlerParams $params |
||
| 221 | */ |
||
| 222 | public function __construct(SessionHandlerParams $params) |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $messageName Method Operation name as defined in the WSDL. |
||
| 238 | * @param BaseWsMessage $messageBody |
||
| 239 | * @param array $messageOptions options: bool 'asString', bool 'endSession' |
||
| 240 | * @return SendResult |
||
| 241 | * @throws \InvalidArgumentException |
||
| 242 | * @throws Client\Exception |
||
| 243 | * @throws \SoapFault |
||
| 244 | */ |
||
| 245 | public function sendMessage($messageName, Client\Struct\BaseWsMessage $messageBody, $messageOptions = []) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Prepare to send a next message and checks if authenticated |
||
| 288 | * |
||
| 289 | * @param string $messageName |
||
| 290 | * @param array $messageOptions |
||
| 291 | */ |
||
| 292 | protected abstract function prepareForNextMessage($messageName, $messageOptions); |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Handles post message actions |
||
| 296 | * |
||
| 297 | * Handles session state based on received response |
||
| 298 | * |
||
| 299 | * @param string $messageName |
||
| 300 | * @param string $lastResponse |
||
| 301 | * @param array $messageOptions |
||
| 302 | * @param mixed $result |
||
| 303 | */ |
||
| 304 | protected abstract function handlePostMessage($messageName, $lastResponse, $messageOptions, $result); |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get the last raw XML message that was sent out |
||
| 308 | * |
||
| 309 | * @param string $msgName |
||
| 310 | * @return string|null |
||
| 311 | */ |
||
| 312 | public function getLastRequest($msgName) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the last raw XML message that was received |
||
| 326 | * |
||
| 327 | * @param string $msgName |
||
| 328 | * @return string|null |
||
| 329 | */ |
||
| 330 | public function getLastResponse($msgName) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get the office that we are using to sign in to. |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getOriginatorOffice() |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * Extract the Messages and versions from the loaded WSDL file. |
||
| 355 | * |
||
| 356 | * Result is an associative array: keys are message names, values are versions. |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function getMessagesAndVersions() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Loads messages & versions from WSDL. |
||
| 371 | * |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | protected function loadMessagesAndVersions() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Generates a unique identifier for a wsdl based on its path. |
||
| 429 | * |
||
| 430 | * @param string $wsdlPath |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | protected function makeWsdlIdentifier($wsdlPath) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * extractMessageVersion |
||
| 441 | * |
||
| 442 | * extracts "4.1" from a string like "Security_SignOut_4_1" |
||
| 443 | * or "1.00" from a string like "tns:AMA_MediaGetMediaRQ_1.000" |
||
| 444 | * |
||
| 445 | * @param string $fullVersionString |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | protected function extractMessageVersion($fullVersionString) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Load the WSDL contents to a queryable DOMXpath. |
||
| 459 | * |
||
| 460 | * @param string $wsdlFilePath |
||
| 461 | * @param string $wsdlId |
||
| 462 | * @uses $this->wsdlDomDoc |
||
| 463 | * @uses $this->wsdlDomXpath |
||
| 464 | */ |
||
| 465 | protected function loadWsdlXpath($wsdlFilePath, $wsdlId) |
||
| 487 | |||
| 488 | |||
| 489 | |||
| 490 | /** |
||
| 491 | * Get the version number active in the WSDL for the given message |
||
| 492 | * |
||
| 493 | * @param $messageName |
||
| 494 | * @return float|string|null |
||
| 495 | */ |
||
| 496 | protected function getActiveVersionFor($messageName) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Get the WSDL ID for the given message |
||
| 511 | * |
||
| 512 | * @param $messageName |
||
| 513 | * @return string|null |
||
| 514 | */ |
||
| 515 | protected function getWsdlIdFor($messageName) |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * @param string $msgName |
||
| 529 | * @return \SoapClient |
||
| 530 | */ |
||
| 531 | protected function getSoapClient($msgName) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param string $wsdlId |
||
| 548 | * @return \SoapClient |
||
| 549 | */ |
||
| 550 | protected abstract function initSoapClient($wsdlId); |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get Messages & Versions from an imported WSDL file |
||
| 554 | * |
||
| 555 | * Imported wsdl's are a little different, they require a different query |
||
| 556 | * to extract the version nrs. |
||
| 557 | * |
||
| 558 | * @param string $import |
||
| 559 | * @param string $wsdlPath |
||
| 560 | * @param string $wsdlIdentifier |
||
| 561 | * @return array |
||
| 562 | * @throws Client\Exception when the WSDL import could not be loaded. |
||
| 563 | */ |
||
| 564 | protected function getMessagesAndVersionsFromImportedWsdl($import, $wsdlPath, $wsdlIdentifier) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @param string $messageName |
||
| 617 | * @uses $this->log |
||
| 618 | */ |
||
| 619 | protected function logRequestAndResponse($messageName) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @param mixed $level |
||
| 633 | * @param string $message |
||
| 634 | * @param array $context |
||
| 635 | * @return null |
||
| 636 | */ |
||
| 637 | protected function log($level, $message, $context = []) |
||
| 645 | } |
||
| 646 |