Skip to content

Commit 9a401d7

Browse files
Merge pull request #9 from rmamchyk/feat/AP-1515
chore: add composer scripts and readme
2 parents dd6432f + ccd2fd7 commit 9a401d7

38 files changed

+6846
-1226
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/node_modules
22
/vendor
3+
/docs
4+
/.phpdoc
35
/.idea
46
/php.ini
57
.env

App/Beacon.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
declare(strict_types=1);
44

5-
namespace App;
5+
namespace Evolv;
66

7-
use App\EvolvContext;
7+
use Evolv\EvolvContext;
88

99
require __DIR__ . '/../vendor/autoload.php';
10-
require_once __DIR__ . '/EvolvStore.php';
11-
require_once __DIR__ . '/Beacon.php';
1210

1311
const ENDPOINT_PATTERN = "/\/(v\d+)\/\w+\/([a-z]+)$/i";
1412

@@ -31,8 +29,6 @@ private function send($payload) {
3129

3230
$data = json_encode($payload);
3331

34-
echo $this->endpoint;
35-
3632
$curl = curl_init();
3733

3834
curl_setopt($curl, CURLOPT_URL, $this->endpoint);
@@ -45,22 +41,9 @@ private function send($payload) {
4541
"Content-Type: application/json",
4642
);
4743
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
48-
49-
$data = <<<DATA
50-
$data
51-
DATA;
52-
53-
echo '<pre>';
54-
print_r($data);
55-
echo '</pre>';
56-
5744
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
5845

59-
$res = curl_exec($curl);
60-
61-
echo "<pre>";
62-
print_r($res);
63-
echo "</pre>";
46+
curl_exec($curl);
6447

6548
curl_close($curl);
6649

App/EvolvClient.php

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace App;
5+
namespace Evolv;
66

7-
use App\EvolvContext;
8-
use App\EvolvStore;
9-
use App\Beacon;
10-
11-
use function App\Utils\waitFor;
12-
use function App\Utils\emit;
7+
use function Evolv\Utils\waitFor;
8+
use function Evolv\Utils\emit;
9+
use Evolv\HttpClient;
1310

1411
require __DIR__ . '/../vendor/autoload.php';
15-
require_once __DIR__ . '/EvolvStore.php';
16-
require_once __DIR__ . '/Beacon.php';
17-
require_once __DIR__ . '/Utils/waitForIt.php';
1812

1913
class EvolvClient
2014
{
@@ -30,7 +24,13 @@ class EvolvClient
3024
private Beacon $contextBeacon;
3125
private Beacon $eventBeacon;
3226

33-
public function __construct(string $environment, string $endpoint = 'https://participants.evolv.ai/', bool $autoconfirm = true)
27+
/**
28+
* @param string $environment
29+
* @param string $endpoint
30+
* @param bool $autoconfirm
31+
* @return object
32+
*/
33+
public function __construct(string $environment, string $endpoint = 'https://participants.evolv.ai/', bool $autoconfirm = true, $llist = false)
3434
{
3535
$this->context = new EvolvContext();
3636
$this->store = new EvolvStore($environment, $endpoint);
@@ -39,29 +39,33 @@ public function __construct(string $environment, string $endpoint = 'https://par
3939
$this->eventBeacon = new Beacon($endpoint . 'v1/' . $environment . '/events', $this->context);
4040

4141
$this->autoconfirm = $autoconfirm;
42+
4243
}
4344

4445
/**
4546
* Initializes the client with required context information.
4647
*
47-
* @param {String} uid A globally unique identifier for the current participant.
48-
* @param {String} sid A globally unique session identifier for the current participant.
49-
* @param {Object} remoteContext A map of data used for evaluating context predicates and analytics.
50-
* @param {Object} localContext A map of data used only for evaluating context predicates.
48+
* @param string $uid A globally unique identifier for the current participant.
49+
* @param object $remoteContext A map of data used for evaluating context predicates and analytics.
50+
* @param object $localContext A map of data used only for evaluating context predicates.
51+
* @return array
5152
*/
5253

53-
public function initialize(string $uid, array $remoteContext = [], array $localContext = [])
54+
public function initialize(string $uid, array $remoteContext = [], array $localContext = [], $httpClient = null)
5455
{
56+
5557
if ($this->initialized) {
5658
throw new \Exception('Evolv: Client is already initialized');
59+
exit('Evolv: Client is already initialized');
5760
}
5861

5962
if (!$uid) {
6063
throw new \Exception('Evolv: "uid" must be specified');
64+
exit('Evolv: "uid" must be specified');
6165
}
6266

6367
$this->context->initialize($uid, $remoteContext, $localContext);
64-
$this->store->initialize($this->context);
68+
$this->store->initialize($this->context, $httpClient);
6569

6670
if ($this->autoconfirm) {
6771
$this->confirm();
@@ -113,10 +117,10 @@ public function initialize(string $uid, array $remoteContext = [], array $localC
113117
* * "contaminated" - Called when the consumer is contaminated (topic)
114118
* * "event.emitted" - Called when an event is emitted through the beacon (topic, type, score)
115119
*
116-
* @param string topic The event topic on which the listener should be invoked.
117-
* @param callable listener The listener to be invoked for the specified topic.
118-
* @method
119-
* @see {@link EvolvClient#once} for listeners that should only be invoked once.
120+
* @param string $topic The event topic on which the listener should be invoked.
121+
* @param callable $listener The listener to be invoked for the specified topic.
122+
* @function
123+
* @see EvolvClient for listeners that should only be invoked once.
120124
*/
121125
public function on(string $topic, callable $listener)
122126
{
@@ -126,9 +130,9 @@ public function on(string $topic, callable $listener)
126130
/**
127131
* Send an event to the events endpoint.
128132
*
129-
* @param {String} type The type associated with the event.
130-
* @param metadata {Object} Any metadata to attach to the event.
131-
* @param flush {Boolean} If true, the event will be sent immediately.
133+
* @param string $type The type associated with the event.
134+
* @param object $metadata Any metadata to attach to the event.
135+
* @param boolean $flush If true, the event will be sent immediately.
132136
*/
133137
public function emit(string $type, $metadata, bool $flush = false)
134138
{
@@ -143,10 +147,10 @@ public function emit(string $type, $metadata, bool $flush = false)
143147
/**
144148
* Check all active keys that start with the specified prefix.
145149
*
146-
* @param {String} prefix The prefix of the keys to check.
150+
* @param string $prefix The prefix of the keys to check.
147151
* @returns {SubscribablePromise.<Object|Error>} A SubscribablePromise that resolves to object
148152
* describing the state of active keys.
149-
* @method
153+
* @function
150154
*/
151155
public function getActiveKeys(string $prefix = '', callable $listener = null)
152156
{
@@ -156,9 +160,9 @@ public function getActiveKeys(string $prefix = '', callable $listener = null)
156160
/**
157161
* Get the value of a specified key.
158162
*
159-
* @param {String} key The key of the value to retrieve.
163+
* @param string $key The key of the value to retrieve.
160164
* @returns @mixed A value of the specified key.
161-
* @method
165+
* @function
162166
*/
163167
public function get(string $key = '', callable $listener = null)
164168
{
@@ -211,11 +215,11 @@ public function confirm()
211215
$this->context->update(['experiments' => ['confirmations' => $newConfirmations]]);
212216

213217
foreach ($confirmableAllocations as $alloc) {
214-
$this->eventBeacon->emit('confirmation', array_merge([
218+
$this->eventBeacon->emit('confirmation', [
215219
'uid' => $alloc['uid'],
216220
'eid' => $alloc['eid'],
217221
'cid' => $alloc['cid']
218-
], $this->context->remoteContext));
222+
]);
219223
};
220224

221225
$this->eventBeacon->flush();
@@ -227,9 +231,9 @@ public function confirm()
227231
* Marks a consumer as unsuccessfully retrieving and / or applying requested values, making them ineligible for
228232
* inclusion in optimization statistics.
229233
*
230-
* @param details {Object} Optional. Information on the reason for contamination. If provided, the object should
234+
* @param object $details Optional. Information on the reason for contamination. If provided, the object should
231235
* contain a reason. Optionally, a 'details' value should be included for extra debugging info
232-
* @param {boolean} allExperiments If true, the user will be excluded from all optimizations, including optimization
236+
* @param boolean $allExperiments If true, the user will be excluded from all optimizations, including optimization
233237
* not applicable to this page
234238
*/
235239
public function contaminate($details, bool $allExperiments = false)
@@ -267,12 +271,12 @@ public function contaminate($details, bool $allExperiments = false)
267271
$this->context->update(['experiments' => ['contaminations' => $newContaminations]]);
268272

269273
foreach ($contaminatableAllocations as $alloc) {
270-
$this->eventBeacon->emit('contamination', array_merge([
274+
$this->eventBeacon->emit('contamination', [
271275
'uid' => $alloc['uid'],
272276
'eid' => $alloc['eid'],
273277
'cid' => $alloc['cid'],
274278
'contaminationReason' => $details
275-
], $this->context->remoteContext));
279+
]);
276280
};
277281

278282
$this->eventBeacon->flush();

App/EvolvContext.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
declare(strict_types=1);
44

5-
namespace App;
5+
namespace Evolv;
66

7-
use function App\Utils\getValueForKey;
8-
use function App\Utils\setKeyToValue;
9-
use function App\Utils\removeValueForKey;
10-
use function App\Utils\emit;
11-
use function App\Utils\flatten;
7+
use function Evolv\Utils\getValueForKey;
8+
use function Evolv\Utils\setKeyToValue;
9+
use function Evolv\Utils\removeValueForKey;
10+
use function Evolv\Utils\emit;
11+
use function Evolv\Utils\flatten;
12+
13+
require_once __DIR__ . '/Utils/flatten.php';
1214
require_once __DIR__ . '/Utils/getValueForKey.php';
1315
require_once __DIR__ . '/Utils/setKeyToValue.php';
1416
require_once __DIR__ . '/Utils/removeValueForKey.php';
1517
require_once __DIR__ . '/Utils/waitForIt.php';
16-
require_once __DIR__ . '/Utils/flatten.php';
17-
1818

1919
const CONTEXT_CHANGED = 'context.changed';
2020
const CONTEXT_INITIALIZED = 'context.initialized';
@@ -108,7 +108,7 @@ public function set(string $key, $value, bool $local = false): void
108108
/**
109109
* Retrieve a value from the context.
110110
*
111-
* @param {String} key The kay associated with the value to retrieve.
111+
* @param string $key The kay associated with the value to retrieve.
112112
* @returns {*} The value associated with the specified key.
113113
*/
114114
public function get(string $key)
@@ -128,7 +128,7 @@ public function get(string $key)
128128
*
129129
* Note: This will cause the effective genome to be recomputed.
130130
*
131-
* @param key {String} The key to remove from the context.
131+
* @param string $key The key to remove from the context.
132132
*/
133133
public function remove(string $key)
134134
{
@@ -151,8 +151,8 @@ public function remove(string $key)
151151
*
152152
* Note: This will cause the effective genome to be recomputed.
153153
*
154-
* @param update {Object} The values to update the context with.
155-
* @param local {Boolean} If true, the values will only be added to the localContext.
154+
* @param array $update The values to update the context with.
155+
* @param boolean $local If true, the values will only be added to the localContext.
156156
*/
157157
public function update(array $update, $local = false) {
158158
$this->ensureInitialized();
@@ -187,8 +187,8 @@ public function update(array $update, $local = false) {
187187
/**
188188
* Checks if the specified key is currently defined in the context.
189189
*
190-
* @param key The key to check.
191-
* @returns {boolean} True if the key has an associated value in the context.
190+
* @param $key The key to check.
191+
* @returns bool True if the key has an associated value in the context.
192192
*/
193193
public function contains(string $key)
194194
{
@@ -200,11 +200,11 @@ public function contains(string $key)
200200
/**
201201
* Adds value to specified array in context. If array doesnt exist its created and added to.
202202
*
203-
* @param key The array to add to.
204-
* @param value Value to add to the array.
205-
* @param local {Boolean} If true, the value will only be added to the localContext.
206-
* @param limit {Number} Max length of array to maintain.
207-
* @returns {boolean} True if value was successfully added.
203+
* @param string $key The array to add to.
204+
* @param array $value Value to add to the array.
205+
* @param bool $local {Boolean} If true, the value will only be added to the localContext.
206+
* @param int $limit {Number} Max length of array to maintain.
207+
* @returns boolean True if value was successfully added.
208208
*/
209209
public function pushToArray(string $key, $value, $local = false, $limit = null)
210210
{

0 commit comments

Comments
 (0)