|
| 1 | +<?php |
| 2 | + |
| 3 | +class SdkClientEntity |
| 4 | +{ |
| 5 | + private $_client; |
| 6 | + private $_logger; |
| 7 | + |
| 8 | + public function __construct($params) |
| 9 | + { |
| 10 | + $tag = $params['tag']; |
| 11 | + |
| 12 | + $logger = new Monolog\Logger('sdkclient'); |
| 13 | + $stream = new Monolog\Handler\StreamHandler('php://stderr', Monolog\Logger::DEBUG); |
| 14 | + $stream->setFormatter(new Monolog\Formatter\LineFormatter( |
| 15 | + "[%datetime%] %channel%.%level_name%: [$tag] %message%\n" |
| 16 | + )); |
| 17 | + $logger->pushHandler($stream); |
| 18 | + $this->_logger = $logger; |
| 19 | + |
| 20 | + $this->_client = self::createSdkClient($params, $logger); |
| 21 | + } |
| 22 | + |
| 23 | + public static function createSdkClient($params, $logger) |
| 24 | + { |
| 25 | + $config = $params['configuration']; |
| 26 | + |
| 27 | + $sdkKey = $config['credential']; |
| 28 | + $options = [ |
| 29 | + 'event_publisher' => LaunchDarkly\Integrations\Guzzle::eventPublisher(), |
| 30 | + 'logger' => $logger |
| 31 | + ]; |
| 32 | + |
| 33 | + $pollingConfig = $config['polling'] ?? []; |
| 34 | + $options['base_uri'] = self::adjustBaseUri($pollingConfig['baseUri'] ?? null); |
| 35 | + |
| 36 | + $options['send_events'] = ($config['events'] ?? null) !== null; |
| 37 | + $eventsConfig = $config['events'] ?? []; |
| 38 | + $options['events_uri'] = self::adjustBaseUri($eventsConfig['baseUri'] ?? null); |
| 39 | + $options['all_attributes_private'] = $eventsConfig['allAttributesPrivate'] ?? false; |
| 40 | + $options['private_attribute_names'] = $eventsConfig['globalPrivateAttributes'] ?? null; |
| 41 | + |
| 42 | + return new LaunchDarkly\LDClient($sdkKey, $options); |
| 43 | + } |
| 44 | + |
| 45 | + public function close() |
| 46 | + { |
| 47 | + // there isn't really any cleanup to do |
| 48 | + $this->_logger->info('Test ended'); |
| 49 | + } |
| 50 | + |
| 51 | + public function doCommand($reqParams) |
| 52 | + { |
| 53 | + $command = $reqParams['command']; |
| 54 | + $commandParams = $reqParams[$command] ?? null; |
| 55 | + switch ($command) { |
| 56 | + case 'customEvent': |
| 57 | + $this->doCustomEvent($commandParams); |
| 58 | + return null; |
| 59 | + |
| 60 | + case 'evaluate': |
| 61 | + return $this->doEvaluate($commandParams); |
| 62 | + |
| 63 | + case 'evaluateAll': |
| 64 | + return $this->doEvaluateAll($commandParams); |
| 65 | + |
| 66 | + case 'identifyEvent': |
| 67 | + $this->doIdentifyEvent($commandParams); |
| 68 | + return null; |
| 69 | + |
| 70 | + case 'flushEvents': |
| 71 | + $this->_client->flush(); |
| 72 | + return null; |
| 73 | + |
| 74 | + case 'secureModeHash': |
| 75 | + return $this->doSecureModeHash($commandParams); |
| 76 | + |
| 77 | + default: |
| 78 | + return false; // means invalid command |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static function adjustBaseUri($baseUri) |
| 83 | + { |
| 84 | + return $baseUri ? |
| 85 | + preg_replace('@^http://localhost:@', 'http://host.docker.internal:', $baseUri) |
| 86 | + : null; |
| 87 | + } |
| 88 | + |
| 89 | + private function doCustomEvent($params) |
| 90 | + { |
| 91 | + $this->_client->track( |
| 92 | + $params['eventKey'], |
| 93 | + $this->makeUser($params['user']), |
| 94 | + $params['data'] ?? null, |
| 95 | + $params['metricValue'] ?? null |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + private function doEvaluate($params) |
| 100 | + { |
| 101 | + $flagKey = $params['flagKey']; |
| 102 | + $user = $this->makeUser($params['user']); |
| 103 | + $defaultValue = $params['defaultValue'] ?? null; |
| 104 | + $detail = $params['detail'] ?? false; |
| 105 | + |
| 106 | + if ($detail) { |
| 107 | + $result = $this->_client->variationDetail($flagKey, $user, $defaultValue); |
| 108 | + return [ |
| 109 | + "value" => $result->getValue(), |
| 110 | + "variationIndex" => $result->getVariationIndex(), |
| 111 | + "reason" => $result->getReason() |
| 112 | + ]; |
| 113 | + } else { |
| 114 | + $value = $this->_client->variation($flagKey, $user, $defaultValue); |
| 115 | + return [ |
| 116 | + "value" => $value |
| 117 | + ]; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private function doEvaluateAll($params) |
| 122 | + { |
| 123 | + $options = []; |
| 124 | + foreach (['clientSideOnly', 'detailsOnlyForTrackedFlags', 'withReasons'] as $option) { |
| 125 | + if ($params[$option] ?: false) { |
| 126 | + $options[$option] = true; |
| 127 | + } |
| 128 | + } |
| 129 | + $state = $this->_client->allFlagsState($this->makeUser($params['user']), $options); |
| 130 | + return [ |
| 131 | + 'state' => $state->jsonSerialize() |
| 132 | + ]; |
| 133 | + } |
| 134 | + |
| 135 | + private function doIdentifyEvent($params) |
| 136 | + { |
| 137 | + $this->_client->identify($this->makeUser($params['user'])); |
| 138 | + } |
| 139 | + |
| 140 | + private function doSecureModeHash($params) |
| 141 | + { |
| 142 | + $user = $this->makeUser($params['user']); |
| 143 | + $result = $this->_client->secureModeHash($user); |
| 144 | + return [ |
| 145 | + 'result' => $result |
| 146 | + ]; |
| 147 | + } |
| 148 | + |
| 149 | + private function makeUser($data) |
| 150 | + { |
| 151 | + $privateAttributeNames = $data['privateAttributeNames'] ?? []; |
| 152 | + |
| 153 | + $builder = new LaunchDarkly\LDUserBuilder(isset($data['key']) ? $data['key'] : null); |
| 154 | + |
| 155 | + $secondary = $data['secondary'] ?? null; |
| 156 | + if (in_array('secondary', $privateAttributeNames)) { |
| 157 | + $builder->privateSecondary($secondary); |
| 158 | + } else { |
| 159 | + $builder->secondary($secondary); |
| 160 | + } |
| 161 | + |
| 162 | + $ip = $data['ip'] ?? null; |
| 163 | + if (in_array('ip', $privateAttributeNames)) { |
| 164 | + $builder->privateIp($ip); |
| 165 | + } else { |
| 166 | + $builder->ip($ip); |
| 167 | + } |
| 168 | + |
| 169 | + $country = $data['country'] ?? null; |
| 170 | + if (in_array('country', $privateAttributeNames)) { |
| 171 | + $builder->privateCountry($country); |
| 172 | + } else { |
| 173 | + $builder->country($country); |
| 174 | + } |
| 175 | + |
| 176 | + $email = $data['email'] ?? null; |
| 177 | + if (in_array('email', $privateAttributeNames)) { |
| 178 | + $builder->privateEmail($email); |
| 179 | + } else { |
| 180 | + $builder->email($email); |
| 181 | + } |
| 182 | + |
| 183 | + $name = $data['name'] ?? null; |
| 184 | + if (in_array('name', $privateAttributeNames)) { |
| 185 | + $builder->privateName($name); |
| 186 | + } else { |
| 187 | + $builder->name($name); |
| 188 | + } |
| 189 | + |
| 190 | + $avatar = $data['avatar'] ?? null; |
| 191 | + if (in_array('avatar', $privateAttributeNames)) { |
| 192 | + $builder->privateAvatar($avatar); |
| 193 | + } else { |
| 194 | + $builder->avatar($avatar); |
| 195 | + } |
| 196 | + |
| 197 | + $firstName = $data['firstName'] ?? null; |
| 198 | + if (in_array('firstName', $privateAttributeNames)) { |
| 199 | + $builder->privateFirstName($firstName); |
| 200 | + } else { |
| 201 | + $builder->firstName($firstName); |
| 202 | + } |
| 203 | + |
| 204 | + $lastName = $data['lastName'] ?? null; |
| 205 | + if (in_array('lastName', $privateAttributeNames)) { |
| 206 | + $builder->privateLastName($lastName); |
| 207 | + } else { |
| 208 | + $builder->lastName($lastName); |
| 209 | + } |
| 210 | + |
| 211 | + if (isset($data['anonymous'])) { |
| 212 | + $builder->anonymous($data['anonymous']); |
| 213 | + } |
| 214 | + |
| 215 | + if (isset($data['custom'])) { |
| 216 | + foreach ($data['custom'] as $key => $value) { |
| 217 | + if (in_array($key, $privateAttributeNames)) { |
| 218 | + $builder->privateCustomAttribute($key, $value); |
| 219 | + } else { |
| 220 | + $builder->customAttribute($key, $value); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + return $builder->build(); |
| 226 | + } |
| 227 | +} |
0 commit comments