Skip to content

Commit

Permalink
Merge pull request #7 from Moesif/feature-update-users
Browse files Browse the repository at this point in the history
Add: updateUser functions
  • Loading branch information
dgilling authored Mar 26, 2019
2 parents 56d8dfb + 4dd1bbb commit b27618c
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 17 deletions.
72 changes: 57 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,66 @@ this API call should be not be sent to Moesif.
Type: `Boolean`
Optional, If true, will print debug messages using Illuminate\Support\Facades\Log

## Convenience method for updateUser
## updateUser

If you are updating the [user profile](https://www.moesif.com/docs/getting-started/users/) to get visibility. You can use the `updateUser` method. This is not part of the Middleware, as this is a method you can invoke anytime you have the
user's profile, such as when the profile is created or updated.
If you are updating the [user profile](https://www.moesif.com/docs/getting-started/users/) to get visibility. You can use the `updateUser` method. This method is attached to the moesif middleware object to update the users profile or metadata.

```php
use Moesif\Sender\MoesifApi;

$moesifApi = MoesifApi::getInstance('your application id', ['fork'=>true, 'debug'=>true]);

$moesifApi->updateUser([
'user_id' => 'joe123',
'metadata' => [
'name' => 'joe',
'email' => '[email protected]',
'special_value' => 'abcdefg'
]
]);
use Moesif\Middleware\MoesifLaravel;

$user = array(
"user_id" => "phpapiuser",
"metadata" => array(
"email" => "[email protected]",
"string_field" => "value_1",
"number_field" => 0,
"object_field" => array(
"field_a" => "value_a",
"field_b" => "value_b"
)
),
);
$middleware = new MoesifLaravel();
$middleware->updateUser($user);
// the user_id field is required.

```

The `metadata` field can be any custom data you want to set on the user. The `user_id` field is required.

## updateUsersBatch

If you are updating the [user profile](https://www.moesif.com/docs/getting-started/users/) to get visibility. You can use the `updateUsersBatch` method. This method is attached to the moesif middleware object to update the users profile or metadata in batch.

```php
use Moesif\Middleware\MoesifLaravel;

$metadata = array(
"email" => "[email protected]",
"string_field" => "value_1",
"number_field" => 0,
"object_field" => array(
"field_a" => "value_a",
"field_b" => "value_b"
)
);

$userA = array(
"user_id" => "phpapiuser",
"metadata" => $metadata,
);

$userB = array(
"user_id" => "phpapiuser1",
"metadata" => $metadata,
);

$users = array();
$users[] = $userA;
$users[] = $userB;

$middleware = new MoesifLaravel();
$middleware->updateUsersBatch($users);
// the user_id field is required.

```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"apimatic/jsonmapper": "~1.0.0"
},
"require-dev": {
"phpunit/phpunit": "4.8.*"
"phpunit/phpunit": "~7.0"
},
"autoload": {
"psr-4": {
Expand Down
60 changes: 59 additions & 1 deletion src/Moesif/Middleware/MoesifLaravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,64 @@ function guidv4($data)
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

/**
* Get Client Ip Address.
*/
function getIp(){
foreach (array('HTTP_X_CLIENT_IP', 'HTTP_CF_CONNECTING_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_TRUE_CLIENT_IP',
'HTTP_X_REAL_IP', 'HTTP_X_REAL_IP', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
if (array_key_exists($key, $_SERVER) === true){
foreach (explode(',', $_SERVER[$key]) as $ip){
$ip = trim($ip); // just to be safe
if (strpos($ip, ':') !== false) {
$ip = array_values(explode(':', $ip))[0];
}
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
return $ip;
}
}
}
}
}

/**
* Update user.
*/
public function updateUser($userData){
$applicationId = config('moesif.applicationId');
$debug = config('moesif.debug');

if (is_null($applicationId)) {
throw new Exception('ApplicationId is missing. Please provide applicationId in moesif.php in config folder.');
}

if (is_null($debug)) {
$debug = false;
}

$moesifApi = MoesifApi::getInstance($applicationId, ['fork'=>true, 'debug'=>$debug]);
$moesifApi->updateUser($userData);
}

/**
* Update users in batch.
*/
public function updateUsersBatch($userData){
$applicationId = config('moesif.applicationId');
$debug = config('moesif.debug');

if (is_null($applicationId)) {
throw new Exception('ApplicationId is missing. Please provide applicationId in moesif.php in config folder.');
}

if (is_null($debug)) {
$debug = false;
}

$moesifApi = MoesifApi::getInstance($applicationId, ['fork'=>true, 'debug'=>$debug]);
$moesifApi->updateUsersBatch($userData);
}

/**
* Function for basic field validation (present and neither empty nor only white space.
*/
Expand Down Expand Up @@ -95,7 +153,7 @@ public function handle($request, Closure $next)
'time' => $startDateTime->format('Y-m-d\TH:i:s.uP'),
'verb' => $request->method(),
'uri' => $request->fullUrl(),
'ip_address' => $request->ip()
'ip_address' => $this->getIp()
];

if (!is_null($apiVersion)) {
Expand Down
23 changes: 23 additions & 0 deletions src/Moesif/Sender/MoesifApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ public function updateUser($userData) {

$this->_sendProducer->updateUser($userData);
}

/**
* Updates Users in batch.
* @param array of userData
* @throws Exception
*/
public function updateUsersBatch($usersBatchData = array()) {
$users = array();

foreach($usersBatchData as $userData) {
if (is_null($userData)) {
throw new Exception('Moesif UpdateUser with a null userData object');
}

if (!isset($userData['user_id'])) {
throw new Exception('Moesif updateUser requires user_id field to be set');
}
$users[] = $userData;
}

$this->_sendProducer->updateUsersBatch($users);
}

/**
* Empty the events queue
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Moesif/Sender/SendCurlTaskConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function __construct($applicationId, $options) {
$this->_host = $options['host'];
$this->_endpoint = $options['endpoint'];
$this->_users_endpoint = $options['users_endpoint'];
$this->_users_batch_endpoint = $options['users_batch_endpoint'];
$this->_connect_timeout = array_key_exists('connect_timeout', $options) ? $options['connect_timeout'] : 5;
$this->_timeout = array_key_exists('timeout', $options) ? $options['timeout'] : 30;
$this->_protocol = array_key_exists('use_ssl', $options) && $options['use_ssl'] == true ? "https" : "http";
Expand Down Expand Up @@ -100,6 +101,20 @@ public function updateUser($userData) {
}
}

/**
* Write to the user endpoint.
*/
public function updateUsersBatch($usersBatchData) {

$data = $this->_encode($usersBatchData);
$url = $this->_protocol . "://" . $this->_host . $this->_users_batch_endpoint;
if ($this->_fork) {
return $this->_execute_forked($url, $data);
} else {
return $this->_execute($url, $data);
}
}

/**
* Write using the cURL php extension
* @param $url
Expand Down
7 changes: 7 additions & 0 deletions src/Moesif/Sender/SendTaskConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ abstract function persist($batch);
* @return boolean success or fail.
*/
abstract function updateUser($userData);

/**
* Update users batch data
* @params array $batch an array of userData
* @return boolean success or fail.
*/
abstract function updateUsersBatch($usersBatchData);
}
15 changes: 15 additions & 0 deletions src/Moesif/Sender/SendTaskProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ protected function _getConsumer() {
}
$this->_options['endpoint'] = $this->_getEndpoint();
$this->_options['users_endpoint'] = $this->_getUsersEndpoint();
$this->_options['users_batch_endpoint'] = $this->_getUsersBatchEndpoint();
return new SendCurlTaskConsumer($this->_appId, $this->_options);
//return new $Strategy($this->_appId, $this->_options);
}
Expand Down Expand Up @@ -177,6 +178,16 @@ protected function _persist($message) {
public function updateUser($userData) {
return $this->_consumer->updateUser($userData);
}

/**
* Given an array of messages, persist it with the instantiated Persistence Strategy
* @param $userData
* @return mixed
*/
public function updateUsersBatch($usersBatchData) {
return $this->_consumer->updateUsersBatch($usersBatchData);
}

/**
* Return the endpoint that should be used by a consumer that consumes messages produced by this producer.
* @return string
Expand All @@ -189,6 +200,10 @@ public function _getUsersEndpoint() {
return '/v1/users';
}

public function _getUsersBatchEndpoint() {
return '/v1/users/batch';
}

/**
* Track an event defined by $event associated with metadata defined by $properties
* @param string $data
Expand Down

0 comments on commit b27618c

Please sign in to comment.