1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Wcomnisky\Flagship; |
6
|
|
|
|
7
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
8
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
9
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
10
|
|
|
use Wcomnisky\Flagship\Api\RequestParameters; |
11
|
|
|
use Wcomnisky\Flagship\Context\ContextInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Flagship |
15
|
|
|
* @package Wcomnisky\Flagship |
16
|
|
|
* @see http://developers.flagship.io/api/v1/ |
17
|
|
|
*/ |
18
|
|
|
class Flagship |
19
|
|
|
{ |
20
|
|
|
private const NAMED_PARAM_ENV_ID = '%ENVIRONMENT_ID'; |
21
|
|
|
private const NAMED_PARAM_CAMPAIGN_ID = '%CAMPAIGN_ID'; |
22
|
|
|
private const NAMED_PARAM_BASE_URL = '%BASE_URL'; |
23
|
|
|
|
24
|
|
|
private const URL_ALL_CAMPAIGNS = self::NAMED_PARAM_BASE_URL . '/' . self::NAMED_PARAM_ENV_ID . '/campaigns'; |
25
|
|
|
private const URL_SINGLE_CAMPAIGN = self::URL_ALL_CAMPAIGNS . '/' . self::NAMED_PARAM_CAMPAIGN_ID; |
26
|
|
|
private const URL_CAMPAIGN_ACTIVATION = self::NAMED_PARAM_BASE_URL . '/activate'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string the base url of flagship endpoints ex: https://decision-api.flagship.io/v1 |
30
|
|
|
*/ |
31
|
|
|
private $baseUrl; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var RequestParameters |
35
|
|
|
* @see http://developers.flagship.io/api/v1/#request-parameters |
36
|
|
|
*/ |
37
|
|
|
private $requestParameters; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string The environment ID identifies your account and environment |
41
|
|
|
*/ |
42
|
|
|
private $environmentId; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var HttpClientInterface |
46
|
|
|
*/ |
47
|
|
|
private $httpClient; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Flagship constructor. |
51
|
|
|
* |
52
|
|
|
* @param string $baseUrl |
53
|
|
|
* @param string $environmentId |
54
|
|
|
* @param HttpClientInterface $httpClient |
55
|
|
|
*/ |
56
|
7 |
|
public function __construct(string $baseUrl, string $environmentId, HttpClientInterface $httpClient) |
57
|
|
|
{ |
58
|
7 |
|
if (empty($baseUrl)) { |
59
|
1 |
|
throw new \InvalidArgumentException('baseUrl cannot be empty'); |
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
if (empty($environmentId)) { |
63
|
1 |
|
throw new \InvalidArgumentException('Environment ID cannot be empty'); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
$this->baseUrl = $baseUrl; |
67
|
5 |
|
$this->environmentId = $environmentId; |
68
|
5 |
|
$this->httpClient = $httpClient; |
69
|
5 |
|
$this->requestParameters = new RequestParameters(); |
70
|
5 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets the Request Parameters |
74
|
|
|
* |
75
|
|
|
* @param RequestParameters $requestParameters |
76
|
|
|
*/ |
77
|
1 |
|
public function setRequestParameters(RequestParameters $requestParameters): void |
78
|
|
|
{ |
79
|
1 |
|
$this->requestParameters = $requestParameters; |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Affects a visitor to a variation. It should be use to manually activate a single |
84
|
|
|
* campaign and variation in case you do not automatically activate them when running |
85
|
|
|
* campaign assignment (See trigger_hit parameter) |
86
|
|
|
* |
87
|
|
|
* It returns a 204 HTTP response |
88
|
|
|
* |
89
|
|
|
* @param string $visitorId |
90
|
|
|
* @param string $variationGroupId |
91
|
|
|
* @param string $variationId |
92
|
|
|
* @return ResponseInterface |
93
|
|
|
* @throws TransportExceptionInterface |
94
|
|
|
* @see http://developers.flagship.io/api/v1/?shell#campaign-activation |
95
|
|
|
* @see http://developers.flagship.io/api/v1/?shell#trigger-hit |
96
|
|
|
*/ |
97
|
1 |
|
public function requestCampaignActivation( |
98
|
|
|
string $visitorId, |
99
|
|
|
string $variationGroupId, |
100
|
|
|
string $variationId |
101
|
|
|
): ResponseInterface { |
102
|
|
|
|
103
|
|
|
$jsonArray = [ |
104
|
1 |
|
'vid' => $visitorId, |
105
|
1 |
|
'cid' => $this->environmentId, |
106
|
1 |
|
'caid' => $variationGroupId, |
107
|
1 |
|
'vaid' => $variationId |
108
|
|
|
]; |
109
|
|
|
|
110
|
1 |
|
$response = $this->httpClient->request( |
111
|
1 |
|
'POST', |
112
|
1 |
|
$this->getCompaignActivationUrl(), |
113
|
|
|
[ |
114
|
1 |
|
'json' => $jsonArray |
115
|
|
|
] |
116
|
|
|
); |
117
|
|
|
|
118
|
1 |
|
return $response; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieves the affection of your visitor ID with a specific |
123
|
|
|
* context (key/value pairs) to the specified campaign ID. |
124
|
|
|
* |
125
|
|
|
* By default, the API will send a hit to trigger a campaign |
126
|
|
|
* assignment event for the visitor ID and the affected campaign. |
127
|
|
|
* |
128
|
|
|
* @param string $visitorId ID of the visitor |
129
|
|
|
* @param string $campaignId The same as Custom ID on their docs |
130
|
|
|
* @param ContextInterface $context |
131
|
|
|
* @return ResponseInterface |
132
|
|
|
* @throws TransportExceptionInterface |
133
|
|
|
*/ |
134
|
1 |
|
public function requestSingleCampaign( |
135
|
|
|
string $visitorId, |
136
|
|
|
string $campaignId, |
137
|
|
|
ContextInterface $context |
138
|
|
|
): ResponseInterface { |
139
|
|
|
|
140
|
|
|
$jsonArray = [ |
141
|
1 |
|
'visitor_id' => $visitorId, |
142
|
1 |
|
'context' => !empty($context->getList()) ? $context->getList() : new \stdClass(), |
143
|
1 |
|
'decision_group' => $this->requestParameters->getDecisionGroup(), |
144
|
1 |
|
'format_response' => $this->requestParameters->isFormatResponseEnabled(), |
145
|
1 |
|
'trigger_hit' => $this->requestParameters->isTriggerHitEnabled() |
146
|
|
|
]; |
147
|
|
|
|
148
|
1 |
|
$response = $this->httpClient->request( |
149
|
1 |
|
'POST', |
150
|
1 |
|
$this->getSingleCampaignUrl($campaignId), |
151
|
|
|
[ |
152
|
1 |
|
'json' => $jsonArray |
153
|
|
|
] |
154
|
|
|
); |
155
|
|
|
|
156
|
1 |
|
return $response; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Retrieves all the campaigns affected to your visitor and the context (key/value pairs). |
161
|
|
|
* |
162
|
|
|
* By default, the API will send a hit to trigger a campaign |
163
|
|
|
* assignment event for the visitor ID and each affected campaign. |
164
|
|
|
* |
165
|
|
|
* @param string $visitorId |
166
|
|
|
* @param ContextInterface $context |
167
|
|
|
* @return ResponseInterface |
168
|
|
|
* @throws TransportExceptionInterface |
169
|
|
|
*/ |
170
|
2 |
|
public function requestAllCampaigns(string $visitorId, ContextInterface $context): ResponseInterface |
171
|
|
|
{ |
172
|
|
|
$jsonArray = [ |
173
|
2 |
|
'visitor_id' => $visitorId, |
174
|
2 |
|
'context' => !empty($context->getList()) ? $context->getList() : new \stdClass(), |
175
|
2 |
|
'decision_group' => $this->requestParameters->getDecisionGroup(), |
176
|
2 |
|
'trigger_hit' => $this->requestParameters->isTriggerHitEnabled() |
177
|
|
|
]; |
178
|
|
|
|
179
|
2 |
|
$response = $this->httpClient->request( |
180
|
2 |
|
'POST', |
181
|
2 |
|
$this->getAllCampaignsUrl(), |
182
|
|
|
[ |
183
|
2 |
|
'json' => $jsonArray |
184
|
|
|
] |
185
|
|
|
); |
186
|
|
|
|
187
|
2 |
|
return $response; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Replaces the named parameter (key) with its correlated value |
192
|
|
|
* |
193
|
|
|
* @param string $source |
194
|
|
|
* @param array $namedParamKVP |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
4 |
|
private function replaceNamedParameter(string $source, array $namedParamKVP): string |
198
|
|
|
{ |
199
|
4 |
|
return str_replace(array_keys($namedParamKVP), array_values($namedParamKVP), $source); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns the SingleCampaign URL with the Environment ID and Campaign ID in place |
204
|
|
|
* |
205
|
|
|
* @param string $campaignId |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
1 |
|
private function getSingleCampaignUrl(string $campaignId): string |
209
|
|
|
{ |
210
|
1 |
|
return $this->replaceNamedParameter( |
211
|
1 |
|
self::URL_SINGLE_CAMPAIGN, |
212
|
|
|
[ |
213
|
1 |
|
self::NAMED_PARAM_BASE_URL => $this->baseUrl, |
214
|
1 |
|
self::NAMED_PARAM_ENV_ID => $this->environmentId, |
215
|
1 |
|
self::NAMED_PARAM_CAMPAIGN_ID => $campaignId |
216
|
|
|
] |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
1 |
|
private function getCompaignActivationUrl(): string |
224
|
|
|
{ |
225
|
1 |
|
return $this->replaceNamedParameter( |
226
|
1 |
|
self::URL_CAMPAIGN_ACTIVATION, |
227
|
|
|
[ |
228
|
1 |
|
self::NAMED_PARAM_BASE_URL => $this->baseUrl |
229
|
|
|
] |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Returns the AllCampaigns URL with the Environment ID in place. If the Mode is |
235
|
|
|
* different than normal (default) it is appended to the URL as a Query String |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
2 |
|
private function getAllCampaignsUrl(): string |
240
|
|
|
{ |
241
|
2 |
|
$url = $this->replaceNamedParameter( |
242
|
2 |
|
self::URL_ALL_CAMPAIGNS, |
243
|
|
|
[ |
244
|
2 |
|
self::NAMED_PARAM_BASE_URL => $this->baseUrl, |
245
|
2 |
|
self::NAMED_PARAM_ENV_ID => $this->environmentId |
246
|
|
|
] |
247
|
|
|
); |
248
|
|
|
|
249
|
2 |
|
if (! $this->requestParameters->isDefaultMode()) { |
250
|
1 |
|
$url .= '?mode=' . $this->requestParameters->getMode(); |
251
|
|
|
} |
252
|
|
|
|
253
|
2 |
|
return $url; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|