| Total Complexity | 42 |
| Total Lines | 272 |
| Duplicated Lines | 0 % |
| Changes | 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 $objXmlWriter; |
||
| 37 | |||
| 38 | private function setDocumentTag(array $arrayDocumentData): void { |
||
| 39 | $this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']); |
||
| 40 | foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) { |
||
| 41 | if ($key === '') { |
||
| 42 | $strValue = sprintf($value, $arrayDocumentData['DocumentTagName']); |
||
| 43 | $this->objXmlWriter->writeAttributeNS(NULL, 'xmlns', NULL, $strValue); |
||
| 44 | } else { |
||
| 45 | $this->objXmlWriter->writeAttributeNS('xmlns', $key, NULL, $value); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | if (array_key_exists('SchemaLocation', $arrayDocumentData)) { |
||
| 49 | $this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | private function setElementComment(string $strKey, string $strSection, bool $includeComments): void { |
||
| 54 | if ($includeComments && array_key_exists($strKey, $this->arraySettings['Comments'][$strSection])) { |
||
| 55 | switch ($strSection) { |
||
| 56 | case 'CAC': |
||
| 57 | $elementComment = $this->arraySettings['Comments'][$strSection][$strKey]; |
||
| 58 | if (is_array($elementComment)) { |
||
| 59 | foreach ($elementComment as $value) { |
||
| 60 | $this->objXmlWriter->writeComment($value); |
||
| 61 | } |
||
| 62 | } else { |
||
| 63 | $this->objXmlWriter->writeComment($elementComment); |
||
| 64 | } |
||
| 65 | break; |
||
| 66 | case 'CBC': |
||
| 67 | $this->objXmlWriter->writeComment($this->arraySettings['Comments'][$strSection][$strKey]); |
||
| 68 | break; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | private function setCompanyElementsOrdered(array $arrayParameters): void { |
||
| 74 | $this->setElementComment($arrayParameters['commentParentKey'], 'CAC', $arrayParameters['comments']); |
||
| 75 | $this->objXmlWriter->startElement('cac:' . $arrayParameters['tag']); |
||
| 76 | $arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$arrayParameters['commentParentKey']]; |
||
| 77 | foreach ($arrayCustomOrder as $value) { |
||
| 78 | if (array_key_exists($value, $arrayParameters['data'])) { |
||
| 79 | $this->setElementComment(implode('_', [ |
||
| 80 | $arrayParameters['commentParentKey'], $value]), 'CAC', $arrayParameters['comments']); |
||
| 81 | if (is_array($arrayParameters['data'][$value])) { |
||
| 82 | $this->objXmlWriter->startElement('cac:' . $value); |
||
| 83 | foreach ($arrayParameters['data'][$value] as $key2 => $value2) { |
||
| 84 | $this->setElementComment(implode('_', [ |
||
| 85 | $arrayParameters['commentParentKey'], $value, $key2]), 'CAC', $arrayParameters['comments']); |
||
| 86 | $this->objXmlWriter->writeElement('cbc:' . $key2, $value2); |
||
| 87 | } |
||
| 88 | $this->objXmlWriter->endElement(); |
||
| 89 | } else { |
||
| 90 | $this->objXmlWriter->writeElement('cbc:' . $value, $arrayParameters['data'][$value]); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | $this->objXmlWriter->endElement(); // $key |
||
| 95 | } |
||
| 96 | |||
| 97 | private function setHeaderCommonAggregateComponentsCompanies(array $arrayParameters): void { |
||
| 98 | $key = $arrayParameters['tag']; |
||
| 99 | $this->setElementComment($key, 'CAC', $arrayParameters['comments']); |
||
| 100 | $this->objXmlWriter->startElement('cac:' . $key); |
||
| 101 | $this->objXmlWriter->startElement('cac:Party'); |
||
| 102 | $arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$key]; |
||
| 103 | foreach ($arrayCustomOrder as $value) { |
||
| 104 | if (array_key_exists($value, $arrayParameters['data'])) { |
||
| 105 | $this->setCompanyElementsOrdered([ |
||
| 106 | 'comments' => $arrayParameters['comments'], |
||
| 107 | 'commentParentKey' => implode('_', [$key, $value]), |
||
| 108 | 'data' => $arrayParameters['data'][$value], |
||
| 109 | 'tag' => $value, |
||
| 110 | ]); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | $this->objXmlWriter->endElement(); // Party |
||
| 114 | $this->objXmlWriter->endElement(); // $key |
||
| 115 | } |
||
| 116 | |||
| 117 | private function setHeaderCommonAggregateComponentsTaxTotal(array $arrayParameters): void { |
||
| 147 | } |
||
| 148 | |||
| 149 | private function setHeaderCommonBasicComponents(array $arrayElementWithData, bool $includeComments): void { |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | private function setTaxCategory(array $arrayParameters): void { |
||
| 158 | $key = $arrayParameters['tag']; |
||
| 159 | $this->objXmlWriter->startElement('cac:' . $key); |
||
| 160 | $this->setElementComment(implode('_', [ |
||
| 161 | $arrayParameters['commentParentKey'], 'ID']), 'CAC', $arrayParameters['comments']); |
||
| 162 | $this->objXmlWriter->writeElement('cbc:ID', $arrayParameters['data']['ID']); |
||
| 163 | $this->setElementComment(implode('_', [ |
||
| 164 | $arrayParameters['commentParentKey'], 'Percent']), 'CAC', $arrayParameters['comments']); |
||
| 165 | $this->objXmlWriter->writeElement('cbc:Percent', $arrayParameters['data']['Percent']); |
||
| 166 | $this->objXmlWriter->startElement('cac:TaxScheme'); |
||
| 167 | $this->objXmlWriter->writeElement('cbc:ID', $arrayParameters['data']['TaxScheme']['ID']); |
||
| 168 | $this->objXmlWriter->endElement(); // $key |
||
| 169 | $this->objXmlWriter->endElement(); // $key |
||
| 170 | } |
||
| 171 | |||
| 172 | public function writeElectronicInvoice(string $strFile, array $arrayData, bool $bolComments): void { |
||
| 173 | $this->objXmlWriter = new \XMLWriter(); |
||
| 174 | $this->objXmlWriter->openURI($strFile); |
||
| 175 | $this->objXmlWriter->setIndent(true); |
||
| 176 | $this->objXmlWriter->setIndentString(str_repeat(' ', 4)); |
||
| 177 | $this->objXmlWriter->startDocument('1.0', 'UTF-8'); |
||
| 178 | // if no DocumentNameSpaces seen take Default ones from local configuration |
||
| 179 | $this->getSettingsFromFileIntoMemory(); |
||
| 180 | $arrayDefaults = $this->getDefaultsIntoDataSet($arrayData); |
||
| 181 | if ($arrayDefaults !== []) { |
||
| 182 | $arrayData = array_merge($arrayData, $arrayDefaults['Root']); |
||
| 183 | if (!array_key_exists('CustomizationID', $arrayData['Header']['CommonBasicComponents-2'])) { |
||
| 184 | $arrayData['Header']['CommonBasicComponents-2']['CustomizationID'] = $arrayDefaults['CIUS-RO']; |
||
| 185 | $arrayData['Header']['CommonBasicComponents-2']['UBLVersionID'] = $arrayDefaults['UBL']; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | $this->setDocumentTag($arrayData); |
||
| 189 | $this->setHeaderCommonBasicComponents($arrayData['Header']['CommonBasicComponents-2'], $bolComments); |
||
| 190 | $arrayAggegateComponents = $arrayData['Header']['CommonAggregateComponents-2']; |
||
| 191 | $this->setHeaderCommonAggregateComponentsCompanies([ |
||
| 192 | 'data' => $arrayAggegateComponents['AccountingSupplierParty']['Party'], |
||
| 193 | 'tag' => 'AccountingSupplierParty', |
||
| 194 | 'comments' => $bolComments, |
||
| 195 | ]); |
||
| 196 | $this->setHeaderCommonAggregateComponentsCompanies([ |
||
| 197 | 'data' => $arrayAggegateComponents['AccountingCustomerParty']['Party'], |
||
| 198 | 'tag' => 'AccountingCustomerParty', |
||
| 199 | 'comments' => $bolComments, |
||
| 200 | ]); |
||
| 201 | // multiple accounts can be specified within PaymentMeans |
||
| 202 | if (is_array($arrayAggegateComponents['PaymentMeans'])) { |
||
| 203 | foreach ($arrayAggegateComponents['PaymentMeans'] as $value) { |
||
| 204 | $this->setHeaderCommonAggregateComponentsOthers([ |
||
| 205 | 'data' => $value, |
||
| 206 | 'tag' => 'PaymentMeans', |
||
| 207 | 'comments' => $bolComments, |
||
| 208 | ]); |
||
| 209 | } |
||
| 210 | } else { |
||
| 211 | $this->setHeaderCommonAggregateComponentsOthers([ |
||
| 212 | 'data' => $arrayAggegateComponents['PaymentMeans'], |
||
| 213 | 'tag' => 'PaymentMeans', |
||
| 214 | 'comments' => $bolComments, |
||
| 215 | ]); |
||
| 216 | } |
||
| 217 | $this->setHeaderCommonAggregateComponentsTaxTotal([ |
||
| 218 | 'data' => $arrayAggegateComponents['TaxTotal'], |
||
| 219 | 'tag' => 'TaxTotal', |
||
| 220 | 'comments' => $bolComments, |
||
| 221 | ]); |
||
| 222 | $this->setHeaderCommonAggregateComponentsOthers([ |
||
| 223 | 'data' => $arrayAggegateComponents['LegalMonetaryTotal'], |
||
| 224 | 'tag' => 'LegalMonetaryTotal', |
||
| 225 | 'comments' => $bolComments, |
||
| 226 | ]); |
||
| 227 | // multiple Lines |
||
| 228 | /* foreach ($arrayData['Lines'] as $key => $value) { |
||
| 229 | $this->setHeaderCommonAggregateComponentsOthers([ |
||
| 230 | 'data' => $value, |
||
| 231 | 'tagForComments' => 'Lines', |
||
| 232 | 'tag' => $arrayData['DocumentTagName'] . 'Line', |
||
| 233 | 'comments' => $bolComments, |
||
| 234 | ]); |
||
| 235 | } */ |
||
| 236 | $this->objXmlWriter->endElement(); // Invoice or CreditNote |
||
| 237 | $this->objXmlWriter->flush(); |
||
| 238 | } |
||
| 239 | |||
| 240 | private function setHeaderCommonAggregateComponentsOthers(array $arrayParameters): void { |
||
| 303 | } |
||
| 304 | } |
||
| 305 |