1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Mediapart Selligent Client API |
5
|
|
|
* |
6
|
|
|
* CC BY-NC-SA <https://github.com/mediapart/selligent> |
7
|
|
|
* |
8
|
|
|
* For the full license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mediapart\Selligent; |
13
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Psr\Log\LoggerAwareInterface; |
16
|
|
|
|
17
|
|
|
use Mediapart\Selligent\Response\GetUserByFilterResponse; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
class Transport |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var \SoapClient |
26
|
|
|
*/ |
27
|
|
|
protected $client; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $list; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $campaign; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var LoggerInterface |
41
|
|
|
*/ |
42
|
|
|
protected $logger = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \SoapClient $client |
46
|
|
|
* @param string $list |
47
|
|
|
* @param string $campaign |
48
|
|
|
*/ |
49
|
11 |
|
public function __construct(\SoapClient $client, $list = null, $campaign = null) |
50
|
|
|
{ |
51
|
11 |
|
$this->client = $client; |
52
|
|
|
|
53
|
11 |
|
if (!is_null($list)) { |
54
|
8 |
|
$this->setList($list); |
55
|
8 |
|
} |
56
|
11 |
|
if (!is_null($campaign)) { |
57
|
8 |
|
$this->setCampaign($campaign); |
58
|
8 |
|
} |
59
|
11 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
7 |
|
public function setLogger(LoggerInterface $logger) |
65
|
|
|
{ |
66
|
7 |
|
$this->logger = $logger; |
67
|
7 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* |
71
|
|
|
* @param string $list Name of the list |
72
|
|
|
* @throws Exception if the list was not found in the remote server |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
10 |
|
public function setList($list) |
76
|
|
|
{ |
77
|
10 |
|
$response = $this->client->GetListID(['name' => $list]); |
78
|
|
|
|
79
|
10 |
|
if (Response::SUCCESSFUL !== $response->getCode()) { |
80
|
1 |
|
throw new \Exception(); |
81
|
|
|
} else { |
82
|
9 |
|
$this->list = $response->getId(); |
83
|
|
|
|
84
|
9 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
2 |
|
public function getList() |
92
|
|
|
{ |
93
|
2 |
|
return $this->list; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
* @param string $campaign Gate Name |
99
|
|
|
* @return self |
100
|
|
|
*/ |
101
|
9 |
|
public function setCampaign($campaign) |
102
|
|
|
{ |
103
|
9 |
|
$this->campaign = $campaign; |
104
|
|
|
|
105
|
9 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
2 |
|
public function getCampaign() |
112
|
|
|
{ |
113
|
2 |
|
return $this->campaign; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param GetUserByFilterResponse $response |
118
|
|
|
* @param string $idKey |
119
|
|
|
* |
120
|
|
|
* @return integer |
121
|
|
|
*/ |
122
|
2 |
|
private function getExistingUserId(GetUserByFilterResponse $response, $idKey) |
123
|
|
|
{ |
124
|
2 |
|
$properties = $response->getProperties(); |
125
|
2 |
|
if (isset($properties[$idKey])) { |
126
|
1 |
|
$id = $properties[$idKey]; |
127
|
1 |
|
if ($this->logger) { |
128
|
1 |
|
$this->logger->info('User already registered', [$idKey => $id]); |
129
|
1 |
|
} |
130
|
1 |
|
return $id; |
131
|
|
|
} else { |
132
|
1 |
|
throw new \UnexpectedValueException(sprintf( |
133
|
1 |
|
"key %s do not exists in %s", |
134
|
1 |
|
$idKey, |
135
|
1 |
|
implode(',', array_keys((array) $properties)) |
136
|
1 |
|
)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param Properties $user |
142
|
|
|
* @return integer |
143
|
|
|
*/ |
144
|
2 |
|
private function createUser(Properties $user) |
145
|
|
|
{ |
146
|
2 |
|
$response = $this->client->CreateUser([ |
147
|
2 |
|
'List' => $this->list, |
148
|
2 |
|
'Changes' => $user, |
149
|
2 |
|
]); |
150
|
2 |
|
$id = null; |
151
|
2 |
|
if (Response::SUCCESSFUL === $response->getCode()) { |
152
|
1 |
|
$id = $response->getUserId(); |
153
|
1 |
|
if ($this->logger) { |
154
|
1 |
|
$this->logger->notice( |
155
|
1 |
|
'New user registered', |
156
|
|
|
[ |
157
|
1 |
|
'id' => $id, |
158
|
|
|
'properties' => $user |
159
|
1 |
|
] |
160
|
1 |
|
); |
161
|
1 |
|
} |
162
|
1 |
|
} |
163
|
2 |
|
return $id; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Subscribe the given user, if not already in list, and returns his identifier |
168
|
|
|
* |
169
|
|
|
* @param Properties $user |
170
|
|
|
* @return int User id |
171
|
|
|
* @throws UnexpectedValueException if $idKey is not an existing Property |
172
|
|
|
* @throws Exception If something happens during the request |
173
|
|
|
*/ |
174
|
5 |
|
public function subscribe(Properties $user, $idKey = 'ID') |
175
|
|
|
{ |
176
|
5 |
|
$response = $this->client->GetUserByFilter([ |
177
|
5 |
|
'List' => $this->list, |
178
|
5 |
|
'Filter' => $user, |
179
|
5 |
|
]); |
180
|
|
|
|
181
|
|
|
try { |
182
|
5 |
|
switch ($response->getCode()) { |
183
|
|
|
|
184
|
|
|
/* the user already exists */ |
185
|
5 |
|
case Response::SUCCESSFUL: |
186
|
2 |
|
$id = $this->getExistingUserId($response, $idKey); |
187
|
1 |
|
break; |
188
|
|
|
|
189
|
|
|
/* the user do not exist, we create it */ |
190
|
3 |
|
case Response::ERROR_NORESULT: |
191
|
2 |
|
if($id = $this->createUser($user)) { |
192
|
1 |
|
break; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/* something wrong */ |
196
|
2 |
|
default: |
197
|
2 |
|
throw new \Exception( |
198
|
2 |
|
$response->getError(), |
199
|
2 |
|
$response->getCode() |
200
|
2 |
|
); |
201
|
3 |
|
} |
202
|
5 |
|
} catch (\Exception $e) { |
203
|
3 |
|
if ($this->logger) { |
204
|
3 |
|
$this->logger->error($e->getMessage()); |
205
|
3 |
|
} |
206
|
3 |
|
throw $e; |
207
|
|
|
} |
208
|
|
|
|
209
|
2 |
|
return $id; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param int $userId |
214
|
|
|
* @param Properties $inputData |
215
|
|
|
* @return string |
216
|
|
|
* @throws Exception |
217
|
|
|
*/ |
218
|
2 |
|
public function triggerCampaign($userId, Properties $inputData) |
219
|
|
|
{ |
220
|
|
|
$options = [ |
221
|
2 |
|
'List' => $this->list, |
222
|
2 |
|
'UserID' => $userId, |
223
|
2 |
|
'GateName' => $this->campaign, |
224
|
2 |
|
'InputData' => $inputData, |
225
|
2 |
|
]; |
226
|
|
|
|
227
|
2 |
|
$response = $this->client->TriggerCampaignForUserWithResult($options); |
228
|
|
|
|
229
|
|
|
$context = [ |
230
|
2 |
|
'request' => $options, |
231
|
|
|
'response' => [ |
232
|
2 |
|
'code' => $response->getCode(), |
233
|
2 |
|
'error' => $response->getError(), |
234
|
2 |
|
'result' => $response->getResult(), |
235
|
2 |
|
], |
236
|
2 |
|
]; |
237
|
|
|
|
238
|
2 |
|
if (Response::SUCCESSFUL !== $response->getCode()) { |
239
|
1 |
|
if ($this->logger) { |
240
|
1 |
|
$this->logger->error('triggerCampaign has failed', $context); |
241
|
1 |
|
} |
242
|
1 |
|
throw new \Exception( |
243
|
1 |
|
$response->getError(), |
244
|
1 |
|
$response->getCode() |
245
|
1 |
|
); |
246
|
|
|
} else { |
247
|
1 |
|
if ($this->logger) { |
248
|
1 |
|
$this->logger->info('triggerCampaign with success', $context); |
249
|
1 |
|
} |
250
|
1 |
|
return $response->getResult(); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|