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