<?php
namespace SabaiApps\Framework\Application;

/**
 * Abstract class representing a response.
 * Provides the interface for sending responses based on context status.
 */
abstract class AbstractResponse {

	/**
	 * Reference to the application instance.
	 *
	 * @var AbstractApplication
	 */
	protected $_application;

	/**
	 * Sets the application instance for this response.
	 *
	 * @param AbstractApplication $application The application instance.
	 * @return $this
	 */
	final public function setApplication( AbstractApplication $application ) {
		$this->_application = $application;

		return $this;
	}

	/**
	 * Sends the response based on the context status.
	 * Calls the appropriate method depending on the status.
	 *
	 * @param Context $context The application context.
	 * @return mixed
	 */
	public function send( Context $context ) {
		switch ( $context->getStatus() ) {
			case Context::STATUS_SUCCESS:
				return $this->_sendSuccess( $context );
			case Context::STATUS_ERROR:
				return $this->_sendError( $context );
			default:
				return $this->_sendView( $context );
		}
	}

	/**
	 * Sends a successful response.
	 * Must be implemented by subclasses.
	 *
	 * @param Context $context The application context.
	 * @return mixed
	 */
	abstract protected function _sendSuccess( Context $context );

	/**
	 * Sends an error response.
	 * Must be implemented by subclasses.
	 *
	 * @param Context $context The application context.
	 * @return mixed
	 */
	abstract protected function _sendError( Context $context );

	/**
	 * Sends a view response (default).
	 * Must be implemented by subclasses.
	 *
	 * @param Context $context The application context.
	 * @return mixed
	 */
	abstract protected function _sendView( Context $context );
}
