<?php
namespace App\Lidia;

abstract class ResponseAbstract
{
	/**
	 * Stores the status response
	 * @var String
	 */
	protected $status;

	/**
	 * Stores the message response
	 * @var String
	 */
	protected $message;

	/**
	 * Set response status (ok, invalid, error)
	 * @param String $status
	 * @return \App\Lidia\ResponseAbstract
	 */
	public function setStatus($status)
	{
		$this->status = $status;
		return $this;
	}

	/**
	 * Set message
	 * @param String $message
	 * @return \App\Lidia\ResponseAbstract
	 */
	public function setMessage($message)
	{
		$this->message = $message;
		return $this;
	}

	/**
	 * Validate response
	 * @throws \App\MessagesException
	 */
	public function check()
	{
		$msgs = new \App\MessagesCollector();

		if (!$this->status) {
			$msgs->addError("The response from LIDIA is not complete. Please verify the logs.");
		}

		if (in_array($this->status, ['invalid', 'error'])) {
			$msgs->addError(sprintf("The status response is `%s`. Message returned: `%s`", $this->status, $this->message));
		}

		$this->doCheck($msgs);

		if ($msgs->hasErrorOrWarning()) {
			throw new \App\MessagesException($msgs);
		}
	}

	/**
	 * Check the specific information from security file
	 * @param \App\MessagesCollector $msgs
	 */
	abstract protected function doCheck(\App\MessagesCollector $msgs);
}
