| Total Complexity | 43 |
| Total Lines | 390 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CommonApiClient 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.
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 CommonApiClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class CommonApiClient implements CommonApiClientInterface |
||
| 42 | { |
||
| 43 | const URL_SANDBOX = 'https://proxy.staging.devpayever.com/'; |
||
| 44 | const URL_LIVE = 'https://proxy.payever.org/'; |
||
| 45 | |||
| 46 | const SUB_URL_AUTH = 'oauth/v2/token'; |
||
| 47 | const SUB_URL_LIST_CHANNEL_SETS = 'api/shop/oauth/%s/channel-sets'; |
||
| 48 | const SUB_URL_CURRENCY = 'api/rest/v1/currency'; |
||
| 49 | |||
| 50 | const FORBIDDEN_ERROR_CODE = 403; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Stores oAuth Authentication Tokens |
||
| 54 | * |
||
| 55 | * @var OauthTokenList $tokens |
||
| 56 | */ |
||
| 57 | protected $tokens; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Stores API Configuration |
||
| 61 | * |
||
| 62 | * @var ClientConfigurationInterface $configuration |
||
| 63 | */ |
||
| 64 | protected $configuration; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Stores current Client |
||
| 68 | * |
||
| 69 | * @var HttpClientInterface $httpClient |
||
| 70 | */ |
||
| 71 | protected $httpClient; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param ClientConfigurationInterface $clientConfiguration |
||
| 75 | * @param OauthTokenList $oauthTokenList |
||
| 76 | * @param HttpClientInterface $httpClient |
||
| 77 | * |
||
| 78 | * @throws \Exception |
||
| 79 | */ |
||
| 80 | public function __construct( |
||
| 81 | ClientConfigurationInterface $clientConfiguration, |
||
| 82 | OauthTokenList $oauthTokenList = null, |
||
| 83 | HttpClientInterface $httpClient = null |
||
| 84 | ) { |
||
| 85 | $this->configuration = $clientConfiguration; |
||
| 86 | $this->httpClient = $httpClient; |
||
| 87 | $this->loadTokens($oauthTokenList); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns Base URL to payever Payments API |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function getBaseUrl() |
||
| 96 | { |
||
| 97 | return $this->getBaseEntrypoint(true); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns current configuration |
||
| 102 | * |
||
| 103 | * @return ClientConfigurationInterface |
||
| 104 | */ |
||
| 105 | public function getConfiguration() |
||
| 106 | { |
||
| 107 | return $this->configuration; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns current OauthToken list |
||
| 112 | * |
||
| 113 | * @return OauthTokenList |
||
| 114 | */ |
||
| 115 | public function getTokens() |
||
| 116 | { |
||
| 117 | return $this->tokens; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Overrides configuration |
||
| 122 | * |
||
| 123 | * @param ClientConfigurationInterface $configuration |
||
| 124 | */ |
||
| 125 | public function setConfiguration(ClientConfigurationInterface $configuration) |
||
| 126 | { |
||
| 127 | $this->configuration = $configuration; |
||
| 128 | |||
| 129 | $this->getTokens()->clear()->save(); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function getHttpClient() |
||
| 136 | { |
||
| 137 | if ($this->httpClient === null) { |
||
| 138 | $this->httpClient = new CurlClient(); |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($this->httpClient instanceof LoggerAwareInterface) { |
||
| 142 | $this->httpClient->setLogger($this->configuration->getLogger()); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $this->httpClient; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $logLevel |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function setHttpClientRequestFailureLogLevel($logLevel = LogLevel::CRITICAL) |
||
| 153 | { |
||
| 154 | if ($this->getHttpClient() instanceof CurlClient) { |
||
| 155 | $this->getHttpClient()->setLogLevel($logLevel); |
||
| 156 | } |
||
| 157 | |||
| 158 | return $this; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $logLevel |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | public function setHttpClientRequestFailureLogLevelOnce($logLevel) |
||
| 166 | { |
||
| 167 | if ($this->getHttpClient() instanceof CurlClient) { |
||
| 168 | $this->getHttpClient()->setLogLevelOnce($logLevel); |
||
| 169 | } |
||
| 170 | |||
| 171 | return $this; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function setHttpClient(HttpClientInterface $httpClient) |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns Authentication OauthToken |
||
| 190 | * |
||
| 191 | * @param string $scope OauthToken scope |
||
| 192 | * |
||
| 193 | * @return OauthTokenInterface |
||
| 194 | * |
||
| 195 | * @throws \Exception |
||
| 196 | */ |
||
| 197 | public function getToken($scope = OauthTokenInterface::SCOPE_PAYMENT_ACTIONS) |
||
| 198 | { |
||
| 199 | $key = md5($this->getConfiguration()->getHash() . $scope); |
||
| 200 | |||
| 201 | $this->configuration->getLogger()->debug(sprintf('Getting OAuth token. Hash: %s', $key)); |
||
| 202 | |||
| 203 | /** @var OauthTokenInterface $token */ |
||
| 204 | $token = $this->getTokens()->get($key); |
||
| 205 | |||
| 206 | if (!$token || ($token instanceof OauthTokenInterface && $token->isExpired() && !$token->isRefreshable())) { |
||
|
|
|||
| 207 | $tokenData = $this->obtainTokenRequest($scope)->getResponseEntity()->toArray(); |
||
| 208 | /** @var OauthTokenInterface $token */ |
||
| 209 | $token = $this->getTokens()->add( |
||
| 210 | $key, |
||
| 211 | $this->getTokens()->create()->load($tokenData)->setUpdatedAt() |
||
| 212 | )->get($key); |
||
| 213 | |||
| 214 | $this->getTokens()->save(); |
||
| 215 | } elseif ($token instanceof OauthTokenInterface && $token->isExpired() && $token->isRefreshable()) { |
||
| 216 | $tokenData = $this->refreshTokenRequest($token)->getResponseEntity()->toArray(); |
||
| 217 | |||
| 218 | $token->load($tokenData)->setUpdatedAt(); |
||
| 219 | |||
| 220 | $this->getTokens()->save(); |
||
| 221 | } |
||
| 222 | |||
| 223 | $this->configuration->getLogger()->debug( |
||
| 224 | sprintf('Got OAuth token. Hash: %s', $key), |
||
| 225 | $token->getParams() |
||
| 226 | ); |
||
| 227 | |||
| 228 | return $token; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | * |
||
| 234 | * @throws \Exception |
||
| 235 | */ |
||
| 236 | public function getCurrenciesRequest($lang = '') |
||
| 237 | { |
||
| 238 | $request = RequestBuilder::get($this->getCurrenciesURL($lang)) |
||
| 239 | ->setResponseEntity(new GetCurrenciesResponse()) |
||
| 240 | ->build() |
||
| 241 | ; |
||
| 242 | |||
| 243 | return $this->getHttpClient()->execute($request); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | * |
||
| 249 | * @throws \Exception |
||
| 250 | */ |
||
| 251 | public function listChannelSetsRequest($businessUuid) |
||
| 252 | { |
||
| 253 | $this->configuration->assertLoaded(); |
||
| 254 | |||
| 255 | $request = RequestBuilder::get($this->getListChannelSetsURL($businessUuid)) |
||
| 256 | ->addRawHeader( |
||
| 257 | $this->getToken(OauthToken::SCOPE_PAYMENT_INFO)->getAuthorizationString() |
||
| 258 | ) |
||
| 259 | ->setResponseEntity(new ListChannelSetsResponse()) |
||
| 260 | ->build() |
||
| 261 | ; |
||
| 262 | |||
| 263 | return $this->executeRequest($request, OauthToken::SCOPE_PAYMENT_INFO); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param bool $staticBind |
||
| 268 | * @return string |
||
| 269 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 270 | */ |
||
| 271 | protected function getBaseEntrypoint($staticBind = false) |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Loads Tokens |
||
| 294 | * |
||
| 295 | * @param OauthTokenList|null $oauthTokenList |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | protected function loadTokens(OauthTokenList $oauthTokenList = null) |
||
| 300 | { |
||
| 301 | if (is_null($oauthTokenList)) { |
||
| 302 | $oauthTokenList = new DummyOauthTokenList(); |
||
| 303 | } |
||
| 304 | |||
| 305 | $this->tokens = $oauthTokenList->load(); |
||
| 306 | |||
| 307 | return $this; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Requests new oAuth OauthToken which will be used further |
||
| 312 | * |
||
| 313 | * @link https://docs.payever.org/shopsystems/api/getting-started/authentication Documentation |
||
| 314 | * |
||
| 315 | * @param string $scope Scope in which the token will be used |
||
| 316 | * |
||
| 317 | * @return ResponseInterface |
||
| 318 | * |
||
| 319 | * @throws \Exception |
||
| 320 | */ |
||
| 321 | protected function obtainTokenRequest($scope) |
||
| 322 | { |
||
| 323 | $this->configuration->assertLoaded(); |
||
| 324 | |||
| 325 | if (!in_array($scope, OauthToken::getScopes())) { |
||
| 326 | throw new \Exception('Scope provided is not valid'); |
||
| 327 | } |
||
| 328 | |||
| 329 | $requestEntity = new AuthenticationRequest(); |
||
| 330 | $requestEntity |
||
| 331 | ->setClientId($this->configuration->getClientId()) |
||
| 332 | ->setClientSecret($this->configuration->getClientSecret()) |
||
| 333 | ->setScope($scope) |
||
| 334 | ->setGrantType(OauthTokenInterface::GRAND_TYPE_OBTAIN_TOKEN); |
||
| 335 | |||
| 336 | $request = RequestBuilder::post($this->getAuthenticationURL()) |
||
| 337 | ->setRequestEntity($requestEntity) |
||
| 338 | ->setResponseEntity(new AuthenticationResponse()) |
||
| 339 | ->build(); |
||
| 340 | |||
| 341 | return $this->getHttpClient()->execute($request); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Requests for an updated oAuth OauthToken data |
||
| 346 | * |
||
| 347 | * @param OauthTokenInterface|object|array $token OauthToken for the update |
||
| 348 | * |
||
| 349 | * @return ResponseInterface |
||
| 350 | * |
||
| 351 | * @throws \Exception |
||
| 352 | */ |
||
| 353 | protected function refreshTokenRequest(OauthTokenInterface $token) |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param Request $request |
||
| 375 | * @param string $scope |
||
| 376 | * |
||
| 377 | * @return \Payever\ExternalIntegration\Core\Http\Response |
||
| 378 | * @throws \Exception |
||
| 379 | */ |
||
| 380 | protected function executeRequest($request, $scope = OauthToken::SCOPE_PAYMENT_ACTIONS) |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Returns URL for Authentication path |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | protected function getAuthenticationURL() |
||
| 405 | { |
||
| 406 | return $this->getBaseEntrypoint() . self::SUB_URL_AUTH; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Returns URL for Available Channel Sets path |
||
| 411 | * |
||
| 412 | * @param string $businessUuid |
||
| 413 | * |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | protected function getListChannelSetsURL($businessUuid) |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Returns URL to Currencies path |
||
| 423 | * |
||
| 424 | * @param string $lang |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | protected function getCurrenciesURL($lang = '') |
||
| 431 | } |
||
| 432 | } |
||
| 433 |