Passed
Push — main ( a8d720...b18f8c )
by Daniel
02:10
created

TraitVersions::getDefaultsIntoDataSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 19
rs 9.7998
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 TraitVersions
32
{
33
34
    protected $arraySettings = [];
35
36
    private function establishCurrentVersion(array $arrayKnownVersions): array {
37
        $arrayVersionToReturn = [];
38
        foreach ($arrayKnownVersions as $value) {
39
            $dtValidityStart = new \DateTime($value['Validity']['Start']);
40
            $dtValidityEnd   = new \DateTime($value['Validity']['End']);
41
            $dtValidityNow   = new \DateTime();
42
            if (($dtValidityNow >= $dtValidityStart) && ($dtValidityNow <= $dtValidityEnd)) {
43
                $arrayVersionToReturn = [
44
                    'UBL'     => $value['UBL'],
45
                    'CIUS-RO' => $value['CIUS-RO'],
46
                ];
47
            }
48
        }
49
        return $arrayVersionToReturn;
50
    }
51
52
    private function getDefaultsIntoDataSet(array $arrayDocumentData): array {
53
        $arrayOutput = [];
54
        if (!array_key_exists('DocumentNameSpaces', $arrayDocumentData)) {
55
            $arrayVersions = $this->establishCurrentVersion($this->arraySettings['Versions']);
56
            $arrayOutput   = [
57
                'Root'    => [
58
                    'DocumentNameSpaces' => $this->arraySettings['Defaults']['DocumentNameSpaces'],
59
                    'SchemaLocation'     => vsprintf($this->arraySettings['Defaults']['SchemaLocation'], [
60
                        $arrayDocumentData['DocumentTagName'],
61
                        $arrayVersions['UBL'],
62
                        $arrayDocumentData['DocumentTagName'],
63
                        $arrayVersions['UBL'],
64
                    ]),
65
                ],
66
                'UBL'     => $arrayVersions['UBL'],
67
                'CIUS-RO' => $arrayVersions['CIUS-RO'],
68
            ];
69
        }
70
        return $arrayOutput;
71
    }
72
73
    private function getSettingsFromFileIntoMemory(): void {
74
        $strFileName = __DIR__ . DIRECTORY_SEPARATOR . 'eFactura.json';
75
        if (!file_exists($strFileName)) {
76
            throw new \RuntimeException(sprintf('File %s does not exists!', $strFileName));
77
        }
78
        $fileHandle = fopen($strFileName, 'r');
79
        if ($fileHandle === false) {
80
            throw new \RuntimeException(sprintf('Unable to open file %s for read purpose!', $strFileName));
81
        }
82
        $fileContent   = fread($fileHandle, ((int) filesize($strFileName)));
83
        fclose($fileHandle);
84
        $arrayToReturn = json_decode($fileContent, true);
85
        if (json_last_error() != JSON_ERROR_NONE) {
86
            throw new \RuntimeException(sprintf('Unable to interpret JSON from %s file...', $strFileName));
87
        }
88
        $this->arraySettings = $arrayToReturn;
89
    }
90
}
91