MethodCollection::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Tonic\Component\ApiLayer\JsonRpc\Method;
4
5
/**
6
 * Holds collection of methods for JSON-RPC server.
7
 */
8
class MethodCollection implements \Iterator
9
{
10
    /**
11
     * @var array
12
     */
13
    private $methods = [];
14
15
    /**
16
     * Constructor.
17
     *
18
     * @param array $methods
19
     */
20
    public function __construct(array $methods = [])
21
    {
22
        foreach ($methods as $methodName => $callable) {
23
            $this->add($methodName, $callable);
24
        }
25
    }
26
27
    /**
28
     * @param string   $methodName
29
     * @param callable $callable
30
     *
31
     * @return MethodCollection
32
     */
33
    public function add($methodName, callable $callable)
34
    {
35
        $this->methods[$methodName] = $callable;
36
37
        return $this;
38
    }
39
40
    /**
41
     * @param string $methodName
42
     *
43
     * @return bool
44
     */
45
    public function has($methodName)
46
    {
47
        return array_key_exists($methodName, $this->methods);
48
    }
49
50
    /**
51
     * @param string $methodName
52
     *
53
     * @return callable|null
54
     */
55
    public function get($methodName)
56
    {
57
        if (!$this->has($methodName)) {
58
            return null;
59
        }
60
61
        return $this->methods[$methodName];
62
    }
63
64
    /**
65
     * @return callable
66
     */
67
    public function current()
68
    {
69
        return current($this->methods);
70
    }
71
72
    /**
73
     * @return callable
74
     */
75
    public function next()
76
    {
77
        return next($this->methods);
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function key()
84
    {
85
        return key($this->methods);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function valid()
92
    {
93
        $key = key($this->methods);
94
95
        return ($key !== false && $key !== null);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function rewind()
102
    {
103
        reset($this->methods);
104
    }
105
}
106