Methods   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 108
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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