Skip to content

Commit

Permalink
Add any operators online and JSONP support
Browse files Browse the repository at this point in the history
closes #1, closes #2
  • Loading branch information
everyx committed Mar 18, 2017
1 parent e532b91 commit 8e5d1f6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
59 changes: 44 additions & 15 deletions Controller/OperatorStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Mibew\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
* Operator Status actions
Expand All @@ -37,30 +38,58 @@ class OperatorStatusController extends AbstractController
/**
* Returns true or false of whether an operator is online or not.
*
* @param Request $request
* @param Request $request
* @return Response Rendered page content
*/
public function indexAction(Request $request)
public function isOperatorOnlineAction(Request $request)
{
$is_online = "true";
$is_online = false;

$opcode = $request->attributes->get('opcode');
$online_operators = get_online_operators();

if ( count($online_operators) == 0 ) {
$is_online = "false";
} else if ( !empty($opcode) ) {
$is_online = "false";
foreach ($online_operators as $item) {
if ($item['code'] == $opcode) {
$is_online = "true";
break;
}
foreach ($online_operators as $item) {
if ($opcode == $item['code']) {
$is_online = true;
break;
}
}

$response = new Response($is_online);
$response->headers->set('Access-Control-Allow-Origin', '*');
$callback = $request->query->get('callback');
return $this->prepareResponse($is_online, $callback);
}

/**
* Returns true or false of whether an operator is online or not.
*
* @param Request $request
* @return Response Rendered page content
*/
public function hasOnlineOperatorsAction(Request $request)
{
$is_online = has_online_operators();

$callback = $request->query->get('callback');
return $this->prepareResponse($is_online, $callback);
}

/**
* Returns prepared response: JSONP or plain text.
*
* @param Boolean $is_online
* @param String $callback
* @return Response Rendered page content
*/
private function prepareResponse($is_online, $callback) {
$response = NULL;

if ( empty($callback) ) {
$response = new Response( $is_online ? 'true' : 'false' );
$response->headers->set('Access-Control-Allow-Origin', '*');
} else {
$response = new JsonResponse($is_online);
$response->setCallback($callback);
}

return $response;
}
}
2 changes: 1 addition & 1 deletion Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public function run()
*/
public static function getVersion()
{
return '0.2.0';
return '0.3.0';
}
}
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,28 @@ Plugin for Mibew, get statement based on the availability of operators.

# Useage

Request `<MIBEW-BASE-URL>/opstatus/<OPERATOR-CODE>`, your will get `true` when operator
is online or `false` when operator is offline.
1. Get any operators online status:

* request URL:`<MIBEW-BASE-URL>/opstatus`.
* return `true` when any operators is online and `false` when not.

2. Get an operator online status by operator code:

* Request URL: `<MIBEW-BASE-URL>/opstatus/<OPERATOR-CODE>`.
* return `true` when operator is online or `false` when not.

3. Use callback parameter:

Just inset `<script>` tag and set `src` to URL above and add `callback` parameter

* `<MIBEW-BASE-URL>/opstatus?callback=<CALLBACK_FUNCTION>`
* `<MIBEW-BASE-URL>/opstatus/<OPERATOR-CODE>?callback=<CALLBACK_FUNCTION>`

will return bellow and run `CALLBACK_FUNCTION` automatically.

```javascript
/**/CALLBACK_FUNCTION(status);
```

# Install

Expand Down
9 changes: 7 additions & 2 deletions routing.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
everyx_operator_status:
everyx_operator_status_has_online_operators:
path: /opstatus
defaults:
_controller: Everyx\Mibew\Plugin\OperatorStatus\Controller\OperatorStatusController::hasOnlineOperatorsAction

everyx_operator_status_is_operator_online:
path: /opstatus/{opcode}
defaults:
_controller: Everyx\Mibew\Plugin\OperatorStatus\Controller\OperatorStatusController::indexAction
_controller: Everyx\Mibew\Plugin\OperatorStatus\Controller\OperatorStatusController::isOperatorOnlineAction

0 comments on commit 8e5d1f6

Please sign in to comment.