Passed
Push — main ( 414ba2...ff44d7 )
by Daniel
02:12
created

traitVersions::establishCurrentVersion()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 9.9332
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 getSettingsFromFileIntoMemory(): void {
53
        $strFileName = __DIR__ . DIRECTORY_SEPARATOR . 'eFactura.json';
54
        if (!file_exists($strFileName)) {
55
            throw new \RuntimeException(sprintf('File %s does not exists!', $strFileName));
56
        }
57
        $fileHandle = fopen($strFileName, 'r', 'read');
0 ignored issues
show
Bug introduced by
'read' of type string is incompatible with the type boolean expected by parameter $use_include_path of fopen(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $fileHandle = fopen($strFileName, 'r', /** @scrutinizer ignore-type */ 'read');
Loading history...
58
        if ($fileHandle === false) {
59
            throw new \RuntimeException(sprintf('Unable to open file %s for read purpose!', $strFileName));
60
        }
61
        $fileContent   = fread($fileHandle, ((int) filesize($strFileName)));
62
        fclose($fileHandle);
63
        $arrayToReturn = json_decode($fileContent, true);
64
        if (json_last_error() != JSON_ERROR_NONE) {
65
            throw new \RuntimeException(sprintf('Unable to interpret JSON from %s file...', $strFileName));
66
        }
67
        $this->arraySettings = $arrayToReturn;
68
    }
69
}
70