| Total Complexity | 41 |
| Total Lines | 228 |
| 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, bool $bolComments, bool $bolSchemaLocation): array |
||
| 39 | { |
||
| 40 | // if no DocumentNameSpaces seen take Default ones from local configuration |
||
| 41 | $this->getSettingsFromFileIntoMemory($bolComments); |
||
| 42 | $arrayDefaults = $this->getDefaultsIntoDataSet($arrayData, $bolSchemaLocation); |
||
| 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 |
||
| 55 | { |
||
| 56 | $this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']); |
||
| 57 | foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) { |
||
| 58 | if ($key === '') { |
||
| 59 | $strValue = sprintf($value, $arrayDocumentData['DocumentTagName']); |
||
| 60 | $this->objXmlWriter->writeAttributeNS(NULL, 'xmlns', NULL, $strValue); |
||
| 61 | } else { |
||
| 62 | $this->objXmlWriter->writeAttributeNS('xmlns', $key, NULL, $value); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | if (array_key_exists('SchemaLocation', $arrayDocumentData)) { |
||
| 66 | $this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | private function setElementsOrdered(array $arrayInput): void |
||
| 71 | { |
||
| 72 | $this->setElementComment($arrayInput['commentParentKey']); |
||
| 73 | $this->objXmlWriter->startElement('cac:' . $arrayInput['tag']); |
||
| 74 | $this->setExtraElement($arrayInput, 'Start'); |
||
| 75 | $arrayCustomOrder = $this->arraySettings['CustomOrder'][$arrayInput['commentParentKey']]; |
||
| 76 | foreach ($arrayCustomOrder as $value) { // get the values in expected order |
||
| 77 | if (array_key_exists($value, $arrayInput['data'])) { // because certain value are optional |
||
| 78 | $key = implode('_', [$arrayInput['commentParentKey'], $value]); |
||
| 79 | $matches = []; |
||
| 80 | preg_match('/^(EndpointID|.*(Amount|Quantity))$/', $value, $matches, PREG_OFFSET_CAPTURE); |
||
| 81 | if ($value === 'TaxSubtotal') { |
||
| 82 | $this->setMultipleElementsOrdered([ |
||
| 83 | 'commentParentKey' => $key, |
||
| 84 | 'data' => $arrayInput['data'][$value], |
||
| 85 | 'tag' => $value, |
||
| 86 | ]); |
||
| 87 | } elseif (($matches !== []) || !is_array($arrayInput['data'][$value])) { |
||
| 88 | $this->setSingleElementWithAttribute([ |
||
| 89 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 90 | 'data' => $arrayInput['data'][$value], |
||
| 91 | 'tag' => $value, |
||
| 92 | ]); |
||
| 93 | } elseif (is_array($arrayInput['data'][$value])) { |
||
| 94 | $this->setElementsOrdered([ |
||
| 95 | 'commentParentKey' => $key, |
||
| 96 | 'data' => $arrayInput['data'][$value], |
||
| 97 | 'tag' => $value, |
||
| 98 | ]); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | $this->setExtraElement($arrayInput, 'End'); |
||
| 103 | $this->objXmlWriter->endElement(); // $key |
||
| 104 | } |
||
| 105 | |||
| 106 | private function setExtraElement(array $arrayInput, string $strType): void |
||
| 107 | { |
||
| 108 | if (in_array($arrayInput['tag'], ['AccountingCustomerParty', 'AccountingSupplierParty'])) { |
||
| 109 | switch ($strType) { |
||
| 110 | case 'End': |
||
| 111 | $this->objXmlWriter->endElement(); |
||
| 112 | break; |
||
| 113 | case 'Start': |
||
| 114 | $this->objXmlWriter->startElement('cac:Party'); |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | private function setHeaderCommonBasicComponents(array $arrayElementWithData): void |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | private function setManageComment(string $strCommentParentKey, array $arrayIn): string |
||
| 140 | } |
||
| 141 | |||
| 142 | private function setMultipleElementsOrdered(array $arrayData): void |
||
| 150 | ]); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | private function setPrepareXml(string $strFile): void |
||
| 161 | } |
||
| 162 | |||
| 163 | private function setProduceMiddleXml(array $arrayData): void |
||
| 164 | { |
||
| 165 | $arrayAggregates = $arrayData['Header']['CommonAggregateComponents-2']; |
||
| 166 | $arrayOptionalElementsHeader = [ |
||
| 167 | 'InvoicePeriod' => 'Single', |
||
| 168 | 'OrderReference' => 'Single', |
||
| 169 | 'BillingReference' => 'Single', |
||
| 170 | 'DespatchDocumentReference' => 'Single', |
||
| 171 | 'ReceiptDocumentReference' => 'Single', |
||
| 172 | 'OriginatorDocumentReference' => 'Single', |
||
| 173 | 'ContractDocumentReference' => 'Single', |
||
| 174 | 'ProjectReference' => 'Single', |
||
| 175 | 'AdditionalDocumentReference' => 'Multiple', |
||
| 176 | 'AccountingSupplierParty' => 'SingleCompany', |
||
| 177 | 'AccountingCustomerParty' => 'SingleCompany', |
||
| 178 | 'PayeeParty' => 'Single', |
||
| 179 | 'TaxRepresentativeParty' => 'Single', |
||
| 180 | 'Delivery' => 'Single', |
||
| 181 | 'PaymentTerms' => 'Single', |
||
| 182 | 'PaymentMeans' => 'Multiple', |
||
| 183 | 'AllowanceCharge' => 'Multiple', |
||
| 184 | 'TaxTotal' => 'Single', |
||
| 185 | 'LegalMonetaryTotal' => 'Single', |
||
| 186 | ]; |
||
| 187 | foreach ($arrayOptionalElementsHeader as $key => $strLogicType) { |
||
| 188 | if (array_key_exists($key, $arrayAggregates)) { |
||
| 189 | switch ($strLogicType) { |
||
| 190 | case 'Multiple': |
||
| 191 | $this->setMultipleElementsOrdered([ |
||
| 192 | 'commentParentKey' => $key, |
||
| 193 | 'data' => $arrayAggregates[$key], |
||
| 194 | 'tag' => $key, |
||
| 195 | ]); |
||
| 196 | break; |
||
| 197 | case 'Single': |
||
| 198 | $this->setElementsOrdered([ |
||
| 199 | 'commentParentKey' => $key, |
||
| 200 | 'data' => $arrayAggregates[$key], |
||
| 201 | 'tag' => $key, |
||
| 202 | ]); |
||
| 203 | break; |
||
| 204 | case 'SingleCompany': |
||
| 205 | $this->setElementsOrdered([ |
||
| 206 | 'commentParentKey' => $key, |
||
| 207 | 'data' => $arrayAggregates[$key]['Party'], |
||
| 208 | 'tag' => $key, |
||
| 209 | ]); |
||
| 210 | break; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | private function setSingleComment(array $arrayInput): void |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | private function setSingleElementWithAttribute(array $arrayInput): void |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | public function writeElectronicInvoice(string $strFile, array $arrayDataIn, bool $bolComments, bool $bolSchemaLocation = false): void |
||
| 261 |