Completed
Pull Request — master (#31)
by
unknown
02:09
created

JsonLoader::getErrorMessage()   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
class JsonLoader {
6
7
    const NO_ERROR = 0;
8
    const ERROR_NO_READABLE = 1;
9
    const ERROR_INVALID_JSON = 2;
10
11
    /**
12
     * @var string
13
     */
14
    private $path;
15
16
    /**
17
     * @var int
18
     */
19
    private $errorCode;
20
21
    /**
22
     * @var string
23
     */
24
    private $errorMessage;
25
26
    /**
27
     * @var mixed
28
     */
29
    private $data;
30
31
    /**
32
     * @param string $path
33
     */
34 3
    public function __construct($path)
35
    {
36 3
        $this->path = $path;
37 3
        if (!is_readable($path) || ($content = file_get_contents($path)) === false) {
38 1
            $this->errorCode = self::ERROR_NO_READABLE;
39 1
            return;
40
        }
41 2
        $this->data = json_decode($content, true);
42 2
        if ($this->data === null && JSON_ERROR_NONE !== json_last_error()) {
43 1
            $this->errorCode = self::ERROR_INVALID_JSON;
44 1
            $this->errorMessage = json_last_error_msg();
45 1
            return;
46
        }
47 1
        $this->errorCode = self::NO_ERROR;
48 1
    }
49
50
    /**
51
     * @return string
52
     */
53 3
    public function getPath()
54
    {
55 3
        return $this->path;
56
    }
57
58
    /**
59
     * @return int
60
     */
61 3
    public function getErrorCode()
62
    {
63 3
        return $this->errorCode;
64
    }
65
66
    /**
67
     * @return string
68
     */
69 2
    public function getErrorMessage()
70
    {
71 2
        return $this->errorMessage;
72
    }
73
74
    /**
75
     * @return mixed
76
     */
77 3
    public function getData()
78
    {
79 3
        return $this->data;
80
    }
81
82
}
83