|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alawar\NginxPushStreamBundle; |
|
4
|
|
|
|
|
5
|
|
|
use Alawar\NginxPushStreamBundle\Filter\FilterInterface; |
|
6
|
|
|
use Alawar\NginxPushStreamBundle\Http\SenderInterface; |
|
7
|
|
|
use Alawar\NginxPushStreamBundle\IdGenerator\IdGeneratorInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Connection |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $pubUrl; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $subUrls; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var FilterInterface[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $filters = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var IdGeneratorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $idGenerator = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var SenderInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $sender = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $pubUrl |
|
38
|
|
|
* @param array $subUrls |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($pubUrl, $subUrls) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->pubUrl = $pubUrl; |
|
43
|
|
|
$this->subUrls = $subUrls; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param FilterInterface $filter |
|
48
|
|
|
*/ |
|
49
|
|
|
public function addFilter(FilterInterface $filter) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->filters[] = $filter; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $data |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function filter($data) |
|
59
|
|
|
{ |
|
60
|
|
|
foreach ($this->filters as $filter) { |
|
61
|
|
|
$data = $filter->filter($data); |
|
62
|
|
|
} |
|
63
|
|
|
return $data; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string[] $tokens |
|
68
|
|
|
* @return string[] |
|
69
|
|
|
*/ |
|
70
|
|
|
public function filterTokens(array $tokens) |
|
71
|
|
|
{ |
|
72
|
|
|
$res = array(); |
|
73
|
|
|
foreach ($tokens as $token) { |
|
74
|
|
|
$res[] = $this->filter($token); |
|
75
|
|
|
} |
|
76
|
|
|
return $res; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param IdGeneratorInterface $idGenerator |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setIdGenerator(IdGeneratorInterface $idGenerator) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->idGenerator = $idGenerator; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param SenderInterface $sender |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setSender(SenderInterface $sender) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->sender = $sender; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string[] $tokens |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getSubUrls(array $tokens) |
|
100
|
|
|
{ |
|
101
|
|
|
$filteredTokens = $this->filterTokens($tokens); |
|
102
|
|
|
$tokensString = join('/', $filteredTokens); |
|
103
|
|
|
$res = array(); |
|
104
|
|
|
foreach ($this->subUrls as $type => $subUrl) { |
|
105
|
|
|
$res[$type] = str_replace('{tokens}', $tokensString, $subUrl); |
|
106
|
|
|
} |
|
107
|
|
|
return $res; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param $token string |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getPubUrl($token) |
|
115
|
|
|
{ |
|
116
|
|
|
$filteredToken = $this->filter($token); |
|
117
|
|
|
$res = str_replace('{token}', $filteredToken, $this->pubUrl); |
|
118
|
|
|
return $res; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $token |
|
123
|
|
|
* @param mixed $data |
|
124
|
|
|
* @param string $type |
|
125
|
|
|
* @param string $id |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
public function send($token, $data, $type = null, $id = null) |
|
129
|
|
|
{ |
|
130
|
|
|
if (!$this->sender) { |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$url = $this->getPubUrl($token); |
|
135
|
|
|
|
|
136
|
|
|
if ($id === null && $this->idGenerator) { |
|
137
|
|
|
$id = $this->idGenerator->generate(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$msg = array(); |
|
141
|
|
|
$msg['token'] = $token; |
|
142
|
|
|
|
|
143
|
|
|
$headers = array(); |
|
144
|
|
|
if ($id !== null) { |
|
145
|
|
|
$msg['id'] = $id; |
|
146
|
|
|
$headers['Event-ID'] = preg_replace('/\\r\\n/', '', $id); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if ($type !== null) { |
|
150
|
|
|
$msg['type'] = $type; |
|
151
|
|
|
$headers['Event-Type'] = preg_replace('/\\r\\n/', '', $type); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$headers['Content-Type'] = 'application/json'; |
|
155
|
|
|
|
|
156
|
|
|
$msg['data'] = $data; |
|
157
|
|
|
|
|
158
|
|
|
$json = json_encode($msg); |
|
159
|
|
|
|
|
160
|
|
|
$body = $json . "\r\n"; |
|
161
|
|
|
|
|
162
|
|
|
return $this->sender->send($url, $body, $headers); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|