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

JsonLoader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 78
ccs 20
cts 20
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 15 5
A getPath() 0 4 1
A getErrorCode() 0 4 1
A getErrorMessage() 0 4 1
A getData() 0 4 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