Passed
Push — master ( 6b56cf...781f2a )
by Matthias
02:34
created

JsonLoader::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ComposerRequireChecker;
4
5
use ComposerRequireChecker\Exception\InvalidJsonException;
6
use ComposerRequireChecker\Exception\NotReadableException;
7
8
class JsonLoader {
9
10
    /**
11
     * @var mixed
12
     */
13
    private $data;
14
15
    /**
16
     * @param string $path
17
     * @throws InvalidJsonException
18
     * @throws NotReadableException
19
     * @internal
20
     */
21 3
    public function __construct($path)
22
    {
23 3
        if (!is_readable($path) || ($content = file_get_contents($path)) === false) {
24 1
            throw new NotReadableException('unable to read ' . $path);
25
        }
26 2
        $this->data = json_decode($content, true);
27 2
        if ($this->data === null && JSON_ERROR_NONE !== json_last_error()) {
28 1
            throw new InvalidJsonException('error parsing ' . $path . ': ' . json_last_error_msg());
29
        }
30 1
    }
31
32
    /**
33
     * @internal
34
     * @return mixed
35
     */
36 1
    public function getData()
37
    {
38 1
        return $this->data;
39
    }
40
41
}
42