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

Json   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 53
ccs 6
cts 11
cp 0.5455
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromFile() 0 8 2
A fromString() 0 12 2
A supportedExtensions() 0 4 1
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
}