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); |
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
|
|
View Code Duplication |
public function add(RpcRequestInterface $request) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
if (!is_null($request)) { |
29
|
|
|
unset($this->requests[$request->getId()]); |
30
|
|
|
$this->requests[$request->getId()] = $request; |
31
|
|
|
} else { |
32
|
|
|
$key = array_search($request, $this->requests, true); |
33
|
|
|
unset($this->requests[$key]); |
34
|
|
|
$this->requests[] = $request; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function all() |
39
|
|
|
{ |
40
|
|
|
return $this->requests; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function get($id) |
44
|
|
|
{ |
45
|
|
|
if (isset($this->requests[$id])) { |
46
|
|
|
return $this->requests[$id]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param RpcRequest $request |
54
|
|
|
* @return string|int|null |
55
|
|
|
*/ |
56
|
|
|
public function getRequestIndex(RpcRequest $request) |
57
|
|
|
{ |
58
|
|
|
return array_search($request, $this->requests); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $id |
63
|
|
|
*/ |
64
|
|
|
public function remove($id) |
65
|
|
|
{ |
66
|
|
|
unset($this->requests[$id]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param RpcRequestCollection $collection |
71
|
|
|
*/ |
72
|
|
|
public function addCollection(RpcRequestCollection $collection) |
73
|
|
|
{ |
74
|
|
|
foreach ($collection->all() as $id => $request) { |
75
|
|
|
unset($this->requests[$id]); |
76
|
|
|
$this->requests[$id] = $request; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $priorityNumber |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function changePriority($priorityNumber) |
85
|
|
|
{ |
86
|
|
|
if (!decoct(octdec($priorityNumber) == $priorityNumber)) { |
87
|
|
|
$priorityNumber = decoct($priorityNumber); |
88
|
|
|
} |
89
|
|
|
$this->priority = $priorityNumber; |
90
|
|
|
|
91
|
|
|
return $this->priority; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
public function clear() |
96
|
|
|
{ |
97
|
|
|
$this->requests = []; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return octect priority |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function getPriority() |
106
|
|
|
{ |
107
|
|
|
return $this->priority; |
108
|
|
|
} |
109
|
|
|
} |
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.