Completed
Pull Request — master (#31)
by
unknown
02:18 queued 34s
created

JsonLoader::__construct()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 8.8571
cc 5
eloc 6
nc 3
nop 1
crap 5
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
     */
20 3
    public function __construct($path)
21
    {
22 3
        if (!is_readable($path) || ($content = file_get_contents($path)) === false) {
23 1
            throw new NotReadableException('unable to read ' . $path);
24
        }
25 2
        $this->data = json_decode($content, true);
26 2
        if ($this->data === null && JSON_ERROR_NONE !== json_last_error()) {
27 1
            throw new InvalidJsonException('error parsing ' . $path . ': ' . json_last_error_msg());
28
        }
29 1
    }
30
31
    /**
32
     * @return mixed
33
     */
34 1
    public function getData()
35
    {
36 1
        return $this->data;
37
    }
38
39
}
40