-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathEngine.php
More file actions
69 lines (61 loc) · 2.32 KB
/
Engine.php
File metadata and controls
69 lines (61 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace SplitIO;
use SplitIO\Grammar\Split as SplitGrammar;
use SplitIO\Grammar\Condition;
use SplitIO\Engine\Splitter;
use SplitIO\Grammar\Condition\ConditionTypeEnum;
use SplitIO\Sdk\Impressions\ImpressionLabel;
use SplitIO\Component\Common\Di;
class Engine
{
const EVALUATION_RESULT_TREATMENT = 'treatment';
const EVALUATION_RESULT_LABEL = 'label';
/**
* @param $matchingKey
* @param $bucketingKey
* @param SplitGrammar $split
* @param array|null $attributes
* @return array
*/
public static function getTreatment($matchingKey, $bucketingKey, SplitGrammar $split, ?array $attributes = null)
{
if ($bucketingKey === null) {
$bucketingKey = $matchingKey;
}
$conditions = $split->getConditions();
$result = array(
self::EVALUATION_RESULT_TREATMENT => null,
self::EVALUATION_RESULT_LABEL => null
);
$inRollOut = false;
foreach ($conditions as $condition) {
if (!$inRollOut && $condition->getConditionType() == ConditionTypeEnum::ROLLOUT) {
if ($split->getTrafficAllocation() < 100) {
$bucket = Di::get('splitter')->getBucket(
$split->getAlgo(),
$bucketingKey,
$split->getTrafficAllocationSeed()
);
if ($bucket > $split->getTrafficAllocation()) {
$result[self::EVALUATION_RESULT_LABEL] = ImpressionLabel::NOT_IN_SPLIT;
$result[self::EVALUATION_RESULT_TREATMENT] = $split->getDefaultTratment();
return $result;
}
$inRollOut = true;
}
}
if ($condition->match($matchingKey, $attributes, $bucketingKey)) {
$result[self::EVALUATION_RESULT_TREATMENT] = Di::get('splitter')->getTreatment(
$bucketingKey,
$split->getSeed(),
$condition->getPartitions(),
$split->getAlgo()
);
$result[self::EVALUATION_RESULT_LABEL] = $condition->getLabel();
//Return the first condition that match.
return $result;
}
}
return $result;
}
}