JSONConfDriver::parseConfigurationFile()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
namespace GeekLab\Conf\Driver;
4
5
use JsonException;
6
7
final class JSONConfDriver implements ConfDriverInterface
8
{
9
    private string $mainConfFile; // Path and file name of the top configuration file.
10
    private string $confLocation; // Path of the configuration files.
11
12
    /**
13
     * JSONConfDriver constructor.
14
     *
15
     * @param string $mainConfFile
16
     * @param string $confLocation
17
     */
18 2
    public function __construct(string $mainConfFile, string $confLocation)
19
    {
20 2
        $this->mainConfFile = $mainConfFile;
21 2
        $this->confLocation = $confLocation;
22
    }
23
24
    /**
25
     * Load and parse a configuration file and return an array.
26
     *
27
     * @param string | null $file If null, then load the main configuration file
28
     *
29
     * @return array
30
     * @throws JsonException
31
     */
32 2
    public function parseConfigurationFile(?string $file = null): array
33
    {
34 2
        $fileName = $file === null ? $this->mainConfFile : $this->confLocation . $file . '.json';
35 2
        $fileContents = file_get_contents($fileName);
36
37 2
        return !empty($fileContents) ? json_decode($fileContents, true, 512, JSON_THROW_ON_ERROR) : [];
38
    }
39
}
40