Passed
Push — main ( 1cc6a3...3edd16 )
by Daniel
02:07
created

setHeaderCommonAggregateComponentsOthers()   C

Complexity

Conditions 13
Paths 10

Size

Total Lines 63
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 53
c 0
b 0
f 0
nc 10
nop 1
dl 0
loc 63
rs 6.6166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2024 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\efactura;
30
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 {
118
        $key = $arrayParameters['tag'];
119
        $this->setElementComment($key, 'CAC', $arrayParameters['comments']);
120
        $this->objXmlWriter->startElement('cac:' . $key);
121
        $this->setElementComment(implode('_', [$key, 'TaxAmount']), 'CAC', $arrayParameters['comments']);
122
        $this->objXmlWriter->startElement('cbc:TaxAmount');
123
        $this->objXmlWriter->writeAttribute('currencyID', $arrayParameters['data']['TaxAmount']['currencyID']);
124
        $this->objXmlWriter->writeRaw($arrayParameters['data']['TaxAmount']['value']);
125
        $this->objXmlWriter->endElement(); // TaxAmount
126
        foreach ($arrayParameters['data']['TaxSubtotal'] as $value) {
127
            $this->objXmlWriter->startElement('cac:TaxSubtotal');
128
            $this->setElementComment(implode('_', [$key, 'TaxSubtotal', 'TaxableAmount']), 'CAC', $arrayParameters['comments']);
129
            $this->objXmlWriter->startElement('cbc:TaxableAmount');
130
            $this->objXmlWriter->writeAttribute('currencyID', $value['TaxableAmount']['currencyID']);
131
            $this->objXmlWriter->writeRaw($value['TaxableAmount']['value']);
132
            $this->objXmlWriter->endElement(); // TaxableAmount
133
            $this->setElementComment(implode('_', [$key, 'TaxSubtotal', 'TaxAmount']), 'CAC', $arrayParameters['comments']);
134
            $this->objXmlWriter->startElement('cbc:TaxAmount');
135
            $this->objXmlWriter->writeAttribute('currencyID', $value['TaxAmount']['currencyID']);
136
            $this->objXmlWriter->writeRaw($value['TaxAmount']['value']);
137
            $this->objXmlWriter->endElement(); // TaxAmount
138
            $this->setTaxCategory([
139
                'comments'         => $arrayParameters['comments'],
140
                'commentParentKey' => implode('_', [$key, 'TaxSubtotal', 'TaxCategory']),
141
                'data'             => $value['TaxCategory'],
142
                'tag'              => 'TaxCategory',
143
            ]);
144
            $this->objXmlWriter->endElement(); // TaxSubtotal
145
        }
146
        $this->objXmlWriter->endElement(); // $key
147
    }
148
149
    private function setHeaderCommonBasicComponents(array $arrayElementWithData, bool $includeComments): void {
150
        $arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header']['CBC'];
151
        foreach ($arrayCustomOrdered as $value) {
152
            $this->setElementComment($value, 'CBC', $includeComments);
153
            $this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]);
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
            '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 {
258
        if (array_key_exists('tagForComments', $arrayParameters)) {
259
            $key = $arrayParameters['tagForComments'];
260
        } else {
261
            $key = $arrayParameters['tag'];
262
        }
263
        $this->setElementComment($key, 'CAC', $arrayParameters['comments']);
264
        $this->objXmlWriter->startElement('cac:' . $arrayParameters['tag']);
265
        foreach ($arrayParameters['data'] as $key2 => $value2) {
266
            $this->setElementComment(implode('_', [$key, $key2]), 'CAC', $arrayParameters['comments']);
267
            if (is_array($value2)) {
268
                if (substr($key2, -6) === 'Amount') {
269
                    $this->objXmlWriter->startElement('cbc:' . $key2);
270
                    $this->objXmlWriter->writeAttribute('currencyID', $value2['currencyID']);
271
                    $this->objXmlWriter->writeRaw($value2['value']);
272
                } elseif (substr($key2, -8) === 'Quantity') {
273
                    $this->objXmlWriter->startElement('cbc:' . $key2);
274
                    $this->objXmlWriter->writeAttribute('unitCode', $value2['unitCode']);
275
                    $this->objXmlWriter->writeRaw($value2['value']);
276
                } else {
277
                    $this->objXmlWriter->startElement('cac:' . $key2);
278
                    foreach ($value2 as $key3 => $value3) {
279
                        $this->setElementComment(implode('_', [
280
                            $key, $key2, $key3]), 'CAC', $arrayParameters['comments']);
281
                        if (substr($key3, -6) === 'Amount') {
282
                            $this->objXmlWriter->startElement('cbc:' . $key3);
283
                            $this->objXmlWriter->writeAttribute('currencyID', $value3['currencyID']);
284
                            $this->objXmlWriter->writeRaw($value3['value']);
285
                            $this->objXmlWriter->endElement(); // $key3
286
                        } elseif (substr($key3, -8) === 'Quantity') {
287
                            $this->objXmlWriter->startElement('cbc:' . $key3);
288
                            $this->objXmlWriter->writeAttribute('unitCode', $value3['unitCode']);
289
                            $this->objXmlWriter->writeRaw($value3['value']);
290
                            $this->objXmlWriter->endElement(); // $key3
291
                        } elseif (is_array($value3)) {
292
                            $this->objXmlWriter->startElement('cac:' . $key3);
293
                            foreach ($value3 as $key4 => $value4) {
294
                                $this->setElementComment(implode('_', [
295
                                    $key, $key2, $key3, $key4]), 'CAC', $arrayParameters['comments']);
296
                                if (is_array($value4)) {
297
                                    $this->objXmlWriter->startElement('cac:' . $key4);
298
                                    foreach ($value4 as $key5 => $value5) {
299
                                        $this->setElementComment(implode('_', [
300
                                            $key, $key2, $key3, $key4, $key5]), 'CAC', $arrayParameters['comments']);
301
                                        $this->objXmlWriter->writeElement('cbc:' . $key5, $value5);
302
                                    }
303
                                    $this->objXmlWriter->endElement(); // $key4
304
                                } else {
305
                                    $this->objXmlWriter->writeElement('cbc:' . $key4, $value4);
306
                                }
307
                            }
308
                            $this->objXmlWriter->endElement(); // $key3
309
                        } else {
310
                            $this->objXmlWriter->writeElement('cbc:' . $key3, $value3);
311
                        }
312
                    }
313
                }
314
                $this->objXmlWriter->endElement(); // $key2
315
            } else {
316
                $this->objXmlWriter->writeElement('cbc:' . $key2, $value2);
317
            }
318
        }
319
        $this->objXmlWriter->endElement(); // $key
320
    }
321
}
322