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

JSONConfDriver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 39
ccs 8
cts 8
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseConfigurationFile() 0 6 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