<?php

namespace Modules\RemoteData\EntityManagementWs\Model\Entity\General;

class GetCurrencyRates extends \Modules\RemoteData\EntityManagementWs\Model\Entity\GeneralAbstract
{

	/**
	 * The currency exchange rates date.
	 */
	protected string $date;

	/**
	 * Associates the request (parent call) and sets the date.
	 * @param \stdClass $request
	 */
	public function __construct(array $requestGet, \stdClass $request, \Modules\RemoteData\EntityManagementWs\Model\Response $response)
	{
		parent::__construct($requestGet, $request, $response);
		$this->date = $requestGet['date']?:TODAY_ISO;
	}

	/**
	 * {@inheritDoc}
	 * @see \Modules\RemoteData\EntityManagementWs\Model\Entity\GeneralAbstract::checkRequestData()
	 */
	protected function checkRequestData() :void
	{
		if (!validDate($this->date)) {
			throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException('Invalid date set.', 'InvalidDateFormat');
		}
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \Modules\RemoteData\EntityManagementWs\Model\Entity\GeneralAbstract::processRequest()
	 */
	protected function processRequest() :void
	{

		$currencyExch = \App\CurrencyExchange\Date::getInstance($this->date);

		$this->response->rates = $currencyExch->getRates();
		$this->response->ratesDefineDate = $currencyExch->getLastDefinedDate();
		$this->response->defaultCurrency = $currencyExch->getMarketCurrency();
	}
}

