1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Rpc; |
4
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Exception\InvalidBodyAMQPMessageException; |
6
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
7
|
|
|
|
8
|
|
|
abstract class RpcBaseService implements RpcServiceInterface |
9
|
|
|
{ |
10
|
|
|
private $request; |
11
|
|
|
private $queueName; |
12
|
|
|
|
13
|
|
|
/** @var array */ |
14
|
|
|
protected $queueOptions = [ |
15
|
|
|
'name' => null, |
16
|
|
|
'passive' => false, |
17
|
|
|
'durable' => true, |
18
|
|
|
'exclusive' => false, |
19
|
|
|
'auto_delete' => false, //Em caso de falha no serviço a filha se mantém para que outro processe. |
20
|
|
|
'nowait' => false, |
21
|
|
|
'arguments' => null, |
22
|
|
|
'ticket' => null |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public function __construct(array $queueOptions, array $parameters = null) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$this->queueName = $queueOptions['name']; |
28
|
|
|
$this->queueOptions = array_merge($this->queueOptions, $queueOptions); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Build response for rpc server |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
protected abstract function buildResponse(); |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return \Closure |
40
|
|
|
* @throws InvalidBodyAMQPMessageException |
41
|
|
|
*/ |
42
|
|
|
public function createCallback() |
43
|
|
|
{ |
44
|
|
|
$callback = function (AMQPMessage $request) { |
45
|
|
|
|
46
|
|
|
$this->request = $request; |
47
|
|
|
$body = $this->buildResponse(); |
48
|
|
|
|
49
|
|
|
if (!is_string($body) || is_null($body)) { |
50
|
|
|
throw new InvalidBodyAMQPMessageException('Invalid Body: Content should be string and not null.'); |
51
|
|
|
} |
52
|
|
|
$message = new AMQPMessage( |
53
|
|
|
$body, |
54
|
|
|
['correlation_id' => $request->get('correlation_id')] |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$request->delivery_info['channel']->basic_publish( |
58
|
|
|
$message, |
59
|
|
|
'', |
60
|
|
|
$request->get('reply_to') |
61
|
|
|
); |
62
|
|
|
$request->delivery_info['channel']->basic_ack( |
63
|
|
|
$request->delivery_info['delivery_tag'] |
|
|
|
|
64
|
|
|
); |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
return $callback; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getQueueName() |
71
|
|
|
{ |
72
|
|
|
return $this->queueName; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function getQueueOptions() |
79
|
|
|
{ |
80
|
|
|
return $this->queueOptions; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return AMQPMessage |
85
|
|
|
*/ |
86
|
|
|
public function getRequest() |
87
|
|
|
{ |
88
|
|
|
return $this->request; |
89
|
|
|
} |
90
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.