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