Passed
Push — main ( 0ce956...433593 )
by Daniel
02:56
created

ClassElectronicInvoiceUserInterface   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 14
Bugs 0 Features 4
Metric Value
eloc 111
c 14
b 0
f 4
dl 0
loc 186
rs 9.92
wmc 31

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setActionToDo() 0 23 4
A setHtmlFooter() 0 11 1
A setHtmlTable() 0 12 3
A setNumbers() 0 6 1
A setUserInterface() 0 11 1
A setHtmlTableHeader() 0 8 3
A getHeaderColumnMapping() 0 11 3
A getButtonToActionSomething() 0 16 3
A setHtmlTableLine() 0 16 5
A getButtonForLocalisation() 0 13 3
A setLocalization() 0 8 2
A setHtmlHeader() 0 10 1
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 ClassElectronicInvoiceUserInterface
18
{
19
    use \danielgp\efactura\TraitUserInterfaceLogic;
0 ignored issues
show
introduced by
The trait danielgp\efactura\TraitUserInterfaceLogic requires some properties which are not provided by danielgp\efactura\ClassE...nicInvoiceUserInterface: $numFiles, $Index_incarcare, $errorMessage, $Error, $Cif_emitent
Loading history...
20
21
    private \SebastianBergmann\Timer\Timer $classTimer;
22
    private $translation;
23
24
    public function __construct()
25
    {
26
        $this->classTimer = new \SebastianBergmann\Timer\Timer();
27
        $this->classTimer->start();
28
        $this->getConfiguration();
29
        $this->setLocalization();
30
    }
31
32
    private function getButtonForLocalisation(string $strLanguageCountry): string
33
    {
34
        $arrayMapFlags = [
35
            'ro_RO' => 'ro',
36
            'it_IT' => 'it',
37
            'en_US' => 'us',
38
        ];
39
        return vsprintf('<a href="?language_COUNTRY=%s" style="float:left;margin-left:10px;">'
40
            . '<span class="fi fi-%s" style="%s">&nbsp;</span>'
41
            . '</a>', [
42
            $strLanguageCountry . (array_key_exists('action', $_GET) ? '&action=' . $_GET['action'] : ''),
43
            $arrayMapFlags[$strLanguageCountry],
44
            ($strLanguageCountry === $_GET['language_COUNTRY'] ? 'width:40px;height:30px;' : 'width:20px;height:15px;'),
45
        ]);
46
    }
47
48
    private function getButtonToActionSomething(array $arrayButtonFeatures): string
49
    {
50
        $arrayButtonStyle = [
51
            'font:bold 14pt Arial',
52
            'margin:2px',
53
            'padding:4px 10px',
54
        ];
55
        $arrayStylePieces = $arrayButtonStyle;
56
        if (array_key_exists('AdditionalStyle', $arrayButtonFeatures)) {
57
            $arrayStylePieces = array_merge($arrayButtonStyle, $arrayButtonFeatures['AdditionalStyle']);
58
        }
59
        return vsprintf('<a href="%s" class="btn btn-outline-primary" style="%s">%s</a>', [
60
            $arrayButtonFeatures['URL']
61
            . (array_key_exists('language_COUNTRY', $_GET) ? '&language_COUNTRY=' . $_GET['language_COUNTRY'] : ''),
62
            implode(';', $arrayStylePieces),
63
            $arrayButtonFeatures['Text'],
64
        ]);
65
    }
66
67
    private function getHeaderColumnMapping(array $arrayColumns): array
68
    {
69
        $arrayMap = [];
70
        foreach ($arrayColumns as $strColumnName) {
71
            $arrayMap[$strColumnName] = $strColumnName;
72
            $strRelevant              = $this->translation->find(null, 'i18n_Clmn_' . $strColumnName);
73
            if (!is_null($strRelevant)) {
74
                $arrayMap[$strColumnName] = $strRelevant->getTranslation();
75
            }
76
        }
77
        return $arrayMap;
78
    }
79
80
    public function setActionToDo(): void
81
    {
82
        echo '<main>';
83
        $arrayOptions = [
84
            'action' => FILTER_SANITIZE_SPECIAL_CHARS,
85
        ];
86
        $arrayInputs  = filter_input_array(INPUT_GET, $arrayOptions);
87
        if (array_key_exists('action', $arrayInputs)) {
88
            switch ($arrayInputs['action']) {
89
                case 'AnalyzeZIPfromANAFfromLocalFolder':
90
                    $strRelevantFolder = 'P:/eFactura_Responses/Luna_Anterioara_NeDeclarata_Inca/';
91
                    $arrayInvoices     = $this->actionAnalyzeZIPfromANAFfromLocalFolder($strRelevantFolder);
92
                    if (count($arrayInvoices) === 0) {
93
                        echo sprintf('<p style="color:red;">'
94
                            . $this->translation->find(null, 'i18n_Msg_NoZip')->getTranslation()
95
                            . '</p>', $strRelevantFolder);
96
                    } else {
97
                        echo $this->setHtmlTable($arrayInvoices);
98
                    }
99
                    break;
100
            }
101
        }
102
        echo '</main>';
103
    }
104
105
    public function setHtmlFooter(): void
106
    {
107
        $strHtmlContent = implode('', $this->getFileEntireContent(implode(DIRECTORY_SEPARATOR, [
108
                __DIR__,
109
                'HTML',
110
                'footer.html',
111
        ])));
112
        echo vsprintf($strHtmlContent, [
113
            (new \SebastianBergmann\Timer\ResourceUsageFormatter())->resourceUsage($this->classTimer->stop()),
114
            date('Y'),
115
            $this->arrayConfiguration['Application']['Developer'],
116
        ]);
117
    }
118
119
    public function setHtmlHeader(): void
120
    {
121
        $strHtmlContent = implode('', $this->getFileEntireContent(implode(DIRECTORY_SEPARATOR, [
122
                __DIR__,
123
                'HTML',
124
                'header.html',
125
        ])));
126
        echo vsprintf($strHtmlContent, [
127
            $this->arrayConfiguration['Application']['Name'],
128
            $this->arrayConfiguration['Application']['Developer'],
129
        ]);
130
    }
131
132
    public function setHtmlTable(array $arrayData): string
133
    {
134
        $strReturn = '<table style="margin-left:auto;margin-right:auto;">';
135
        foreach ($arrayData as $intLineNo => $arrayContent) {
136
            ksort($arrayContent);
137
            if ($intLineNo === 0) {
138
                $strReturn .= $this->setHtmlTableHeader(array_keys($arrayContent))
139
                    . '<tbody>';
140
            }
141
            $strReturn .= $this->setHtmlTableLine(($intLineNo + 1), $arrayContent);
142
        }
143
        return ($strReturn . '</tbody>' . '</table>');
144
    }
145
146
    private function setHtmlTableHeader(array $arrayData): string
147
    {
148
        $arrayMap    = $this->getHeaderColumnMapping(array_values($arrayData));
149
        $strToReturn = '<th>#</th>';
150
        foreach ($arrayData as $key) {
151
            $strToReturn .= sprintf('<th>%s</th>', (array_key_exists($key, $arrayMap) ? $arrayMap[$key] : $key));
152
        }
153
        return '<thead><tr>' . $strToReturn . '</tr></thead>';
154
    }
155
156
    private function setHtmlTableLine(int $intLineNo, array $arrayLine): string
157
    {
158
        $arrayContent = [];
159
        foreach ($arrayLine as $strColumn => $strValue) {
160
            if (str_starts_with($strColumn, 'Amount_')) {
161
                $arrayContent[] = sprintf('<td style="text-align:right;">%s</td>', $this->setNumbers($strValue, 2, 2));
162
            } elseif (str_starts_with($strColumn, 'Size')) {
163
                $arrayContent[] = sprintf('<td style="text-align:right;">%s</td>', $this->setNumbers($strValue, 0, 0));
164
            } else {
165
                $arrayContent[] = sprintf('<td>%s</td>', $strValue);
166
            }
167
        }
168
        return '<tr' . ($arrayLine['Error'] === '' ? '' : ' style="color:red;"') . '>'
169
            . '<td>' . $intLineNo . '</td>'
170
            . implode('', $arrayContent)
171
            . '</tr>';
172
    }
173
174
    private function setLocalization(): void
175
    {
176
        if (!array_key_exists('language_COUNTRY', $_GET)) {
177
            $_GET['language_COUNTRY'] = 'ro_RO';
178
        }
179
        $loader            = new \Gettext\Loader\PoLoader();
180
        $this->translation = $loader->loadFile(__DIR__ . '/locale/' . $_GET['language_COUNTRY']
181
            . '/LC_MESSAGES/eFactura.po');
182
    }
183
184
    private function setNumbers(float $floatNumber, int $intMinDigits, int $intMaxDigits): string
185
    {
186
        $classFormat = new \NumberFormatter($_GET['language_COUNTRY'], \NumberFormatter::DECIMAL);
187
        $classFormat->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $intMinDigits);
188
        $classFormat->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $intMaxDigits);
189
        return $classFormat->format($floatNumber);
190
    }
191
192
    public function setUserInterface(): void
193
    {
194
        echo '<header class="border-bottom">'
195
        . $this->getButtonForLocalisation('en_US')
196
        . $this->getButtonForLocalisation('it_IT')
197
        . $this->getButtonForLocalisation('ro_RO')
198
        . $this->getButtonToActionSomething([
199
            'Text' => $this->translation->find(null, 'i18n_Btn_AnalyzeZIP')->getTranslation(),
200
            'URL'  => '?action=AnalyzeZIPfromANAFfromLocalFolder',
201
        ])
202
        . ' </header>';
203
    }
204
}
205