| Conditions | 15 |
| Paths | 50 |
| Total Lines | 101 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 135 | private function generateTransaction(int $bunqAccountId, array $entry): array |
||
| 136 | { |
||
| 137 | $return = [ |
||
| 138 | 'apply_rules' => $this->configuration->isRules(), |
||
| 139 | 'error_if_duplicate_hash' => true, |
||
| 140 | 'transactions' => [ |
||
| 141 | [ |
||
| 142 | 'type' => 'withdrawal', // reverse |
||
| 143 | 'date' => substr($entry['created'], 0, 10), |
||
| 144 | 'datetime' => $entry['created'], // not used in API, only for transaction filtering. |
||
| 145 | 'amount' => 0, |
||
| 146 | 'description' => $entry['description'], |
||
| 147 | 'order' => 0, |
||
| 148 | 'currency_code' => $entry['currency_code'], |
||
| 149 | 'tags' => [$entry['type'], $entry['sub_type']], |
||
| 150 | ], |
||
| 151 | ], |
||
| 152 | ]; |
||
| 153 | |||
| 154 | // save meta: |
||
| 155 | $return['transactions'][0]['bunq_payment_id'] = $entry['id']; |
||
| 156 | $return['transactions'][0]['external_id'] = $entry['id']; |
||
| 157 | $return['transactions'][0]['internal_reference'] = $bunqAccountId; |
||
| 158 | |||
| 159 | // give "auto save" transactions a different description: |
||
| 160 | if ('SAVINGS' === $entry['type'] && 'PAYMENT' === $entry['sub_type']) { |
||
| 161 | $return['transactions'][0]['description'] = '(auto save transaction)'; |
||
| 162 | } |
||
| 163 | |||
| 164 | if (1 === bccomp($entry['amount'], '0')) { |
||
| 165 | // amount is positive: deposit or transfer. Bunq account is destination |
||
| 166 | $return['transactions'][0]['type'] = 'deposit'; |
||
| 167 | $return['transactions'][0]['amount'] = $entry['amount']; |
||
| 168 | |||
| 169 | // destination is bunq |
||
| 170 | $return['transactions'][0]['destination_id'] = (int) $this->accounts[$bunqAccountId]; |
||
| 171 | |||
| 172 | // source is the other side: |
||
| 173 | $return['transactions'][0]['source_iban'] = $entry['counter_party']['iban']; |
||
| 174 | $return['transactions'][0]['source_name'] = $entry['counter_party']['display_name']; |
||
| 175 | |||
| 176 | $mappedId = $this->getMappedId($entry['counter_party']['display_name'], (string) $entry['counter_party']['iban']); |
||
| 177 | if (null !== $mappedId && 0 !== $mappedId) { |
||
| 178 | $mappedType = $this->getMappedType($mappedId); |
||
| 179 | $return['transactions'][0]['type'] = $this->getTransactionType($mappedType, 'asset'); |
||
| 180 | $return['transactions'][0]['source_id'] = $mappedId; |
||
| 181 | unset($return['transactions'][0]['source_iban'], $return['transactions'][0]['source_name']); |
||
| 182 | } |
||
| 183 | //Log::debug(sprintf('Mapped ID is %s', var_export($mappedId, true))); |
||
| 184 | // check target accounts as well: |
||
| 185 | $iban = $entry['counter_party']['iban']; |
||
| 186 | if ((null === $mappedId || 0 === $mappedId) && isset($this->targetAccounts[$iban])) { |
||
| 187 | Log::debug(sprintf('Found IBAN %s in target accounts (ID %d). Type is %s', $iban, $this->targetAccounts[$iban], $this->targetTypes[$iban])); |
||
| 188 | |||
| 189 | // type: source comes from $targetTypes, destination is asset (see above). |
||
| 190 | $return['transactions'][0]['type'] = $this->getTransactionType($this->targetTypes[$iban] ?? '', 'asset'); |
||
| 191 | $return['transactions'][0]['source_id'] = $this->targetAccounts[$iban]; |
||
| 192 | unset($return['transactions'][0]['source_iban'], $return['transactions'][0]['source_name']); |
||
| 193 | Log::debug(sprintf('Replaced source IBAN %s with ID #%d (type %s).', $iban, $this->targetAccounts[$iban], $this->targetTypes[$iban])); |
||
| 194 | } |
||
| 195 | unset($iban); |
||
| 196 | } |
||
| 197 | |||
| 198 | // TODO these two if statements are mirrors of each other. |
||
| 199 | |||
| 200 | if (-1 === bccomp($entry['amount'], '0')) { |
||
| 201 | // amount is negative: withdrawal or transfer. |
||
| 202 | $return['transactions'][0]['amount'] = bcmul($entry['amount'], '-1'); |
||
| 203 | |||
| 204 | // source is bunq: |
||
| 205 | $return['transactions'][0]['source_id'] = (int) $this->accounts[$bunqAccountId]; |
||
| 206 | |||
| 207 | // dest is shop |
||
| 208 | $return['transactions'][0]['destination_iban'] = $entry['counter_party']['iban']; |
||
| 209 | $return['transactions'][0]['destination_name'] = $entry['counter_party']['display_name']; |
||
| 210 | |||
| 211 | $mappedId = $this->getMappedId($entry['counter_party']['display_name'], (string) $entry['counter_party']['iban']); |
||
| 212 | //Log::debug(sprintf('Mapped ID is %s', var_export($mappedId, true))); |
||
| 213 | if (null !== $mappedId && 0 !== $mappedId) { |
||
| 214 | $return['transactions'][0]['destination_id'] = $mappedId; |
||
| 215 | $mappedType = $this->getMappedType($mappedId); |
||
| 216 | $return['transactions'][0]['type'] = $this->getTransactionType('asset', $mappedType); |
||
| 217 | unset($return['transactions'][0]['destination_iban'], $return['transactions'][0]['destination_name']); |
||
| 218 | } |
||
| 219 | |||
| 220 | // check target accounts as well: |
||
| 221 | $iban = $entry['counter_party']['iban']; |
||
| 222 | if ((null === $mappedId || 0 === $mappedId) && isset($this->targetAccounts[$iban])) { |
||
| 223 | Log::debug(sprintf('Found IBAN %s in target accounts (ID %d). Type is %s', $iban, $this->targetAccounts[$iban], $this->targetTypes[$iban])); |
||
| 224 | |||
| 225 | // source is always asset, destination depends on $targetType. |
||
| 226 | $return['transactions'][0]['type'] = $this->getTransactionType('asset', $this->targetTypes[$iban] ?? ''); |
||
| 227 | $return['transactions'][0]['destination_id'] = $this->targetAccounts[$iban]; |
||
| 228 | unset($return['transactions'][0]['destination_iban'], $return['transactions'][0]['destination_name']); |
||
| 229 | Log::debug(sprintf('Replaced source IBAN %s with ID #%d (type %s).', $iban, $this->targetAccounts[$iban], $this->targetTypes[$iban])); |
||
| 230 | } |
||
| 231 | unset($iban); |
||
| 232 | } |
||
| 233 | app('log')->debug(sprintf('Parsed bunq transaction #%d', $entry['id'])); |
||
| 234 | |||
| 235 | return $return; |
||
| 236 | } |
||
| 321 |