1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Rpc; |
4
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Routing\MethodRouter; |
6
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Exception\JsonRpcGenericErrorException; |
7
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Exception\JsonRpcParserErrorException; |
8
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Request\JsonRpcRequestFactory; |
9
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Request\RpcRequestCollectionInterface; |
10
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Request\RpcRequestInterface; |
11
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Response\JsonRpcResponse; |
12
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponseCollectionInterface; |
13
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponseInterface; |
14
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
15
|
|
|
|
16
|
|
|
class RpcMessager |
17
|
|
|
{ |
18
|
|
|
private $router; |
19
|
|
|
private $requestCollection; |
20
|
|
|
private $responseCollection; |
21
|
|
|
private $requestFactory; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
MethodRouter $router, |
25
|
|
|
RpcRequestCollectionInterface $requests, |
26
|
|
|
RpcResponseCollectionInterface $responses, |
27
|
|
|
JsonRpcRequestFactory $factory |
28
|
|
|
) |
29
|
|
|
{ |
30
|
|
|
$this->router = $router; |
31
|
|
|
$this->requestCollection = $requests; |
32
|
|
|
$this->responseCollection = $responses; |
33
|
|
|
$this->requestFactory = $factory; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function parseAMQPMessage(AMQPMessage $message) |
37
|
|
|
{ |
38
|
|
|
$body = $message->body; |
39
|
|
|
|
40
|
|
|
try { |
41
|
|
|
$requests = json_decode($body, true); |
42
|
|
|
} catch (\Exception $e) { |
43
|
|
|
throw new JsonRpcParserErrorException(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!isset($requests[0])) { |
47
|
|
|
$this->buildRequest($requests); |
48
|
|
|
} else { |
49
|
|
|
|
50
|
|
|
foreach ($requests as $request) { |
51
|
|
|
$this->buildRequest($request); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function addRequest(RpcRequestInterface $request) |
57
|
|
|
{ |
58
|
|
|
$this->requestCollection->add($request); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function addResponse(RpcResponseInterface $response) |
62
|
|
|
{ |
63
|
|
|
$this->responseCollection->add($response); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function addRequestCollection(RpcRequestCollectionInterface $collection) |
67
|
|
|
{ |
68
|
|
|
$this->requestCollection = $collection; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function addResponseCollection(RpcResponseCollectionInterface $collection) |
72
|
|
|
{ |
73
|
|
|
$this->responseCollection = $collection; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function buildRequest($request) |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
$request = $this->requestFactory->factory($request); |
80
|
|
|
$this->router->setContext($request); |
81
|
|
|
|
82
|
|
|
if (!$request->attributes->has('_controller')) { |
83
|
|
|
$parameters = $this->router->match($request->getMethod()); |
84
|
|
|
$request->attributes->add($parameters); |
85
|
|
|
} |
86
|
|
|
$this->requestCollection->add($request); |
87
|
|
|
} catch (JsonRpcGenericErrorException $e) { |
88
|
|
|
$response = new JsonRpcResponse([], $e); |
89
|
|
|
$this->responseCollection->add($response); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return RpcRequestCollectionInterface |
95
|
|
|
*/ |
96
|
|
|
public function getRequestCollection() |
97
|
|
|
{ |
98
|
|
|
return $this->requestCollection; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return RpcResponseCollectionInterface |
103
|
|
|
*/ |
104
|
|
|
public function getResponseCollection() |
105
|
|
|
{ |
106
|
|
|
return $this->responseCollection; |
107
|
|
|
} |
108
|
|
|
} |