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