Completed
Push — master ( b48de3...82623c )
by Daniel
04:05
created

RpcRequestCollection   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 15
c 4
b 1
f 1
lcom 2
cbo 0
dl 0
loc 93
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
A add() 0 9 2
A all() 0 4 1
A get() 0 8 2
A getRequestIndex() 0 4 1
A remove() 0 4 1
A addCollection() 0 7 2
A changePriority() 0 7 2
A getPriority() 0 4 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc\Request;
4
5
6
class RpcRequestCollection implements RpcRequestCollectionInterface, \IteratorAggregate, \Countable
7
{
8
    private $priority;
9
    public $requests = [];
10
11
    public function __construct($priority = RpcRequestCollectionInterface::PRIORITY_LOW)
12
    {
13
        $this->priority = $this->changePriority($priority);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->priority is correct as $this->changePriority($priority) (which targets Cmobi\RabbitmqBundle\Rpc...ction::changePriority()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
14
    }
15
16
    public function getIterator()
17
    {
18
        return new \ArrayIterator($this->requests);
19
    }
20
21
    public function count()
22
    {
23
        return count($this->requests);
24
    }
25
26
    public function add(RpcRequestInterface $request)
27
    {
28
        $key = array_search($request, $this->requests, true);
29
30
        if ($key !== false) {
31
            unset($this->requests[$key]);
32
        }
33
        $this->requests[] = $request;
34
    }
35
36
    public function all()
37
    {
38
        return $this->requests;
39
    }
40
41
    public function get($id)
42
    {
43
        if (isset($this->requests[$id])) {
44
            return $this->requests[$id];
45
        }
46
47
        return null;
48
    }
49
50
    /**
51
     * @param RpcRequest $request
52
     * @return string|int|null
53
     */
54
    public function getRequestIndex(RpcRequest $request)
55
    {
56
        return array_search($request, $this->requests);
57
    }
58
59
    /**
60
     * @param $id
61
     */
62
    public function remove($id)
63
    {
64
        unset($this->requests[$id]);
65
    }
66
67
    /**
68
     * @param RpcRequestCollection $collection
69
     */
70
    public function addCollection(RpcRequestCollection $collection)
71
    {
72
        foreach ($collection->all() as $id => $request) {
73
            unset($this->requests[$id]);
74
            $this->requests[$id] = $request;
75
        }
76
    }
77
78
    /**
79
     * @param $priorityNumber
80
     */
81
    public function changePriority($priorityNumber)
82
    {
83
        if (!decoct(octdec($priorityNumber) == $priorityNumber)) {
84
            $priorityNumber = decoct($priorityNumber);
85
        }
86
        $this->priority = $priorityNumber;
87
    }
88
89
    /**
90
     * Return octect priority
91
     *
92
     * @return int
93
     */
94
    public function getPriority()
95
    {
96
        return $this->priority;
97
    }
98
}