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 | 24 | public function initialize(array $config = []) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param Event $event |
||
| 65 | * @return void |
||
| 66 | */ |
||
| 67 | 24 | public function startup(Event $event) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param controller $controller |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | 24 | public function setController($controller) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $shopDomain |
||
| 83 | * @return string|null |
||
| 84 | */ |
||
| 85 | 6 | public function setShopDomain($shopDomain) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @return string|null |
||
| 92 | */ |
||
| 93 | 6 | public function getShopDomain() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $shopDomain |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | 6 | public function validDomain($shopDomain) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $token |
||
| 109 | * @return string|null |
||
| 110 | */ |
||
| 111 | 6 | public function setAccessToken($token) |
|
| 112 | { |
||
| 113 | 6 | return $this->token = $token; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return string|null |
||
| 118 | */ |
||
| 119 | 6 | public function getAccessToken() |
|
| 120 | { |
||
| 121 | 6 | return $this->token; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return int|null |
||
| 126 | */ |
||
| 127 | public function callsMade() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return int|null |
||
| 134 | */ |
||
| 135 | public function callLimit() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param Response $responseHeaders |
||
| 142 | * @return int|null |
||
| 143 | */ |
||
| 144 | public function callsLeft($responseHeaders) |
||
| 145 | { |
||
| 146 | return $this->callLimit() - $this->callsMade(); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $method |
||
| 151 | * @param string $path |
||
| 152 | * @param array $params |
||
| 153 | * @return array|null |
||
| 154 | */ |
||
| 155 | public function call($method, $path, $params = []) |
||
| 156 | { |
||
| 157 | if (!$this->_isReady()) { |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | if (!in_array($method, ['POST', 'PUT', 'GET', 'DELETE'])) { |
||
| 162 | return false; |
||
| 163 | } |
||
| 164 | |||
| 165 | $http = new Client( |
||
| 166 | [ |
||
| 167 | 'host' => $this->shopDomain, |
||
| 168 | 'scheme' => 'https', |
||
| 169 | 'headers' => (($this->privateApp != 'true') ? (['X-Shopify-Access-Token' => $this->token]) : []), |
||
| 170 | 'auth' => (($this->privateApp != 'true') ? [] : (['username' => $this->apiKey, 'password' => $this->privateAppPassword])) |
||
| 171 | ] |
||
| 172 | ); |
||
| 173 | |||
| 174 | $this->response = $http->{strtolower($method)}( |
||
| 175 | $path, |
||
| 176 | ((in_array($method, ['POST', 'PUT'])) ? json_encode($params) : $params), |
||
| 177 | ((in_array($method, ['POST', 'PUT'])) ? ['type' => 'json'] : []) |
||
| 178 | ); |
||
| 179 | $this->response = $this->response->json; |
||
| 180 | |||
| 181 | return (is_array($this->response) && (count($this->response) > 0)) ? array_shift($this->response) : $this->response; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param int $index |
||
| 186 | * @return int |
||
| 187 | */ |
||
| 188 | private function shopApiCallLimitParam($index) |
||
| 189 | { |
||
| 190 | $params = explode("/", $this->response->getHeaderLine('http_x_shopify_shop_api_call_limit')); |
||
| 191 | |||
| 192 | return (int)$params[$index]; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $shopDomain |
||
| 197 | * @param string $redirectUrl |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getAuthorizeUrl($shopDomain, $redirectUrl) |
||
| 201 | { |
||
| 202 | $url = 'https://' . $shopDomain . '/admin/oauth/authorize?client_id=' . $this->apiKey; |
||
| 203 | $url .= '&scope=' . urlencode($this->scope); |
||
| 204 | $url .= '&redirect_uri=' . urlencode($redirectUrl); |
||
| 205 | $url .= '&state=' . $this->getNonce($shopDomain); |
||
| 206 | |||
| 207 | return $url; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $shopDomain |
||
| 212 | * @param string $code |
||
| 213 | * @return string|false |
||
| 214 | */ |
||
| 215 | public function requestAccessToken($shopDomain, $code) |
||
| 216 | { |
||
| 217 | $this->shopDomain = $shopDomain; |
||
| 218 | |||
| 219 | $http = new Client( |
||
| 220 | [ |
||
| 221 | 'host' => $shopDomain, |
||
| 222 | 'scheme' => 'https' |
||
| 223 | ] |
||
| 224 | ); |
||
| 225 | |||
| 226 | $response = $http->post( |
||
| 227 | '/admin/oauth/access_token', |
||
| 228 | 'client_id=' . $this->apiKey . |
||
| 229 | '&client_secret=' . $this->sharedSecret . |
||
| 230 | '&code=' . $code |
||
| 231 | ); |
||
| 232 | $response = $response->json; |
||
| 233 | ; |
||
| 234 | |||
| 235 | if (isset($response['access_token'])) { |
||
| 236 | $this->token = $response['access_token']; |
||
| 237 | |||
| 238 | return $this->token; |
||
| 239 | } else { |
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $shopDomain |
||
| 246 | * @return string|null |
||
| 247 | */ |
||
| 248 | 6 | public function setNonce($shopDomain) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @return string|null |
||
| 255 | */ |
||
| 256 | 6 | public function getNonce() |
|
| 257 | { |
||
| 258 | 6 | return $this->nonce; |
|
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return json |
||
| 263 | */ |
||
| 264 | public function getShopData() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param array $query |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | 6 | public function validateHMAC($query) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $url |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | 6 | private function _urlEncode($url) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | private function _isReady() |
||
| 314 | } |
||
| 315 |