| Total Complexity | 57 |
| Total Lines | 289 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ElectornicInvoiceWrite 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 ElectornicInvoiceWrite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ElectornicInvoiceWrite |
||
| 32 | { |
||
| 33 | |||
| 34 | use TraitVersions; |
||
| 35 | |||
| 36 | protected \XMLWriter $objXmlWriter; |
||
| 37 | |||
| 38 | private function loadSettingsAndManageDefaults(array $arrayData, array $arrayFeatures): array |
||
| 39 | { |
||
| 40 | // if no DocumentNameSpaces seen take Default ones from local configuration |
||
| 41 | $this->getSettingsFromFileIntoMemory($arrayFeatures['Comments']); |
||
| 42 | $arrayDefaults = $this->getDefaultsIntoDataSet($arrayData, $arrayFeatures['SchemaLocation']); |
||
| 43 | if ($arrayDefaults !== []) { |
||
| 44 | $arrayData = array_merge($arrayData, $arrayDefaults['Root']); |
||
| 45 | if (!array_key_exists('CustomizationID', $arrayData['Header']['CommonBasicComponents-2'])) { |
||
| 46 | $arrayData['Header']['CommonBasicComponents-2']['CustomizationID'] = 'urn:cen.eu:en16931:2017' |
||
| 47 | . '#compliant#urn:efactura.mfinante.ro:CIUS-RO:' . $arrayDefaults['CIUS-RO']; |
||
| 48 | $arrayData['Header']['CommonBasicComponents-2']['UBLVersionID'] = $arrayDefaults['UBL']; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | return $arrayData; |
||
| 52 | } |
||
| 53 | |||
| 54 | private function setDocumentTag(array $arrayDocumentData): void |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | private function setElementComment(string $strKey): void |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | private function setElementsOrdered(array $arrayInput): void |
||
| 85 | { |
||
| 86 | if ($arrayInput['commentParentKey'] === 'AllowanceCharge~ChargeIndicator0') { |
||
| 87 | error_log(json_encode($arrayInput)); |
||
| 88 | } |
||
| 89 | $this->setElementComment($arrayInput['commentParentKey']); |
||
| 90 | $this->objXmlWriter->startElement('cac:' . $arrayInput['tag']); |
||
| 91 | $this->setExtraElement($arrayInput, 'Start'); |
||
| 92 | $arrayCustomOrder = $this->arraySettings['CustomOrder'][$arrayInput['commentParentKey']]; |
||
| 93 | foreach ($arrayCustomOrder as $value) { // get the values in expected order |
||
| 94 | if (array_key_exists($value, $arrayInput['data'])) { // because certain value are optional |
||
| 95 | $key = implode('_', [$arrayInput['commentParentKey'], $value]); |
||
| 96 | $matches = []; |
||
| 97 | preg_match('/^.*(Amount|Quantity)$/', $value, $matches, PREG_OFFSET_CAPTURE); |
||
| 98 | if ($key === 'Lines_AllowanceCharge') { |
||
| 99 | foreach ($arrayInput['data'][$value] as $value2) { |
||
| 100 | $this->setElementsOrdered([ |
||
| 101 | 'commentParentKey' => $key, |
||
| 102 | 'data' => $value2, |
||
| 103 | 'tag' => $value, |
||
| 104 | ]); |
||
| 105 | } |
||
| 106 | } elseif (in_array($value, ['EmbeddedDocumentBinaryObject', 'EndpointID']) || in_array($arrayInput['commentParentKey'], ['AccountingCustomerParty_PartyIdentification', 'AccountingSupplierParty_PartyIdentification', 'Lines_Item_SellersItemIdentification', 'Lines_Item_StandardItemIdentification', 'Lines_Item_CommodityClassification', 'PayeeParty_PartyIdentification']) || ($key === 'Delivery_DeliveryLocation_ID')) { |
||
| 107 | $this->setSingleElementWithAttribute([ |
||
| 108 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 109 | 'data' => $arrayInput['data'][$value], |
||
| 110 | 'tag' => $value, |
||
| 111 | ]); |
||
| 112 | } elseif (in_array($value, ['AdditionalItemProperty', 'CommodityClassification', 'PartyTaxScheme', 'StandardItemIdentification', 'TaxSubtotal'])) { |
||
| 113 | $this->setMultipleElementsOrdered([ |
||
| 114 | 'commentParentKey' => $key, |
||
| 115 | 'data' => $arrayInput['data'][$value], |
||
| 116 | 'tag' => $value, |
||
| 117 | ]); |
||
| 118 | } elseif (($matches !== []) || !is_array($arrayInput['data'][$value])) { |
||
| 119 | $this->setSingleElementWithAttribute([ |
||
| 120 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 121 | 'data' => $arrayInput['data'][$value], |
||
| 122 | 'tag' => $value, |
||
| 123 | ]); |
||
| 124 | } elseif (is_array($arrayInput['data'][$value])) { |
||
| 125 | $this->setElementsOrdered([ |
||
| 126 | 'commentParentKey' => $key, |
||
| 127 | 'data' => $arrayInput['data'][$value], |
||
| 128 | 'tag' => $value, |
||
| 129 | ]); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | $this->setExtraElement($arrayInput, 'End'); |
||
| 134 | $this->objXmlWriter->endElement(); // $key |
||
| 135 | } |
||
| 136 | |||
| 137 | private function setExtraElement(array $arrayInput, string $strType): void |
||
| 138 | { |
||
| 139 | if (in_array($arrayInput['tag'], ['AccountingCustomerParty', 'AccountingSupplierParty'])) { |
||
| 140 | switch ($strType) { |
||
| 141 | case 'End': |
||
| 142 | $this->objXmlWriter->endElement(); |
||
| 143 | break; |
||
| 144 | case 'Start': |
||
| 145 | $this->objXmlWriter->startElement('cac:Party'); |
||
| 146 | break; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | private function setHeaderCommonBasicComponents(array $arrayElementWithData): void |
||
| 152 | { |
||
| 153 | $arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header_CBC']; |
||
| 154 | foreach ($arrayCustomOrdered as $value) { |
||
| 155 | if (array_key_exists($value, $arrayElementWithData)) { |
||
| 156 | $this->setElementComment($value); |
||
| 157 | $this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | private function setManageComment(string $strCommentParentKey, array $arrayIn): string |
||
| 163 | { |
||
| 164 | if (str_starts_with($strCommentParentKey, 'AllowanceCharge')) { |
||
| 165 | $arrayCommentPieces = explode('_', $strCommentParentKey); |
||
| 166 | // carefully manage a child to decide on comment tag |
||
| 167 | $strChargeIndicator = $arrayIn['ChargeIndicator']; |
||
| 168 | if (in_array($strChargeIndicator, ['0', '1'])) { |
||
| 169 | $strChargeIndicator = [ |
||
| 170 | '0' => 'false', |
||
| 171 | '1' => 'true', |
||
| 172 | ][$arrayIn['ChargeIndicator']]; |
||
| 173 | } |
||
| 174 | array_splice($arrayCommentPieces, 0, 1, 'AllowanceCharge~ChargeIndicator' |
||
| 175 | . ucfirst($strChargeIndicator)); |
||
| 176 | $strCommentParentKey = implode('_', $arrayCommentPieces); |
||
| 177 | } |
||
| 178 | return $strCommentParentKey; |
||
| 179 | } |
||
| 180 | |||
| 181 | private function setMultipleElementsOrdered(array $arrayData): void |
||
| 182 | { |
||
| 183 | foreach ($arrayData['data'] as $value) { |
||
| 184 | $strCommentParentKey = $this->setManageComment($arrayData['commentParentKey'], $value); |
||
| 185 | $this->setElementsOrdered([ |
||
| 186 | 'commentParentKey' => $strCommentParentKey, |
||
| 187 | 'data' => $value, |
||
| 188 | 'tag' => $arrayData['tag'], |
||
| 189 | ]); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | protected function setNumericValue(string $strTag, array $arrayDataIn): string|float |
||
| 194 | { |
||
| 195 | $sReturn = $arrayDataIn['value']; |
||
| 196 | $arrayRawTags = ['CreditedQuantity', 'EndpointID', 'InvoicedQuantity', 'ItemClassificationCode', 'PriceAmount']; |
||
| 197 | if (is_numeric($arrayDataIn['value']) && !in_array($strTag, $arrayRawTags)) { |
||
| 198 | $fmt = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL); |
||
| 199 | $fmt->setAttribute(\NumberFormatter::GROUPING_USED, 0); |
||
| 200 | $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0); |
||
| 201 | // if contains currencyID consider 2 decimals as minimum |
||
| 202 | if (in_array('currencyID', array_keys($arrayDataIn))) { |
||
| 203 | $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2); |
||
| 204 | } |
||
| 205 | $fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 2); |
||
| 206 | $sReturn = $fmt->format($arrayDataIn['value']); |
||
| 207 | } |
||
| 208 | return $sReturn; |
||
| 209 | } |
||
| 210 | |||
| 211 | private function setPrepareXml(string $strFile, int $intIdent = 4): void |
||
| 218 | } |
||
| 219 | |||
| 220 | private function setProduceMiddleXml(array $arrayData): void |
||
| 221 | { |
||
| 222 | $arrayAggregates = $arrayData['Header']['CommonAggregateComponents-2']; |
||
| 223 | $arrayOptionalElementsHeader = [ |
||
| 224 | 'InvoicePeriod' => 'Single', |
||
| 225 | 'OrderReference' => 'Single', |
||
| 226 | 'BillingReference' => 'Single', |
||
| 227 | 'DespatchDocumentReference' => 'Single', |
||
| 228 | 'ReceiptDocumentReference' => 'Single', |
||
| 229 | 'OriginatorDocumentReference' => 'Single', |
||
| 230 | 'ContractDocumentReference' => 'Single', |
||
| 231 | 'AdditionalDocumentReference' => 'Multiple', |
||
| 232 | 'ProjectReference' => 'Single', |
||
| 233 | 'AccountingSupplierParty' => 'SingleCompany', |
||
| 234 | 'AccountingCustomerParty' => 'SingleCompany', |
||
| 235 | 'PayeeParty' => 'Single', |
||
| 236 | 'TaxRepresentativeParty' => 'Single', |
||
| 237 | 'Delivery' => 'Single', |
||
| 238 | 'PaymentMeans' => 'Multiple', |
||
| 239 | 'PaymentTerms' => 'Single', |
||
| 240 | 'DocumentReference' => 'Single', |
||
| 241 | 'AllowanceCharge' => 'Multiple', |
||
| 242 | 'TaxTotal' => 'Multiple', |
||
| 243 | 'LegalMonetaryTotal' => 'Single', |
||
| 244 | ]; |
||
| 245 | foreach ($arrayOptionalElementsHeader as $key => $strLogicType) { |
||
| 246 | if (array_key_exists($key, $arrayAggregates)) { |
||
| 247 | switch ($strLogicType) { |
||
| 248 | case 'Multiple': |
||
| 249 | $this->setMultipleElementsOrdered([ |
||
| 250 | 'commentParentKey' => $key, |
||
| 251 | 'data' => $arrayAggregates[$key], |
||
| 252 | 'tag' => $key, |
||
| 253 | ]); |
||
| 254 | break; |
||
| 255 | case 'Single': |
||
| 256 | $this->setElementsOrdered([ |
||
| 257 | 'commentParentKey' => $key, |
||
| 258 | 'data' => $arrayAggregates[$key], |
||
| 259 | 'tag' => $key, |
||
| 260 | ]); |
||
| 261 | break; |
||
| 262 | case 'SingleCompany': |
||
| 263 | $this->setElementsOrdered([ |
||
| 264 | 'commentParentKey' => $key, |
||
| 265 | 'data' => $arrayAggregates[$key]['Party'], |
||
| 266 | 'tag' => $key, |
||
| 267 | ]); |
||
| 268 | break; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | private function setSingleComment(array $arrayInput): void |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | private function setSingleElementWithAttribute(array $arrayInput): void |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | public function writeElectronicInvoice(string $strFile, array $inData, array $arrayFeatures): void |
||
| 320 | } |
||
| 321 | } |
||
| 322 |