Completed
Push — master ( f743de...f8a04a )
by Daniel
03:18
created

JsonRpcResponseCollection::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc\Response;
4
5
6 View Code Duplication
class JsonRpcResponseCollection implements RpcResponseCollectionInterface, \IteratorAggregate, \Countable
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
7
{
8
    public $responses = [];
9
10
    public function getIterator()
11
    {
12
        return new \ArrayIterator($this->responses);
13
    }
14
15
    public function count()
16
    {
17
        return count($this->responses);
18
    }
19
20
    public function add($id = null, RpcResponseInterface $response)
21
    {
22
        unset($this->responses[$id]);
23
24
        $this->responses[$id] = $response;
25
    }
26
    public function all()
27
    {
28
        return $this->responses;
29
    }
30
31
32
    public function get($id)
33
    {
34
        if (isset($this->responses[$id])) {
35
            return $this->responses[$id];
36
        }
37
38
        return null;
39
    }
40
41
    public function remove($id)
42
    {
43
        unset($this->responses[$id]);
44
    }
45
46
    public function addCollection(JsonRpcResponseCollection $collection)
47
    {
48
        foreach ($collection->all() as $id => $response) {
49
            unset($this->responses[$id]);
50
            $this->responses[$id] = $response;
51
        }
52
    }
53
54
    public function __toString()
55
    {
56
        $response = [];
57
        foreach ($this->responses as $response) {
58
            $response = json_decode($response, true);
59
            $response[] = $response;
60
        }
61
62
        return json_encode($response);
63
    }
64
}