Passed
Push — main ( 39d695...502311 )
by Daniel
02:55
created

ClassElectronicInvoiceRead::setArrayProcessing()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 14
ccs 12
cts 12
cp 1
crap 6
rs 9.2222
1
<?php
2
3
/*
4
 * Copyright (c) 2024, Daniel Popiniuc and its licensors.
5
 *
6
 * All rights reserved. This program and the accompanying materials
7
 * are made available under the terms of the Eclipse Public License v1.0
8
 * which accompanies this distribution, and is available at
9
 * http://www.eclipse.org/legal/epl-v20.html
10
 *
11
 * Contributors:
12
 *    Daniel Popiniuc
13
 */
14
15
namespace danielgp\efactura;
16
17
class ClassElectronicInvoiceRead
18
{
19
    use TraitBasic;
20
    use TraitTax;
21
    use TraitLines;
22
23 13
    private function getAccountingCustomerOrSupplierParty(array $arrayIn): array
24
    {
25 13
        $arrayOut = [];
26 13
        foreach ($this->arraySettings['CustomOrder'][$arrayIn['type']] as $strElement) {
27 13
            if (isset($arrayIn['data']->children($this->arrayProcessing['mapping']['cac'], true)->$strElement)) {
28 13
                if ($strElement === 'PartyTaxScheme') {
29 12
                    $arrayOut[$strElement] = $this->getMultipleElementsByKey($arrayIn['data']
30 12
                            ->children($this->arrayProcessing['mapping']['cac'], true)->$strElement);
31
                } else {
32 13
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']
33 13
                            ->children($this->arrayProcessing['mapping']['cac'], true)->$strElement);
34
                }
35
            }
36 13
            if (isset($arrayIn['data']->children($this->arrayProcessing['mapping']['cbc'], true)->$strElement)) {
37 3
                if ($strElement === 'EndpointID') {
38 3
                    $arrayOut['EndpointID'] = [
39 3
                        'schemeID' => $arrayIn['data']
40 3
                            ->children($this->arrayProcessing['mapping']['cbc'], true)->EndpointID
41 3
                            ->attributes()->schemeID->__toString(),
42 3
                        'value'    => $arrayIn['data']
43 3
                            ->children($this->arrayProcessing['mapping']['cbc'], true)->EndpointID
44 3
                            ->__toString(),
45 3
                    ];
46
                } else {
47
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']
48
                            ->children($this->arrayProcessing['mapping']['cbc'], true)->$strElement);
49
                }
50
            }
51
        }
52 13
        return $arrayOut;
53
    }
54
55 13
    private function getBasicOrAggregateKey(array $arrayDocNmSp, string $strBasOrAggr): string
56
    {
57 13
        $arrayPieces = explode(':', $arrayDocNmSp[$strBasOrAggr]);
58 13
        return $arrayPieces[count($arrayPieces) - 1];
59
    }
60
61 13
    private function getDocumentRoot(object $objFile): array
62
    {
63 13
        $arrayDocument = [
64 13
            'DocumentTagName'    => $objFile->getName(),
65 13
            'DocumentNameSpaces' => $objFile->getDocNamespaces(true),
66 13
        ];
67 13
        if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) {
68 11
            if (isset($objFile->attributes('xsi', true)->schemaLocation)) {
69 10
                $arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation;
70
            }
71
        }
72 13
        return $arrayDocument;
73
    }
74
75 13
    private function getElementsOrdered(array $arrayDataIn): array
76
    {
77 13
        $arrayOutput = [];
78 13
        foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) {
79 13
            if (isset($arrayDataIn['data']->$value)) {
80 13
                $arrayOutput[$value] = $arrayDataIn['data']->$value->__toString();
81
            }
82
        }
83 13
        return $arrayOutput;
84
    }
85
86 13
    private function getHeader(array $arrayParams): array
87
    {
88 13
        $arrayDocument = [
89 13
            'TaxTotal' => $this->getTax($arrayParams['CAC']->TaxTotal),
90 13
        ];
91 13
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
92 13
            if (isset($arrayParams['CAC']->$key)) {
93 13
                $arrayDocument[$key] = $this->getHeaderComponents($arrayParams, $key, $value);
94
            }
95
        }
96 13
        return $arrayDocument;
97
    }
98
99 13
    private function getHeaderComponents(array $arrayParams, string $key, string $value): array | string
100
    {
101 13
        $arrayDocument = [];
102 13
        if ($value === 'SingleCompany') {
103 13
            $arrayDocument = [
104 13
                'Party' => $this->getAccountingCustomerOrSupplierParty([
105 13
                    'data' => $arrayParams['CAC']->$key
106 13
                        ->children($this->arrayProcessing['mapping']['cac'], true)->Party,
107 13
                    'type' => $key,
108 13
                ])
109 13
            ];
110
        } else {
111 13
            $arrayMapping  = [
112 13
                'Multiple'                  => [
113 13
                    'getMultipleElementsByKey'
114 13
                    , $arrayParams['data']->children('cac', true)->$key
115 13
                ],
116 13
                'MultipleStandard'          => ['getMultipleElementsStandard', $arrayParams['CAC']->$key],
117 13
                'Single'                    => ['getElements', $arrayParams['CAC']->$key],
118 13
                'SingleCompanyWithoutParty' => [
119 13
                    'getAccountingCustomerOrSupplierParty'
120 13
                    , [
121 13
                        'data' => $arrayParams['CAC']->$key,
122 13
                        'type' => $key,
123 13
                    ]
124 13
                ],
125 13
            ];
126 13
            $arrayDocument = $this->getRightMethod($arrayMapping[$value][0], $arrayMapping[$value][1]);
127
        }
128 13
        return $arrayDocument;
129
    }
130
131 13
    public function readElectronicInvoice(string $strFile): array
132
    {
133 13
        $this->getProcessingDetails();
134 13
        $this->getHierarchyTagOrder();
135 13
        $flags                   = LIBXML_PARSEHUGE | LIBXML_BIGLINES | LIBXML_NOERROR;
136 13
        $bolIsLocal              = is_file($strFile);
137 13
        $objFile                 = new \SimpleXMLElement($strFile, $flags, $bolIsLocal);
138 13
        $arrayDocument           = $this->getDocumentRoot($objFile);
139 13
        $this->setArrayProcessing($arrayDocument['DocumentNameSpaces']);
140 13
        $strMap                  = $this->arrayProcessing['mapping'];
141 13
        $arrayBasics             = $this->getElementsOrdered([
142 13
            'data'          => $objFile->children($strMap['cbc'], true),
143 13
            'namespace_cbc' => $arrayDocument['DocumentNameSpaces'][$strMap['cbc']],
144 13
        ]);
145 13
        $arrayAggregates         = $this->getHeader([
146 13
            'CAC'  => $objFile->children($strMap['cac'], true),
147 13
            'data' => $objFile,
148 13
        ]);
149 13
        $arrayDocument['Header'] = [
150 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], $strMap['cbc']) => $arrayBasics,
151 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], $strMap['cac']) => $arrayAggregates,
152 13
        ];
153 13
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
154 13
        return $arrayDocument;
155
    }
156
157 13
    private function setArrayProcessing(array $arrayDocumentNameSpaces): void
158
    {
159 13
        $bolMappingsNotSet = [
160 13
            'cac' => true,
161 13
            'cbc' => true,
162 13
        ];
163 13
        foreach ($arrayDocumentNameSpaces as $key => $value) {
164 13
            if (str_ends_with($value, ':CommonAggregateComponents-2') && $bolMappingsNotSet['cac']) {
165 13
                $this->arrayProcessing['mapping']['cac'] = $key;
166 13
                $bolMappingsNotSet['cac']                = false;
167
            }
168 13
            if (str_ends_with($value, ':CommonBasicComponents-2') && $bolMappingsNotSet['cbc']) {
169 13
                $this->arrayProcessing['mapping']['cbc'] = $key;
170 13
                $bolMappingsNotSet['cbc']                = false;
171
            }
172
        }
173
    }
174
}
175