Skip to content

Commit c37e32e

Browse files
committed
feat(Status): refactored to new polling service
1 parent d29612f commit c37e32e

4 files changed

Lines changed: 61 additions & 41 deletions

File tree

rest/app/Http/Controllers/ConsumePollingServiceController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,21 @@ public function reward(Request $request)
5757
// return consume status
5858
return ['status' => $success];
5959
}
60+
61+
/**
62+
* consume polling service for OathKeeper
63+
*
64+
* @return \Illuminate\Http\JsonResponse
65+
*/
66+
public function status(Request $request)
67+
{
68+
// get body of array and convert it to object
69+
$transaction = array_to_object($request->all());
70+
71+
// process transaction
72+
$success = PollingHelper::processStatusEvent($transaction);
73+
74+
// return consume status
75+
return ['status' => $success];
76+
}
6077
}

rest/app/Libraries/PollingHelper.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use \App\Models\RewardActivity;
99
use \App\Models\RoleContract;
1010
use \App\Models\Slot;
11+
use \App\Models\Status;
1112

1213
class PollingHelper
1314
{
@@ -109,4 +110,29 @@ public static function processRewardEvent($transaction)
109110
return $success;
110111
}
111112

113+
public static function processStatusEvent($transaction)
114+
{
115+
// intial status for success
116+
$success = -1;
117+
118+
switch ($transaction->event_name) {
119+
// StatusAdded event
120+
case 'StatusAdded':
121+
$success = Status::statusAdded($transaction);
122+
break;
123+
124+
// StateChanged event
125+
case 'StateChanged':
126+
$success = Status::stateChanged($transaction);
127+
break;
128+
129+
// StatusTypeChanged event
130+
case 'StatusTypeChanged':
131+
$success = Status::statusTypeChanged($transaction);
132+
break;
133+
}
134+
135+
return $success;
136+
}
137+
112138
}

rest/app/Models/Status.php

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,16 @@ public function scopeFilters($query, $filters)
2323
}
2424

2525
/**
26-
* Consume AMQP
26+
* create Reward when `StatusAdded` event triggered
2727
*
28-
* @param Object $payload: payload data send by AMQP server
28+
* @param Object $payload: payload data send by Polling-Server
2929
* @return Boolean the success or failure message
3030
*/
31-
public static function consumeAMQP($payload)
31+
public static function statusAdded($payload)
3232
{
33-
$saved = false;
33+
// get data object
34+
$data = $payload->data;
3435

35-
switch ($payload->eventIdentifier) {
36-
37-
// StatusAdded event
38-
case 'StatusAdded':
39-
$saved = Status::store($payload->data);
40-
break;
41-
42-
// StateChanged event
43-
case 'StateChanged':
44-
$saved = Status::updateState($payload->data);
45-
break;
46-
47-
// StatusTypeChanged event
48-
case 'StatusTypeChanged':
49-
$saved = Status::updateStatusType($payload->data);
50-
break;
51-
52-
}
53-
54-
// Return process status
55-
return $saved;
56-
}
57-
58-
/**
59-
* Store status
60-
*
61-
* @param Object $data: payload data send by AMQP server
62-
* @return Boolean the success or failure message
63-
*/
64-
public static function store($data)
65-
{
6636
$exists = Status::where(['wallet' => $data->statusHolder])->first();
6737

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

8656
/**
87-
* Update state of a Status
57+
* create Reward when `StateChanged` event triggered
8858
*
89-
* @param Object $data: payload data send by AMQP server
59+
* @param Object $payload: payload data send by Polling-Server
9060
* @return Boolean the success or failure message
9161
*/
92-
public static function updateState($data)
62+
public static function stateChanged($payload)
9363
{
64+
// get data object
65+
$data = $payload->data;
66+
9467
$status = Status::where(['wallet' => $data->statusHolder])->first();
9568

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

10679
/**
107-
* Update type of a Status
80+
* create Reward when `StatusTypeChanged` event triggered
10881
*
109-
* @param Object $data: payload data send by AMQP server
82+
* @param Object $payload: payload data send by Polling-Server
11083
* @return Boolean the success or failure message
11184
*/
112-
public static function updateStatusType($data)
85+
public static function statusTypeChanged($payload)
11386
{
87+
// get data object
88+
$data = $payload->data;
89+
11490
$status = Status::where(['wallet' => $data->statusHolder])->first();
11591

11692
if (!isset($status)) {

rest/routes/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
$api->post('oath-keeper', 'App\Http\Controllers\ConsumePollingServiceController@oathKeeper');
99
$api->post('advocate', 'App\Http\Controllers\ConsumePollingServiceController@advocate');
1010
$api->post('reward', 'App\Http\Controllers\ConsumePollingServiceController@reward');
11+
$api->post('status', 'App\Http\Controllers\ConsumePollingServiceController@status');
1112
});
1213

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

0 commit comments

Comments
 (0)