<?php

/**
 * Created by PhpStorm.
 * User: benny
 * Date: 05.04.2016
 * Time: 09:22
 */
abstract class AHDB_Core_RestService
{
    const STATUS_OK = 200;
    const STATUS_CREATED = 201;
    const STATUS_NO_CONTENT = 204;

    const STATUS_BAD_REQUEST = 400;
    const STATUS_UNAUTHORIZED = 401;    // Requires a HTTP Authentificate header (e.g. no valid session at all)
    const STATUS_FORBIDDEN = 403;       // Authorization will not help, use this if a user needs a complete different role etc.
    const STATUS_NOT_FOUND = 404;
    const STATUS_CONFLICT = 409;

    const STATUS_INTERNAL_SERVER_ERROR = 500;

    protected $params;

    public function __construct( array $params = array() )
    {
        $this->params = new AHDB_Core_RequestData( $params );
    }

    /**
     * Retrieve information. GET requests must be safe and idempotent, meaning regardless of how many times it repeats with the same parameters, the results are the same. They can have side effects, but the user doesn't expect them, so they cannot be critical to the operation of the system. Requests can also be partial or conditional.
     */
    public function get()
    {
        throw new Exception( 'Service method not available.', self::STATUS_BAD_REQUEST );
    }

    /**
     * Request that the resource at the URI do something with the provided entity. Often POST is used to create a new entity, but it can also be used to update an entity.
     */
    public function post()
    {
        throw new Exception( 'Service method not available.', self::STATUS_BAD_REQUEST );
    }

    /**
     * Store an entity at a URI. PUT can create a new entity or update an existing one. A PUT request is idempotent. Idempotency is the main difference between the expectations of PUT versus a POST request.
     * Note: PUT replaces an existing entity. If only a subset of data elements are provided, the rest will be replaced with empty or null.
     */
    public function put()
    {
        throw new Exception( 'Service method not available.', self::STATUS_BAD_REQUEST );
    }

    /**
     * Update only the specified fields of an entity at a URI. A PATCH request is idempotent. Idempotency is the main difference between the expectations of PUT versus a POST request.
     */
    public function patch()
    {
        throw new Exception( 'Service method not available.', self::STATUS_BAD_REQUEST );
    }

    /**
     * Request that a resource be removed; however, the resource does not have to be removed immediately. It could be an asynchronous or long-running request.
     */
    public function delete()
    {
        throw new Exception( 'Service method not available.', self::STATUS_BAD_REQUEST );
    }

    public function throwBadRequest( $msg = "" )
    {
        throw new Exception( $msg, self::STATUS_BAD_REQUEST );
    }

    public function throwUnauthorized( $msg = "" )
    {
        throw new Exception( $msg, self::STATUS_UNAUTHORIZED );
    }

    public function throwForbidden( $msg = "" )
    {
        throw new Exception( $msg, self::STATUS_FORBIDDEN );
    }

    public function throwNotFound( $msg = "" )
    {
        throw new Exception( $msg, self::STATUS_NOT_FOUND );
    }

    public function throwConflict( $msg = "" )
    {
        throw new Exception( $msg, self::STATUS_CONFLICT );
    }
}