|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Facile\CrossbarHTTPPublisherBundle\Publisher; |
|
4
|
|
|
|
|
5
|
|
|
use Facile\CrossbarHTTPPublisherBundle\Exception\PublishRequestException; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Publisher |
|
10
|
|
|
* @package Facile\CrossbarHTTPPublisherBundle\Publisher |
|
11
|
|
|
*/ |
|
12
|
|
|
class Publisher |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \GuzzleHttp\Client */ |
|
15
|
|
|
private $client; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private $key; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $secret; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param Client $client |
|
25
|
|
|
* @param $key |
|
26
|
|
|
* @param $secret |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function __construct(Client $client, $key, $secret) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->client = $client; |
|
31
|
1 |
|
$this->key = $key; |
|
32
|
1 |
|
$this->secret = $secret; |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param $topic string |
|
37
|
|
|
* @param $args array|null |
|
38
|
|
|
* @param $kwargs array|null |
|
39
|
|
|
* @return array JSON decoded response |
|
40
|
|
|
* @throws \Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
public function publish($topic, array $args = null, array $kwargs = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$jsonBody = $this->prepareBody($topic, $args, $kwargs); |
|
45
|
|
|
|
|
46
|
|
|
$request = $this->client->createRequest( |
|
47
|
|
|
'POST', |
|
48
|
|
|
null, |
|
49
|
|
|
array( |
|
50
|
|
|
'body' => $jsonBody, |
|
51
|
|
|
'query' => $this->prepareSignature($jsonBody) |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
|
|
$response = $this->client->send($request); |
|
57
|
|
|
} catch (\Exception $e) { |
|
58
|
|
|
throw new PublishRequestException($e->getMessage(), 500, $e); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $response->json(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $topic |
|
66
|
|
|
* @param $args |
|
67
|
|
|
* @param $kwargs |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
private function prepareBody($topic, $args, $kwargs) |
|
71
|
|
|
{ |
|
72
|
|
|
$body = array(); |
|
73
|
|
|
|
|
74
|
|
|
$body['topic'] = $topic; |
|
75
|
|
|
|
|
76
|
|
|
if (null !== $args) { |
|
77
|
|
|
$body['args'] = $args; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (null !== $kwargs) { |
|
81
|
|
|
$body['kwargs'] = $kwargs; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return json_encode($body); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $body |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
private function prepareSignature($body) |
|
92
|
|
|
{ |
|
93
|
|
|
$query = array(); |
|
94
|
|
|
|
|
95
|
|
|
$seq = mt_rand(0, pow(2, 12)); |
|
96
|
|
|
$now = new \DateTime('now', new \DateTimeZone('UTC')); |
|
97
|
|
|
$timestamp = $now->format("Y-m-d\TH:i:s.u\Z"); |
|
98
|
|
|
|
|
99
|
|
|
$query['seq'] = $seq; |
|
100
|
|
|
$query['timestamp'] = $timestamp; |
|
101
|
|
|
|
|
102
|
|
|
if (null !== $this->key && null !== $this->secret) { |
|
103
|
|
|
|
|
104
|
|
|
$nonce = mt_rand(0, pow(2, 53)); |
|
105
|
|
|
$signature = hash_hmac( |
|
106
|
|
|
'sha256', |
|
107
|
|
|
$this->key . $timestamp . $seq . $nonce . $body, |
|
108
|
|
|
$this->secret, |
|
109
|
|
|
true |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
$query['key'] = $this->key; |
|
113
|
|
|
$query['nonce'] = $nonce; |
|
114
|
|
|
$query['signature'] = strtr(base64_encode($signature), '+/', '-_'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $query; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|