JsonConfigParser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 4
cp 0.75
crap 1.0156
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Config\Parser;
4
5
use Venta\Contracts\Config\ConfigFileParser;
6
use Venta\Contracts\Config\ConfigStringParser;
7
use Venta\Contracts\Filesystem\Filesystem;
8
9
/**
10
 * Class JsonConfigParser
11
 *
12
 * @package Venta\Config\Parser
13
 */
14
class JsonConfigParser implements ConfigFileParser, ConfigStringParser
15
{
16
    /**
17
     * @var Filesystem
18
     */
19
    private $filesystem;
20
21
    /**
22
     * Construct function.
23
     *
24
     * @param Filesystem $filesystem
25
     */
26 3
    public function __construct(Filesystem $filesystem)
27
    {
28 3
        $this->filesystem = $filesystem;
29 3
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34 1
    public function parseFile(string $filename): array
35
    {
36 1
        return $this->parseString($this->filesystem->read($filename));
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 2
    public function parseString(string $configuration): array
43
    {
44 2
        $array = json_decode($configuration, true);
45 2
        if (json_last_error() !== JSON_ERROR_NONE) {
46 1
            throw new \RuntimeException(
47 1
                sprintf('Unable to parse configuration string: "%s".', json_last_error_msg()),
48
                json_last_error()
49
            );
50
        }
51
52 2
        return $array;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58 1
    public function supportedExtensions(): array
59
    {
60 1
        return ['json'];
61
    }
62
}