Passed
Push — main ( 985c98...41eab8 )
by Daniel
02:41
created

TraitHeader::getHeader()   B

Complexity

Conditions 9
Paths 192

Size

Total Lines 57
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 42
c 1
b 0
f 0
nc 192
nop 1
dl 0
loc 57
rs 7.0791

How to fix   Long Method   

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
trait TraitHeader
32
{
33
34
    use TraitCompanies,
35
        TraitTax;
36
37
    private function getHeader(array $arrayParams): array
38
    {
39
        $arrayCBC      = explode(':', $arrayParams['DocumentNameSpaces']['cbc']);
40
        $strCBC        = $arrayCBC[count($arrayCBC) - 1]; // CommonBasicComponents
41
        $strCAC        = $arrayParams['cacName']; // CommonAggregateComponents
42
        $arrayDocument = [
43
            $strCBC => $this->getHeaderCommonBasicComponents($arrayParams['DocumentTagName'], $arrayParams['CBC']),
44
            $strCAC => [
45
                'AccountingCustomerParty' => $this->getAccountingCustomerParty($arrayParams['CAC']
46
                    ->AccountingCustomerParty->children('cac', true)->Party),
47
                'AccountingSupplierParty' => $this->getAccountingSupplierParty($arrayParams['CAC']
48
                    ->AccountingSupplierParty->children('cac', true)->Party),
49
                'LegalMonetaryTotal'      => $this->getLegalMonetaryTotal($arrayParams['CAC']->LegalMonetaryTotal),
50
                'TaxTotal'                => $this->getTaxTotal($arrayParams['CAC']->TaxTotal),
51
            ],
52
        ];
53
        // optional components =========================================================================================
54
        if (isset($arrayParams['CAC']->PaymentMeans)) {
55
            $arrayDocument[$strCAC]['PaymentMeans'] = $this->getMultipleElements($arrayParams['CAC']->PaymentMeans);
56
        }
57
        if (isset($arrayParams['CAC']->PaymentTerms)) {
58
            $arrayDocument[$strCAC]['PaymentTerms']['Note'] = $arrayParams['CAC']
59
                ->PaymentTerms->children('cbc', true)->Note->__toString();
60
        }
61
        if (isset($arrayParams['CAC']->InvoicePeriod)) {
62
            $arrayDocument[$strCAC]['InvoicePeriod'] = $this->getLegalInvoicePeriod($arrayParams['CAC']->InvoicePeriod);
63
        }
64
        if (isset($arrayParams['CAC']->ContractDocumentReference)) {
65
            $arrayDocument[$strCAC]['ContractDocumentReference']['ID'] = $arrayParams['CAC']
66
                ->ContractDocumentReference->children('cbc', true)->ID->__toString();
67
        }
68
        if (isset($arrayParams['CAC']->OrderReference)) {
69
            $arrayDocument[$strCAC]['OrderReference']['ID'] = $arrayParams['CAC']
70
                ->OrderReference->children('cbc', true)->ID->__toString();
71
        }
72
        if (isset($arrayParams['CAC']->AdditionalDocumentReference)) {
73
            $arrayDocument[$strCAC]['AdditionalDocumentReference'] = $this->getMultipleElementsStandard($arrayParams['CAC']
74
                ->AdditionalDocumentReference);
75
        }
76
        if (isset($arrayParams['CAC']->Delivery)) {
77
            $strEl                                                             = $arrayParams['CAC']->Delivery;
78
            $strElement                                                        = $strEl->children('cac', true)
79
                ->DeliveryLocation->children('cac', true)->Address;
80
            $arrayDocument[$strCAC]['Delivery']['DeliveryLocation']['Address'] = [
81
                'StreetName' => $strElement->children('cbc', true)->StreetName->__toString(),
82
                'CityName'   => $strElement->children('cbc', true)->CityName->__toString(),
83
                'PostalZone' => $strElement->children('cbc', true)->PostalZone->__toString(),
84
                'Country'    => [
85
                    'IdentificationCode' => $strElement->children('cac', true)->Country->children('cbc', true)->IdentificationCode->__toString(),
86
                ],
87
            ];
88
            if (isset($strEl->children('cbc', true)->ActualDeliveryDate)) {
89
                $arrayDocument[$strCAC]['Delivery']['ActualDeliveryDate'] = $strEl
90
                        ->children('cbc', true)->ActualDeliveryDate->__toString();
91
            }
92
        }
93
        return $arrayDocument;
94
    }
95
96
    private function getHeaderCommonBasicComponents(string $strType, $objCommonBasicComponents): array
97
    {
98
        $arrayOutput = [
99
            'CustomizationID'      => $objCommonBasicComponents->CustomizationID->__toString(),
100
            'DocumentCurrencyCode' => $objCommonBasicComponents->DocumentCurrencyCode->__toString(),
101
            'ID'                   => $objCommonBasicComponents->ID->__toString(),
102
            'IssueDate'            => $objCommonBasicComponents->IssueDate->__toString(),
103
        ];
104
        if (isset($objCommonBasicComponents->DueDate)) {
105
            $arrayOutput['DueDate'] = $objCommonBasicComponents->DueDate->__toString();
106
        }
107
        if (isset($objCommonBasicComponents->Note)) {
108
            $arrayOutput['Note'] = $objCommonBasicComponents->Note->__toString();
109
        }
110
        if (isset($objCommonBasicComponents->TaxPointDate)) {
111
            $arrayOutput['TaxPointDate'] = $objCommonBasicComponents->TaxPointDate->__toString();
112
        }
113
        if (isset($objCommonBasicComponents->UBLVersionID)) {
114
            $arrayOutput['UBLVersionID'] = $objCommonBasicComponents->UBLVersionID->__toString();
115
        }
116
        return array_merge($arrayOutput, $this->getHeaderTypeCode($strType, $objCommonBasicComponents));
117
    }
118
119
    private function getHeaderTypeCode(string $strType, $objCommonBasicComponents): array
120
    {
121
        $arrayOutput = [];
122
        switch ($strType) {
123
            case 'CreditNote':
124
                $arrayOutput['CreditNoteTypeCode'] = (integer) $objCommonBasicComponents
125
                    ->CreditNoteTypeCode->__toString();
126
                break;
127
            case 'Invoice':
128
                $arrayOutput['InvoiceTypeCode']    = (integer) $objCommonBasicComponents
129
                    ->InvoiceTypeCode->__toString();
130
                break;
131
        }
132
        return $arrayOutput;
133
    }
134
135
    private function getLegalInvoicePeriod($child2): array
136
    {
137
        $arrayOutput = [];
138
        if (isset($child2->children('cbc', true)->StartDate)) {
139
            $arrayOutput['StartDate'] = $child2->children('cbc', true)->StartDate->__toString();
140
        }
141
        if (isset($child2->children('cbc', true)->EndDate)) {
142
            $arrayOutput['EndDate'] = $child2->children('cbc', true)->EndDate->__toString();
143
        }
144
        return $arrayOutput;
145
    }
146
147
    private function getLegalMonetaryTotal($child2): array
148
    {
149
        $objCBC      = $child2->children('cbc', true);
150
        $arrayOutput = [
151
            'LineExtensionAmount' => $this->getTagWithCurrencyParameter($objCBC->LineExtensionAmount),
152
            'TaxExclusiveAmount'  => $this->getTagWithCurrencyParameter($objCBC->TaxExclusiveAmount),
153
            'TaxInclusiveAmount'  => $this->getTagWithCurrencyParameter($objCBC->TaxInclusiveAmount),
154
            'PayableAmount'       => $this->getTagWithCurrencyParameter($objCBC->PayableAmount),
155
        ];
156
        // optional components =========================================================================================
157
        if (isset($child2->children('cbc', true)->AllowanceTotalAmount)) {
158
            $arrayOutput['AllowanceTotalAmount'] = $this->getTagWithCurrencyParameter($objCBC->AllowanceTotalAmount);
159
        }
160
        if (isset($child2->children('cbc', true)->ChargeTotalAmount)) {
161
            $arrayOutput['ChargeTotalAmount'] = $this->getTagWithCurrencyParameter($objCBC->ChargeTotalAmount);
162
        }
163
        if (isset($child2->children('cbc', true)->PayableRoundingAmount)) {
164
            $arrayOutput['PayableRoundingAmount'] = $this->getTagWithCurrencyParameter($objCBC->PayableRoundingAmount);
165
        }
166
        if (isset($child2->children('cbc', true)->PrepaidAmount)) {
167
            $arrayOutput['PrepaidAmount'] = $this->getTagWithCurrencyParameter($objCBC->PrepaidAmount);
168
        }
169
        return $arrayOutput;
170
    }
171
172
    private function getMultipleElements(array|\SimpleXMLElement $arrayIn): array
173
    {
174
        $arrayToReturn = [];
175
        $intLineNo     = 0;
176
        foreach ($arrayIn as $child) {
177
            $intLineNo++;
178
            $intLineStr                 = ($intLineNo < 10 ? '0' : '') . $intLineNo;
179
            $arrayToReturn[$intLineStr] = $this->getPaymentMeans($child);
180
        }
181
        return $arrayToReturn;
182
    }
183
184
    private function getMultipleElementsStandard(array|\SimpleXMLElement $arrayIn): array
185
    {
186
        $arrayToReturn = [];
187
        $intLineNo     = 0;
188
        foreach ($arrayIn as $child) {
189
            $intLineNo++;
190
            $intLineStr = ($intLineNo < 10 ? '0' : '') . $intLineNo;
191
            foreach ($child->children('cbc', true) as $key2 => $value2) {
192
                $arrayToReturn[$intLineStr][$key2] = $value2;
193
            }
194
        }
195
        return $arrayToReturn;
196
    }
197
}
198