Passed
Push — release ( 459660...233a99 )
by Kenneth
03:35 queued 59s
created

JSONConfDriver::parseConfigurationFile()   A

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
final class JSONConfDriver implements ConfDriverInterface
6
{
7
    /** @var string $mainConfFile Path and file name of the top configuration file. */
8
    private string $mainConfFile;
9
10
    /** @var string $confLocation Path of the configuration files. */
11
    private string $confLocation;
12
13
    /**
14
     * JSONConfDriver constructor.
15
     *
16
     * @param string $mainConfFile
17
     * @param string $confLocation
18
     */
19 2
    public function __construct(string $mainConfFile, string $confLocation)
20
    {
21 2
        $this->mainConfFile = $mainConfFile;
22 2
        $this->confLocation = $confLocation;
23 2
    }
24
25
    /**
26
     * Load and parse a configuration file and return an array.
27
     *
28
     * @param string | null $file If null, then load the main configuration file
29
     *
30
     * @return array
31
     */
32
    /**
33
     * @param string | null $file
34
     *
35
     * @return array
36
     * @throws \JsonException
37
     */
38 2
    public function parseConfigurationFile(?string $file = null): array
39
    {
40 2
        $fileName = $file === null ? $this->mainConfFile : $this->confLocation . $file . '.json';
41 2
        $fileContents = file_get_contents($fileName);
42
43 2
        return !empty($fileContents) ? json_decode($fileContents, true, 512, JSON_THROW_ON_ERROR) : [];
44
    }
45
}
46