Total Complexity | 46 |
Total Lines | 289 |
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']; |
||
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 | 'subTag' => 'Party', |
||
195 | 'comments' => $bolComments, |
||
196 | ]); |
||
197 | $this->setHeaderCommonAggregateComponentsCompanies([ |
||
198 | 'data' => $arrayAggegateComponents['AccountingCustomerParty']['Party'], |
||
199 | 'tag' => 'AccountingCustomerParty', |
||
200 | 'subTag' => 'Party', |
||
201 | 'comments' => $bolComments, |
||
202 | ]); |
||
203 | // multiple accounts can be specified within PaymentMeans |
||
204 | foreach ($arrayAggegateComponents['PaymentMeans'] as $value) { |
||
205 | $this->setHeaderCommonAggregateComponentsOrdered([ |
||
206 | 'data' => $value, |
||
207 | 'tag' => 'PaymentMeans', |
||
208 | 'comments' => $bolComments, |
||
209 | ]); |
||
210 | } |
||
211 | $this->setHeaderCommonAggregateComponentsTaxTotal([ |
||
212 | 'data' => $arrayAggegateComponents['TaxTotal'], |
||
213 | 'tag' => 'TaxTotal', |
||
214 | 'comments' => $bolComments, |
||
215 | ]); |
||
216 | $this->setHeaderCommonAggregateComponentsOthers([ |
||
217 | 'data' => $arrayAggegateComponents['LegalMonetaryTotal'], |
||
218 | 'tag' => 'LegalMonetaryTotal', |
||
219 | 'comments' => $bolComments, |
||
220 | ]); |
||
221 | // multiple Lines |
||
222 | /* foreach ($arrayData['Lines'] as $key => $value) { |
||
223 | $this->setHeaderCommonAggregateComponentsOthers([ |
||
224 | 'data' => $value, |
||
225 | 'tagForComments' => 'Lines', |
||
226 | 'tag' => $arrayData['DocumentTagName'] . 'Line', |
||
227 | 'comments' => $bolComments, |
||
228 | ]); |
||
229 | } */ |
||
230 | $this->objXmlWriter->endElement(); // Invoice or CreditNote |
||
231 | $this->objXmlWriter->flush(); |
||
232 | } |
||
233 | |||
234 | private function setHeaderCommonAggregateComponentsOrdered(array $arrayParameters): void { |
||
235 | $key = $arrayParameters['tag']; |
||
236 | $this->setElementComment($key, 'CAC', $arrayParameters['comments']); |
||
237 | $this->objXmlWriter->startElement('cac:' . $key); |
||
238 | $arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$key]; |
||
239 | foreach ($arrayCustomOrder as $value) { |
||
240 | if (array_key_exists($value, $arrayParameters['data'])) { |
||
241 | $this->setElementComment(implode('_', [$key, $value]), 'CAC', $arrayParameters['comments']); |
||
242 | if (is_array($arrayParameters['data'][$value])) { |
||
243 | $this->objXmlWriter->startElement('cac:' . $value); |
||
244 | foreach ($arrayParameters['data'][$value] as $key2 => $value2) { |
||
245 | $this->setElementComment(implode('_', [$key, $value, $key2]), 'CAC', $arrayParameters['comments']); |
||
246 | $this->objXmlWriter->writeElement('cbc:' . $key2, $value2); |
||
247 | } |
||
248 | $this->objXmlWriter->endElement(); // $value |
||
249 | } else { |
||
250 | $this->objXmlWriter->writeElement('cbc:' . $value, $arrayParameters['data'][$value]); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | $this->objXmlWriter->endElement(); // $key |
||
255 | } |
||
256 | |||
257 | private function setHeaderCommonAggregateComponentsOthers(array $arrayParameters): void { |
||
320 | } |
||
321 | } |
||
322 |