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

TraitBasic   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 46
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTagWithUnitCodeParameter() 0 5 1
A getJsonFromFile() 0 17 4
A getTagWithCurrencyParameter() 0 5 1
A getTagWithCurrencyParameterAsString() 0 5 1
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 TraitBasic
32
{
33
34
    protected array $arraySettings = [];
35
36
    private function getTagWithCurrencyParameter($childLineExtensionAmount): array
37
    {
38
        return [
39
            'currencyID' => $childLineExtensionAmount->attributes()->currencyID->__toString(),
40
            'value'      => (float) $childLineExtensionAmount->__toString(),
41
        ];
42
    }
43
44
    private function getTagWithCurrencyParameterAsString($childLineExtensionAmount): array
45
    {
46
        return [
47
            'currencyID' => $childLineExtensionAmount->attributes()->currencyID->__toString(),
48
            'value'      => $childLineExtensionAmount->__toString(),
49
        ];
50
    }
51
52
    private function getTagWithUnitCodeParameter($childLineExtensionAmount): array
53
    {
54
        return [
55
            'unitCode' => $childLineExtensionAmount->attributes()->unitCode->__toString(),
56
            'value'    => (float) $childLineExtensionAmount->__toString(),
57
        ];
58
    }
59
60
    private function getJsonFromFile(string $strFileName): array
61
    {
62
        $strFileName = __DIR__ . DIRECTORY_SEPARATOR . $strFileName;
63
        if (!file_exists($strFileName)) {
64
            throw new \RuntimeException(sprintf('File %s does not exists!', $strFileName));
65
        }
66
        $fileHandle = fopen($strFileName, 'r');
67
        if ($fileHandle === false) {
68
            throw new \RuntimeException(sprintf('Unable to open file %s for read purpose!', $strFileName));
69
        }
70
        $fileContent   = fread($fileHandle, ((int) filesize($strFileName)));
71
        fclose($fileHandle);
72
        $arrayToReturn = json_decode($fileContent, true);
73
        if (json_last_error() != JSON_ERROR_NONE) {
74
            throw new \RuntimeException(sprintf('Unable to interpret JSON from %s file...', $strFileName));
75
        }
76
        return $arrayToReturn;
77
    }
78
}
79