Test Failed
Push — master ( b5ddc7...73c405 )
by Konstantins
05:51
created

Json::fromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Config\Parser;
4
5
use League\Flysystem\FilesystemInterface;
6
use Venta\Contracts\Config\ConfigFileParser;
7
8
/**
9
 * Class Json
10
 *
11
 * @package Venta\Config\Parser
12
 */
13
class Json implements ConfigFileParser
14
{
15
    /**
16
     * @var FilesystemInterface
17
     */
18
    private $filesystem;
19 3
20
    /**
21 3
     * Construct function.
22 3
     *
23 1
     * @param FilesystemInterface $filesystem
24 1
     */
25
    public function __construct(FilesystemInterface $filesystem)
26
    {
27
        $this->filesystem = $filesystem;
28
    }
29 2
30
    /**
31
     * @inheritDoc
32
     */
33
    public function fromFile(string $filename): array
34
    {
35
        if ($this->filesystem->has($filename)) {
36
            return $this->fromString($this->filesystem->read($filename));
0 ignored issues
show
Security Bug introduced by
It seems like $this->filesystem->read($filename) targeting League\Flysystem\FilesystemInterface::read() can also be of type false; however, Venta\Config\Parser\Json::fromString() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
37
        }
38
39
        throw new \RuntimeException(sprintf('Unable to parse configuration file: "%s".', $filename));
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function fromString(string $configuration): array
46
    {
47
        $array = json_decode($configuration, true);
48
        if (json_last_error() !== JSON_ERROR_NONE) {
49
            throw new \RuntimeException(
50
                sprintf('Unable to parse configuration string: "%s".', json_last_error_msg()),
51
                json_last_error()
52
            );
53
        }
54
55
        return $array;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function supportedExtensions(): array
62
    {
63
        return ['json'];
64
    }
65
}