You are here

entity_share_server.rest.inc in Entity Share 7

Class for handling REST request.

File

modules/entity_share_server/includes/entity_share_server.rest.inc
View source
<?php

/**
 * @file
 * Class for handling REST request.
 */

/**
 * Class EntityShareServerRest.
 */
class EntityShareServerRest extends EntityShareServerRestAbstract {

  /**
   * Handle the GET method action.
   *
   * Read an entity by UUID.
   */
  protected function handleGet() {
    $entity_type = $this
      ->getParam('type');
    $entity_uuid = $this
      ->getParam('id');
    if (!empty($entity_type)) {
      $uuids = array();
      if (!empty($entity_uuid)) {
        $uuids[] = $entity_uuid;
      }
      $entities = entity_uuid_load($entity_type, $uuids);
      if (!empty($entity_uuid)) {
        if (empty($entities)) {
          $this
            ->setError('Entity not found', '404 Not Found');
        }
        else {
          $entity = reset($entities);
          $export = new EntityShareEntityExport($entity);
          $exported_entity = $export
            ->execute();
          $this
            ->setResult($exported_entity);
        }
      }
      else {

        // @todo Deal with list get.
        $this
          ->setResult($entities);
      }
    }
    else {

      // @todo Should we set a more precise error?
      $this
        ->setError('Incorrect parameters');
    }
  }

  /**
   * Handle the POST method action.
   *
   * Create a new entity.
   */
  protected function handlePost() {
    $request = $this
      ->getRequest();
    if (isset($request)) {
      $entity = $request['datas'];
      try {
        $import = new EntityShareEntityImport($entity);
        $entity_id = $import
          ->execute();
        if (!empty($entity_id)) {
          $this
            ->setResult(array(
            'entity_id' => $entity_id,
          ));
        }
        else {
          $this
            ->setError('Entity not created');
        }
      } catch (Exception $e) {
        $this
          ->setError('Error will trying to create entity', '500 Internal Server Error');
      }
    }
  }

  /**
   * Handle the PUT method action.
   *
   * Update an entity.
   */
  protected function handlePut() {
    $this
      ->handlePost();
  }

  /**
   * Handle the DELETE method action.
   *
   * Delete an entity by UUID.
   */
  protected function handleDelete() {
    try {
      $result = entity_uuid_delete($this
        ->getParam('type'), $this
        ->getParam('id'));
      $this
        ->setResult($result === FALSE ? FALSE : TRUE);
    } catch (Exception $e) {
      $this
        ->setError('Error will trying to delete entity', '500 Internal Server Error');
    }
  }

}

Classes

Namesort descending Description
EntityShareServerRest Class EntityShareServerRest.