You are here

class NodejsPages in Node.js integration 8

Returns responses for Node.js routes.

Hierarchy

Expanded class hierarchy of NodejsPages

File

src/Controller/NodejsPages.php, line 11

Namespace

Drupal\nodejs\Controller
View source
class NodejsPages {

  /**
   * @todo .
   */
  public function messageHandler() {
    if (!isset($_POST['serviceKey']) || !nodejs_is_valid_service_key($_POST['serviceKey'])) {
      return new JsonResponse(array(
        'error' => 'Invalid service key.',
      ), 404);
    }
    if (!isset($_POST['messageJson'])) {
      return new JsonResponse(array(
        'error' => 'No message.',
      ), 400);
    }
    $message = Json::decode($_POST['messageJson']);
    $response = array();
    switch ($message['messageType']) {
      case 'authenticate':
        $response = nodejs_auth_check($message);
        break;
      case 'userOffline':
        nodejs_user_set_offline($message['uid']);
        $response = array(
          'status' => 'success',
        );
        break;
      case 'userOnline':
        nodejs_user_set_online($message['uid']);
        $response = array(
          'status' => 'success',
        );
        break;
      default:
        $handlers = array();
        foreach (\Drupal::moduleHandler()
          ->invokeAll('nodejs_message_callback', array(
          $message['messageType'],
        )) as $handler) {
          $handlers[] = $handler;
        }
        foreach ($handlers as $callback) {
          $callback($message, $response);
        }
    }
    \Drupal::moduleHandler()
      ->alter('nodejs_message_response', $response, $message);
    if (!empty($response)) {
      return new JsonResponse($response);
    }
    else {
      return new JsonResponse(array(
        'error' => 'Not implemented',
      ), 400);
    }
  }

}

Members