You are here

ListenerPageController.php in Evercurrent 8

Same filename and directory in other branches
  1. 8.2 src/Controller/ListenerPageController.php

Contains \Drupal\example\Controller\ExamleController.

File

src/Controller/ListenerPageController.php
View source
<?php

/**
 * @file
 * Contains \Drupal\example\Controller\ExamleController.
 */
namespace Drupal\evercurrent\Controller;

use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Access\AccessResult;

/**
 * Creates a callback for listening to the server.
 */
class ListenerPageController {

  /**
   * Check for page access. Only if listening is set to true.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   * @return \Drupal\Core\Access\AccessResult
   */
  public function access(AccountInterface $account) {
    $config = \Drupal::config('evercurrent.admin_config');
    $listen = $config
      ->get('listen');
    return $listen ? AccessResult::allowed() : AccessResult::forbidden();
  }
  public function content() {
    $received = json_decode($_POST['data'], TRUE);
    $updateHelper = \Drupal::service('evercurrent.update_helper');

    // No key equals error response.
    if (!isset($received['key'])) {
      $severity = RMH_STATUS_ERROR;
      $message = 'Received an invalid request in listening mode.';
      $updateHelper
        ->writeStatus($severity, $message, $output = FALSE);
      return $this
        ->jsonResponse('Malformed data.', 'error');
    }
    $key = $received['key'];
    $result = $updateHelper
      ->testUpdate($key);
    if ($result == TRUE) {
      $this
        ->jsonResponse('Success.', 'status');
    }
    else {
      $this
        ->jsonResponse('Unknown error.', 'error');
    }
    return $this
      ->jsonResponse('Can you see this message?', 'error');
  }
  public function jsonResponse($message, $type) {
    return new JsonResponse([
      'data' => [
        'message' => $message,
        'type' => $type,
      ],
    ]);
  }

}

Classes

Namesort descending Description
ListenerPageController Creates a callback for listening to the server.