Skip to content

Commit

Permalink
feat(Status): refactored to new polling service
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmsuhail committed Jul 21, 2020
1 parent d29612f commit c37e32e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 41 deletions.
17 changes: 17 additions & 0 deletions rest/app/Http/Controllers/ConsumePollingServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,21 @@ public function reward(Request $request)
// return consume status
return ['status' => $success];
}

/**
* consume polling service for OathKeeper
*
* @return \Illuminate\Http\JsonResponse
*/
public function status(Request $request)
{
// get body of array and convert it to object
$transaction = array_to_object($request->all());

// process transaction
$success = PollingHelper::processStatusEvent($transaction);

// return consume status
return ['status' => $success];
}
}
26 changes: 26 additions & 0 deletions rest/app/Libraries/PollingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use \App\Models\RewardActivity;
use \App\Models\RoleContract;
use \App\Models\Slot;
use \App\Models\Status;

class PollingHelper
{
Expand Down Expand Up @@ -109,4 +110,29 @@ public static function processRewardEvent($transaction)
return $success;
}

public static function processStatusEvent($transaction)
{
// intial status for success
$success = -1;

switch ($transaction->event_name) {
// StatusAdded event
case 'StatusAdded':
$success = Status::statusAdded($transaction);
break;

// StateChanged event
case 'StateChanged':
$success = Status::stateChanged($transaction);
break;

// StatusTypeChanged event
case 'StatusTypeChanged':
$success = Status::statusTypeChanged($transaction);
break;
}

return $success;
}

}
58 changes: 17 additions & 41 deletions rest/app/Models/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,16 @@ public function scopeFilters($query, $filters)
}

/**
* Consume AMQP
* create Reward when `StatusAdded` event triggered
*
* @param Object $payload: payload data send by AMQP server
* @param Object $payload: payload data send by Polling-Server
* @return Boolean the success or failure message
*/
public static function consumeAMQP($payload)
public static function statusAdded($payload)
{
$saved = false;
// get data object
$data = $payload->data;

switch ($payload->eventIdentifier) {

// StatusAdded event
case 'StatusAdded':
$saved = Status::store($payload->data);
break;

// StateChanged event
case 'StateChanged':
$saved = Status::updateState($payload->data);
break;

// StatusTypeChanged event
case 'StatusTypeChanged':
$saved = Status::updateStatusType($payload->data);
break;

}

// Return process status
return $saved;
}

/**
* Store status
*
* @param Object $data: payload data send by AMQP server
* @return Boolean the success or failure message
*/
public static function store($data)
{
$exists = Status::where(['wallet' => $data->statusHolder])->first();

info($exists);
Expand All @@ -84,13 +54,16 @@ public static function store($data)
}

/**
* Update state of a Status
* create Reward when `StateChanged` event triggered
*
* @param Object $data: payload data send by AMQP server
* @param Object $payload: payload data send by Polling-Server
* @return Boolean the success or failure message
*/
public static function updateState($data)
public static function stateChanged($payload)
{
// get data object
$data = $payload->data;

$status = Status::where(['wallet' => $data->statusHolder])->first();

if (!isset($status)) {
Expand All @@ -104,13 +77,16 @@ public static function updateState($data)
}

/**
* Update type of a Status
* create Reward when `StatusTypeChanged` event triggered
*
* @param Object $data: payload data send by AMQP server
* @param Object $payload: payload data send by Polling-Server
* @return Boolean the success or failure message
*/
public static function updateStatusType($data)
public static function statusTypeChanged($payload)
{
// get data object
$data = $payload->data;

$status = Status::where(['wallet' => $data->statusHolder])->first();

if (!isset($status)) {
Expand Down
1 change: 1 addition & 0 deletions rest/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
$api->post('oath-keeper', 'App\Http\Controllers\ConsumePollingServiceController@oathKeeper');
$api->post('advocate', 'App\Http\Controllers\ConsumePollingServiceController@advocate');
$api->post('reward', 'App\Http\Controllers\ConsumePollingServiceController@reward');
$api->post('status', 'App\Http\Controllers\ConsumePollingServiceController@status');
});

$api->group(['middleware' => 'wallet.auth'], function ($api) {
Expand Down

0 comments on commit c37e32e

Please sign in to comment.