Complex classes like ShopifyAPIComponent 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 ShopifyAPIComponent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class ShopifyAPIComponent extends Component |
||
| 26 | { |
||
| 27 | |||
| 28 | public $apiKey; |
||
| 29 | |||
| 30 | private $shopDomain; |
||
| 31 | private $token; |
||
| 32 | private $sharedSecret; |
||
| 33 | private $privateApp; |
||
| 34 | private $privateAppPassword; |
||
| 35 | private $nonce; |
||
| 36 | |||
| 37 | public $controller = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param array $config |
||
|
|
|||
| 41 | * @return void |
||
| 42 | */ |
||
| 43 | 12 | public function initialize(array $config = []) |
|
| 44 | { |
||
| 45 | 12 | parent::initialize($config); |
|
| 46 | |||
| 47 | 12 | $this->apiKey = isset($config['apiKey']) ? $config['apiKey'] : ''; |
|
| 48 | |||
| 49 | 12 | if (!empty($this->apiKey)) { |
|
| 50 | 12 | $this->sharedSecret = Configure::read('Multidimensional/Cakephpify.' . $this->apiKey . '.sharedSecret'); |
|
| 51 | 12 | $this->scope = Configure::read('Multidimensional/Cakephpify.' . $this->apiKey . '.scope'); |
|
| 52 | 12 | $this->privateApp = Configure::read('Multidimensional/Cakephpify.' . $this->apiKey . '.privateApp'); |
|
| 53 | 12 | $this->privateAppPassword = Configure::read('Multidimensional/Cakephpify.' . $this->apiKey . '.privateAppPassword'); |
|
| 54 | 12 | } else { |
|
| 55 | throw new NotImplementedException(__('Shopify API key not found')); |
||
| 56 | } |
||
| 57 | |||
| 58 | 12 | if (!$this->sharedSecret) { |
|
| 59 | throw new NotImplementedException(__('Shopify Shared Secret not found')); |
||
| 60 | } |
||
| 61 | 12 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param Event $event |
||
| 65 | * @return void |
||
| 66 | */ |
||
| 67 | 12 | public function startup(Event $event) |
|
| 68 | { |
||
| 69 | 12 | $this->setController($event->subject()); |
|
| 70 | 12 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param controller $controller |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | 12 | public function setController($controller) |
|
| 77 | { |
||
| 78 | 12 | $this->controller = $controller; |
|
| 79 | 12 | if (!isset($this->controller->paginate)) { |
|
| 80 | $this->controller->paginate = []; |
||
| 81 | } |
||
| 82 | 12 | } |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $shopDomain |
||
| 86 | * @return string|null |
||
| 87 | */ |
||
| 88 | 6 | public function setShopDomain($shopDomain) |
|
| 89 | { |
||
| 90 | 6 | return $this->shopDomain = $shopDomain; |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string|null |
||
| 95 | */ |
||
| 96 | 6 | public function getShopDomain() |
|
| 97 | { |
||
| 98 | 6 | return $this->shopDomain; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $token |
||
| 103 | * @return string|null |
||
| 104 | */ |
||
| 105 | public function setAccessToken($token) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return int|null |
||
| 112 | */ |
||
| 113 | public function callsMade() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return int|null |
||
| 120 | */ |
||
| 121 | public function callLimit() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param Response $responseHeaders |
||
| 128 | * @return int|null |
||
| 129 | */ |
||
| 130 | public function callsLeft($responseHeaders) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $method |
||
| 137 | * @param string $path |
||
| 138 | * @param array $params |
||
| 139 | * @return array|null |
||
| 140 | */ |
||
| 141 | public function call($method, $path, $params = []) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param int $index |
||
| 172 | * @return int |
||
| 173 | */ |
||
| 174 | private function shopApiCallLimitParam($index) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $shopDomain |
||
| 183 | * @param string $redirectUrl |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getAuthorizeUrl($shopDomain, $redirectUrl) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $shopDomain |
||
| 198 | * @param string $code |
||
| 199 | * @return string|bool |
||
| 200 | */ |
||
| 201 | public function getAccessToken($shopDomain, $code) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $shopDomain |
||
| 232 | * @return string|null |
||
| 233 | */ |
||
| 234 | 6 | public function setNonce($shopDomain) |
|
| 235 | { |
||
| 236 | 6 | return $this->nonce = md5(strtolower($shopDomain)); |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return string|null |
||
| 241 | */ |
||
| 242 | public function getNonce() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $shopDomain |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | 6 | public function validDomain($shopDomain) |
|
| 252 | { |
||
| 253 | 6 | return true; |
|
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return json |
||
| 258 | */ |
||
| 259 | public function getShopData() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param array $query |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function validateHMAC($query) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $url |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | private function _urlEncode($url) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | private function _isReady() |
||
| 309 | } |
||
| 310 |