Errors   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 98
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 9 5
A add() 0 15 2
A __construct() 0 3 1
A delete() 0 15 2
1
<?php namespace Comodojo\RpcServer\Component;
2
3
use \Psr\Log\LoggerInterface;
4
5
/**
6
 * RPC errors manager
7
 *
8
 * @package     Comodojo Spare Parts
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     MIT
11
 *
12
 * LICENSE:
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
 * THE SOFTWARE.
21
 */
22
23
class Errors {
24
25
    /**
26
     * Array of capabilities
27
     *
28
     * @var array
29
     */
30
    private $rpc_errors = [];
31
32
    /**
33
     * Current logger
34
     *
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
39
    /**
40
     * Class constructor
41
     *
42
     * @param LoggerInterface $logger
43
     */
44 90
    public function __construct(LoggerInterface $logger) {
45
46 90
        $this->logger = $logger;
47
48 90
    }
49
50
    /**
51
     * Add an error
52
     *
53
     * @param int    $code
54
     * @param string $message
55
     *
56
     * @return bool
57
     */
58 90
    final public function add($code, $message) {
59
60 90
        if ( array_key_exists($code, $this->rpc_errors) ) {
61
62 3
            $this->logger->warning("Cannot add error $code: duplicate entry");
63
64 3
            return false;
65
66
        } else {
67
68 90
            $this->rpc_errors[$code] = $message;
69
70 90
            $this->logger->debug("Added error $code");
71
72 90
            return true;
73
74
        }
75
76
    }
77
78
    /**
79
     * Delete an error
80
     *
81
     * @param int $code
82
     *
83
     * @return bool
84
     */
85 3
    final public function delete($code) {
86
87 3
        if ( array_key_exists($code, $this->rpc_errors) ) {
88
89 3
            unset($this->rpc_errors[$code]);
90
91 3
            $this->logger->debug("Deleted error $code");
92
93 3
            return true;
94
95
        } else {
96
97
            $this->logger->warning("Cannot delete error $code: entry not found");
98
99
            return false;
100
101
        }
102
103
    }
104
105
    /**
106
     * Get registered error(s)
107
     *
108
     * @param int $code
109
     *
110
     * @return mixed
111
     */
112 15
    final public function get($code = null) {
113
114 15
        if ( is_null($code) ) return $this->rpc_errors;
115
116 15
        else if ( array_key_exists($code, $this->rpc_errors) ) return $this->rpc_errors[$code];
117
118 3
        else if ( $code >= -32099 && $code <= -32000 ) return 'Server Error';
119
120 3
        else return 'Unknown Error';
121
122
    }
123
124
}
125