Complex classes like PaymentService 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 PaymentService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class PaymentService extends Object{ |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var Guzzle\Http\ClientInterface |
||
| 23 | */ |
||
| 24 | private static $httpclient; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Guzzle\Http\Message\Request |
||
| 28 | */ |
||
| 29 | private static $httprequest; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Payment |
||
| 33 | */ |
||
| 34 | protected $payment; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var String |
||
| 38 | */ |
||
| 39 | protected $returnurl; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var String |
||
| 43 | */ |
||
| 44 | protected $cancelurl; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Guzzle\Http\Message\Response |
||
| 48 | */ |
||
| 49 | protected $response; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var GatewayFactory |
||
| 53 | */ |
||
| 54 | protected $gatewayFactory; |
||
| 55 | |||
| 56 | private static $dependencies = array( |
||
| 57 | 'gatewayFactory' => '%$\Omnipay\Common\GatewayFactory', |
||
| 58 | ); |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * @param Payment |
||
| 63 | */ |
||
| 64 | public function __construct(Payment $payment) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get the url to return to, that has been previously stored. |
||
| 71 | * This is not a database field. |
||
| 72 | * @return string the url |
||
| 73 | */ |
||
| 74 | public function getReturnUrl() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Set the url to redirect to after payment is made/attempted. |
||
| 80 | * This function also populates the cancel url, if it is empty. |
||
| 81 | * @return PaymentService this object for chaining |
||
| 82 | */ |
||
| 83 | public function setReturnUrl($url) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return string cancel url |
||
| 94 | */ |
||
| 95 | public function getCancelUrl() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set the url to redirect to after payment is cancelled |
||
| 101 | * @return PaymentService this object for chaining |
||
| 102 | */ |
||
| 103 | public function setCancelUrl($url) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the appropriate redirect url |
||
| 111 | */ |
||
| 112 | public function getRedirectURL() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Update class properties via array. |
||
| 126 | */ |
||
| 127 | public function update($data) { |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Get the omnipay gateway associated with this payment, |
||
| 139 | * with configuration applied. |
||
| 140 | * |
||
| 141 | * @throws RuntimeException - when gateway doesn't exist. |
||
| 142 | * @return AbstractGateway omnipay gateway class |
||
| 143 | */ |
||
| 144 | public function oGateway() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Generate a return/notify url for off-site gateways (completePayment). |
||
| 161 | * @return string endpoint url |
||
| 162 | */ |
||
| 163 | protected function getEndpointURL($action, $identifier) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Record a transaction on this for this payment. |
||
| 169 | * @param string $type the type of transaction to create. |
||
| 170 | * This is any class that is (or extends) PaymentMessage. |
||
| 171 | * @param array|string|AbstractResponse|AbstractRequest|OmnipayException $data the response to record, or data to store |
||
| 172 | * @return GatewayTransaction newly created dataobject, saved to database. |
||
| 173 | */ |
||
| 174 | protected function createMessage($type, $data = null) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Helper function for logging gateway requests |
||
| 226 | */ |
||
| 227 | protected function logToFile($data, $type = "") { |
||
| 244 | |||
| 245 | protected function createGatewayResponse() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return GatewayFactory |
||
| 253 | */ |
||
| 254 | public function getGatewayFactory() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param GatewayFactory $gatewayFactory |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function setGatewayFactory($gatewayFactory) { |
||
| 271 | |||
| 272 | //testing functions (could these instead be injected somehow?) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Set the guzzle client (for testing) |
||
| 276 | * @param Guzzle\Http\ClientInterface $httpClient guzzle client for testing |
||
| 277 | */ |
||
| 278 | public static function setHttpClient(Guzzle\Http\ClientInterface $httpClient) |
||
| 282 | |||
| 283 | public static function getHttpClient() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set the symphony http request (for testing) |
||
| 290 | * @param Symfony\Component\HttpFoundation\Request $httpRequest symphony http request for testing |
||
| 291 | */ |
||
| 292 | public static function setHttpRequest(Symfony\Component\HttpFoundation\Request $httpRequest) |
||
| 296 | |||
| 297 | public static function getHttpRequest() |
||
| 301 | |||
| 302 | // ----------------------------------------------------------------------------------------------------------------- |
||
| 303 | // Deprecated methods. |
||
| 304 | // TODO: Remove with 3.0 |
||
| 305 | // ----------------------------------------------------------------------------------------------------------------- |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Set the guzzle client (for testing) |
||
| 309 | * @param Guzzle\Http\ClientInterface $httpClient guzzle client for testing |
||
| 310 | * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use setHttpClient |
||
| 311 | */ |
||
| 312 | public static function set_http_client(Guzzle\Http\ClientInterface $httpClient) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use getHttpClient |
||
| 319 | */ |
||
| 320 | public static function get_http_client() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set the symphony http request (for testing) |
||
| 327 | * @param Symfony\Component\HttpFoundation\Request $httpRequest symphony http request for testing |
||
| 328 | * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use setHttpRequest |
||
| 329 | */ |
||
| 330 | public static function set_http_request(Symfony\Component\HttpFoundation\Request $httpRequest) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use getHttpRequest |
||
| 337 | */ |
||
| 338 | public static function get_http_request() { |
||
| 342 | |||
| 343 | } |
||
| 344 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.