<?php
namespace Modules\Orders\ImportedOrders\Webservice\Model\ExecOperation;

abstract class GeneralAbstract
{
	/**
	 * The original request.
	 * @var \stdClass
	 */
	protected $request;

	/**
	 * The response response.
	 * @var \Modules\Orders\ImportedOrders\Webservice\Model\Response
	 */
	protected $response;

	/**
	 * Associates the request and extracts the entity operation.
	 * @param \stdClass $request
	 * @param \Modules\RemoteData\EntityManagementWs\Model\Response $response
	 */
	public function __construct(array $R, \stdClass $request, \Modules\Orders\ImportedOrders\Webservice\Model\Response $response)
	{
		$this->request = 		$request;
		$this->response = 		$response;
	}

	/**
	 * The main operation execution, broken down into checking and the actual execution.
	 */
	public function executeOperation()
	{

		$this->checkAuth();

		$this->checkRequest();

		$this->executeRequestedOperation();

	}

	/**
	 * Checks if the request is valid in terms of session token.
	 * @throws \RuntimeException - if the auth data is not valid.
	 */
	protected function checkAuth()
	{

		try {
			$sessionTokenObject = new \Modules\Orders\ImportedOrders\Webservice\Model\SessionToken($this->request->username);
			$sessionTokenObject->check($this->request->sessionToken);
		}
		catch (\Modules\Orders\ImportedOrders\Webservice\Model\SessionTokenException $ex) {
			\Engine\Os::sleep(\Engine\RandomGenerator::randomFloat(1, 2));
			throw new \RuntimeException($ex->getMessage());
		}

	}

	/**
	 * Checks the request data before being executed.
	 */
	abstract protected function checkRequest();


	/**
	 * The actual operation - it is executed after the requst is checked.
	*/
	abstract protected function executeRequestedOperation();

}