| Conditions | 6 |
| Paths | 8 |
| Total Lines | 69 |
| Code Lines | 32 |
| 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 |
||
| 128 | public function exportUsingGivenIBAN(array $payment_orders, string $debtor_iban, string $debtor_bic, string $debtor_account_name): SEPAExport |
||
| 129 | { |
||
| 130 | //We need IBAN without spaces |
||
| 131 | $debtor_iban = str_replace(" ", "", $debtor_iban); |
||
| 132 | |||
| 133 | $groupHeader = $this->group_header_helper->getGroupHeader($debtor_bic); |
||
| 134 | $sepaFile = new CustomerCreditTransferFile($groupHeader); |
||
| 135 | |||
| 136 | $payment = new PaymentInformation( |
||
| 137 | md5(random_bytes(50)), //Use a random payment identifier ID |
||
| 138 | $debtor_iban, |
||
| 139 | $debtor_bic, |
||
| 140 | $debtor_account_name |
||
| 141 | ); |
||
| 142 | |||
| 143 | //Disable batch booking, as the commerzbank does not show details for "Sammelüberweisungen" |
||
| 144 | $payment->setBatchBooking(false); |
||
| 145 | |||
| 146 | //Add each payment order as transaction |
||
| 147 | foreach ($payment_orders as $payment_order) { |
||
| 148 | //Ensure that type is correct |
||
| 149 | if (!$payment_order instanceof PaymentOrder) { |
||
| 150 | throw new \InvalidArgumentException('$payment_orders must be an array of PaymentOrder elements!'); |
||
| 151 | } |
||
| 152 | |||
| 153 | //Ensure that the ID is available |
||
| 154 | if (!$payment_order->getId() === null) { |
||
| 155 | throw new \InvalidArgumentException('A payment order that should be exported misses an ID. All payment orders must have been persisted!'); |
||
| 156 | } |
||
| 157 | |||
| 158 | $transfer = new CustomerCreditTransferInformation( |
||
| 159 | $payment_order->getAmount(), |
||
| 160 | $payment_order->getBankInfo()->getIbanWithoutSpaces(), |
||
| 161 | $payment_order->getBankInfo()->getAccountOwner() |
||
| 162 | ); |
||
| 163 | |||
| 164 | //BIC is optional, only set it if it was set. |
||
| 165 | if (!empty($payment_order->getBankInfo()->getBic())) { |
||
| 166 | $transfer->setBic($payment_order->getBankInfo()->getBic()); |
||
| 167 | } |
||
| 168 | |||
| 169 | //We use the ID String of the payment order as end to end reference |
||
| 170 | $transfer->setEndToEndIdentification($payment_order->getIDString()); |
||
| 171 | //Set the reference ID of the payment order as |
||
| 172 | $transfer->setRemittanceInformation($payment_order->getBankInfo()->getReference()); |
||
| 173 | |||
| 174 | $payment->addTransfer($transfer); |
||
| 175 | } |
||
| 176 | |||
| 177 | //Add payment infos to SEPA file |
||
| 178 | $sepaFile->addPaymentInformation($payment); |
||
| 179 | |||
| 180 | // We have to use the format 'pain.001.001.03' |
||
| 181 | $domBuilder = DomBuilderFactory::createDomBuilder($sepaFile, 'pain.001.001.03'); |
||
| 182 | |||
| 183 | if (!$domBuilder instanceof BaseDomBuilder) { |
||
|
|
|||
| 184 | throw new \RuntimeException('$domBuilder must be an BaseDomBuilder instance!'); |
||
| 185 | } |
||
| 186 | |||
| 187 | //Create a temporary file with the XML content |
||
| 188 | $xml_string = $domBuilder->asXml(); |
||
| 189 | |||
| 190 | //We use the format YYYYMMDDHHmmss_MsgID.xml |
||
| 191 | $original_filename = sprintf("%s_%s.xml", |
||
| 192 | (new \DateTime())->format('YmdHis'), |
||
| 193 | $groupHeader->getMessageIdentification(), |
||
| 194 | ); |
||
| 195 | |||
| 196 | return SEPAExport::createFromXMLString($xml_string, $original_filename); |
||
| 197 | } |
||
| 202 | } |