7
7
use function Evolv \Utils \waitFor ;
8
8
use function Evolv \Utils \emit ;
9
9
10
-
10
+ /**
11
+ * The EvolvClient provides a low level integration with the Evolv participant APIs.
12
+ *
13
+ * The client provides asynchronous access to key states, values, contexts, and configurations.
14
+ *
15
+ */
11
16
class EvolvClient
12
17
{
13
18
const INITIALIZED = 'initialized ' ;
@@ -16,16 +21,19 @@ class EvolvClient
16
21
const EVENT_EMITTED = 'event.emitted ' ;
17
22
18
23
public bool $ initialized = false ;
24
+ /**
25
+ * The context against which the key predicates will be evaluated.
26
+ */
19
27
public EvolvContext $ context ;
20
28
private $ store ;
21
29
private bool $ autoconfirm ;
22
30
private Beacon $ contextBeacon ;
23
31
private Beacon $ eventBeacon ;
24
32
25
33
/**
26
- * @param string $environment
27
- * @param string $endpoint
28
- * @param bool $autoconfirm
34
+ * @param string $environment The current environment id.
35
+ * @param string $endpoint The participants API endpoint.
36
+ * @param bool $autoconfirm Optional. True by default. The autoconfirm flag.
29
37
* @return object
30
38
*/
31
39
public function __construct (string $ environment , string $ endpoint = 'https://participants.evolv.ai/ ' , bool $ autoconfirm = true )
@@ -44,14 +52,11 @@ public function __construct(string $environment, string $endpoint = 'https://par
44
52
* Initializes the client with required context information.
45
53
*
46
54
* @param string $uid A globally unique identifier for the current participant.
47
- * @param object $remoteContext A map of data used for evaluating context predicates and analytics.
48
- * @param object $localContext A map of data used only for evaluating context predicates.
49
- * @return array
55
+ * @param array $remoteContext A map of data used for evaluating context predicates and analytics.
56
+ * @param array $localContext A map of data used only for evaluating context predicates.
50
57
*/
51
-
52
58
public function initialize (string $ uid , array $ remoteContext = [], array $ localContext = [], $ httpClient = null )
53
59
{
54
-
55
60
if ($ this ->initialized ) {
56
61
throw new \Exception ('Evolv: Client is already initialized ' );
57
62
exit ('Evolv: Client is already initialized ' );
@@ -62,15 +67,6 @@ public function initialize(string $uid, array $remoteContext = [], array $localC
62
67
exit ('Evolv: "uid" must be specified ' );
63
68
}
64
69
65
- $ this ->context ->initialize ($ uid , $ remoteContext , $ localContext );
66
- $ this ->store ->initialize ($ this ->context , $ httpClient );
67
-
68
- if ($ this ->autoconfirm ) {
69
- $ this ->confirm ();
70
- }
71
-
72
- $ this ->initialized = true ;
73
-
74
70
waitFor (CONTEXT_INITIALIZED , function ($ type , $ ctx ) {
75
71
$ this ->contextBeacon ->emit ($ type , $ this ->context ->remoteContext );
76
72
});
@@ -81,12 +77,27 @@ public function initialize(string $uid, array $remoteContext = [], array $localC
81
77
}
82
78
$ this ->contextBeacon ->emit ($ type , ['key ' => $ key , 'value ' => $ value ]);
83
79
});
84
- waitFor (CONTEXT_VALUE_CHANGED , function ($ type , $ key , $ value , $ local ) {
80
+ waitFor (CONTEXT_VALUE_CHANGED , function ($ type , $ key , $ value , $ before , $ local ) {
85
81
if ($ local ) {
86
82
return ;
87
83
}
88
84
$ this ->contextBeacon ->emit ($ type , ['key ' => $ key , 'value ' => $ value ]);
89
85
});
86
+ waitFor (CONTEXT_VALUE_REMOVED , function ($ type , $ key , $ local ) {
87
+ if ($ local ) {
88
+ return ;
89
+ }
90
+ $ this ->contextBeacon ->emit ($ type , ['key ' => $ key ]);
91
+ });
92
+
93
+ $ this ->context ->initialize ($ uid , $ remoteContext , $ localContext );
94
+ $ this ->store ->initialize ($ this ->context , $ httpClient );
95
+
96
+ if ($ this ->autoconfirm ) {
97
+ $ this ->confirm ();
98
+ }
99
+
100
+ $ this ->initialized = true ;
90
101
91
102
emit (EvolvClient::INITIALIZED );
92
103
}
@@ -117,8 +128,7 @@ public function initialize(string $uid, array $remoteContext = [], array $localC
117
128
*
118
129
* @param string $topic The event topic on which the listener should be invoked.
119
130
* @param callable $listener The listener to be invoked for the specified topic.
120
- * @function
121
- * @see EvolvClient for listeners that should only be invoked once.
131
+ * @see EvolvClient for listeners that should only be invoked once.
122
132
*/
123
133
public function on (string $ topic , callable $ listener )
124
134
{
@@ -129,8 +139,8 @@ public function on(string $topic, callable $listener)
129
139
* Send an event to the events endpoint.
130
140
*
131
141
* @param string $type The type associated with the event.
132
- * @param object $metadata Any metadata to attach to the event.
133
- * @param boolean $flush If true, the event will be sent immediately.
142
+ * @param mixed $metadata Any metadata to attach to the event.
143
+ * @param bool $flush If true, the event will be sent immediately.
134
144
*/
135
145
public function emit (string $ type , $ metadata , bool $ flush = false )
136
146
{
@@ -146,9 +156,8 @@ public function emit(string $type, $metadata, bool $flush = false)
146
156
* Check all active keys that start with the specified prefix.
147
157
*
148
158
* @param string $prefix The prefix of the keys to check.
149
- * @returns {SubscribablePromise.<Object|Error>} A SubscribablePromise that resolves to object
150
- * describing the state of active keys.
151
- * @function
159
+ * @param callable $listener Optional. The callback function to listen to active keys changes.
160
+ * @return array An array describing the state of active keys.
152
161
*/
153
162
public function getActiveKeys (string $ prefix = '' , callable $ listener = null )
154
163
{
@@ -159,8 +168,8 @@ public function getActiveKeys(string $prefix = '', callable $listener = null)
159
168
* Get the value of a specified key.
160
169
*
161
170
* @param string $key The key of the value to retrieve.
162
- * @returns @mixed A value of the specified key.
163
- * @function
171
+ * @param callable $listener Optional. The callback function to listen to the specified key changes .
172
+ * @return mixed A value of the specified key.
164
173
*/
165
174
public function get (string $ key = '' , callable $ listener = null )
166
175
{
@@ -229,12 +238,12 @@ public function confirm()
229
238
* Marks a consumer as unsuccessfully retrieving and / or applying requested values, making them ineligible for
230
239
* inclusion in optimization statistics.
231
240
*
232
- * @param object $details Optional. Information on the reason for contamination. If provided, the object should
241
+ * @param array $details Optional. Information on the reason for contamination. If provided, the object should
233
242
* contain a reason. Optionally, a 'details' value should be included for extra debugging info
234
- * @param boolean $allExperiments If true, the user will be excluded from all optimizations, including optimization
243
+ * @param bool $allExperiments If true, the user will be excluded from all optimizations, including optimization
235
244
* not applicable to this page
236
245
*/
237
- public function contaminate ($ details , bool $ allExperiments = false )
246
+ public function contaminate (array $ details , bool $ allExperiments = false )
238
247
{
239
248
$ allocations = $ this ->context ->get ('experiments.allocations ' );
240
249
if (!isset ($ allocations ) || !count ($ allocations )) {
@@ -281,10 +290,3 @@ public function contaminate($details, bool $allExperiments = false)
281
290
emit (EvolvClient::CONTAMINATED );
282
291
}
283
292
}
284
-
285
-
286
-
287
-
288
-
289
-
290
-
0 commit comments