You are here

public function MonitoringMultigraphResource::get in Monitoring 8

Responds to multigraph GET requests.

Parameters

string $id: (optional) The multigraph name, returns a list of all multigraphs when empty.

Return value

\Drupal\rest\ResourceResponse The response containing the multigraph.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException

File

modules/multigraph/src/Plugin/rest/resource/MonitoringMultigraphResource.php, line 64
Contains \Drupal\monitoring_multigraph\Plugin\rest\resource\MonitoringMultigraphResource.

Class

MonitoringMultigraphResource
Provides a resource for monitoring multigraphs.

Namespace

Drupal\monitoring_multigraph\Plugin\rest\resource

Code

public function get($id = NULL) {
  $request = \Drupal::request();
  $format = $request
    ->getRequestFormat('ĵson');
  if ($id) {

    /** @var \Drupal\monitoring_multigraph\Entity\Multigraph $multigraph */
    $multigraph = \Drupal::entityTypeManager()
      ->getStorage('monitoring_multigraph')
      ->load($id);
    if ($multigraph == NULL) {
      throw new NotFoundHttpException('No multigraph with name "' . $id . '"');
    }
    $response = $multigraph
      ->getDefinition();
    $url = Url::fromRoute('rest.monitoring-multigraph.GET', [
      'id' => $id,
      '_format' => $format,
    ])
      ->setAbsolute()
      ->toString(TRUE);
    $response['uri'] = $url
      ->getGeneratedUrl();
    $response = new ResourceResponse($response);
    $response
      ->addCacheableDependency($multigraph);
    $response
      ->addCacheableDependency($url);
    return $response;
  }
  $list = array();
  $multigraphs = \Drupal::entityTypeManager()
    ->getStorage('monitoring_multigraph')
    ->loadMultiple();
  $cacheable_metadata = new CacheableMetadata();
  foreach ($multigraphs as $name => $multigraph) {

    /** @var \Drupal\monitoring_multigraph\Entity\Multigraph $multigraph */
    $list[$name] = $multigraph
      ->getDefinition();
    $url = Url::fromRoute('rest.monitoring-multigraph.GET', [
      'id' => $name,
      '_format' => $format,
    ])
      ->setAbsolute()
      ->toString(TRUE);
    $list[$name]['uri'] = $url
      ->getGeneratedUrl();
    $cacheable_metadata = $cacheable_metadata
      ->merge($url);
    $cacheable_metadata = $cacheable_metadata
      ->merge(CacheableMetadata::createFromObject($multigraph));
  }
  $response = new ResourceResponse($list);
  return $response
    ->addCacheableDependency($cacheable_metadata);
}