| Conditions | 6 |
| Paths | 24 |
| Total Lines | 52 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 42 | public static function getApiContext(): ApiContext |
||
| 43 | { |
||
| 44 | $contextFile = ''; |
||
| 45 | $environmentType = BunqEnumApiEnvironmentType::SANDBOX(); |
||
| 46 | if (config('bunq.use_sandbox')) { |
||
| 47 | $environmentType = BunqEnumApiEnvironmentType::SANDBOX(); |
||
| 48 | $contextFile = storage_path('context/bunq_sandbox.context'); |
||
| 49 | app('log')->debug('Will create sandbox bunq API Context'); |
||
| 50 | } |
||
| 51 | if (config('bunq.use_production')) { |
||
| 52 | $environmentType = BunqEnumApiEnvironmentType::PRODUCTION(); |
||
| 53 | $contextFile = storage_path('context/bunq_pr.context'); |
||
| 54 | app('log')->debug('Will create PR bunq API Context'); |
||
| 55 | } |
||
| 56 | // restore if exists. |
||
| 57 | if (file_exists($contextFile)) { |
||
| 58 | $apiContext = ApiContext::restore($contextFile); |
||
| 59 | BunqContext::loadApiContext($apiContext); |
||
| 60 | app('log')->debug('Restored existing bunq context.'); |
||
| 61 | |||
| 62 | return $apiContext; |
||
| 63 | } |
||
| 64 | // create if not. |
||
| 65 | $apiKey = config('bunq.api_code'); |
||
| 66 | $deviceDescription = sprintf('Firefly III bunq importer v%s', config('bunq.version')); |
||
| 67 | $permittedIps = []; // List the real expected IPs of this device or leave empty to use the current IP |
||
| 68 | try { |
||
| 69 | app('log')->debug('Try to build API context with given parameters.'); |
||
| 70 | $apiContext = ApiContext::create( |
||
| 71 | $environmentType, |
||
| 72 | $apiKey, |
||
| 73 | $deviceDescription, |
||
| 74 | $permittedIps |
||
| 75 | ); |
||
| 76 | } catch (BadRequestException $e) { |
||
| 77 | app('log')->error($e->getMessage()); |
||
| 78 | app('log')->error($e->getTraceAsString()); |
||
| 79 | throw new ImportException($e->getMessage()); |
||
| 80 | } |
||
| 81 | |||
| 82 | BunqContext::loadApiContext($apiContext); |
||
| 83 | try { |
||
| 84 | app('log')->debug('Trying to save API context.'); |
||
| 85 | $apiContext->save($contextFile); |
||
| 86 | } catch (BunqException $e) { |
||
| 87 | app('log')->error($e->getMessage()); |
||
| 88 | app('log')->error($e->getTraceAsString()); |
||
| 89 | throw new ImportException($e->getMessage()); |
||
| 90 | } |
||
| 91 | app('log')->debug('Done! return API context.'); |
||
| 92 | |||
| 93 | return $apiContext; |
||
| 94 | } |
||
| 96 |