<?php

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

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

	protected $forcedRequestAttributes = ['orderIdSystem', 'externalSystemCode'];

	/**
	 * {@inheritDoc}
	 * @see \Modules\RemoteData\EntityManagementWs\Model\Entity\GeneralAbstract::checkRequestData()
	 */
	protected function checkRequestData() :void
	{
		if (!$this->request->orderIdSystem) {
			throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException('No imported order ID set.', 'NoIdSet');
		}
		if (!$this->request->externalSystemCode) {
			throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException('No `externalSystemCode` set.', 'NoExternalSystemSet');
		}
	}

	/**
	 * Different approach for matching, and a different output also.
	 * {@inheritDoc}
	 * @see \Modules\RemoteData\EntityManagementWs\Model\Entity\GeneralAbstract::processRequest()
	 */
	protected function processRequest() :void
	{

		// match and check the system first
		$idExtSystem = \Entity\ExtResSystem::getSystemIdByCode($this->request->externalSystemCode);
		if (!$idExtSystem) {
			throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException('Invalid `externalSystemCode` set.', 'InvalidExternalSystemSet');
		}

		// imported order match - return the id (if found)
		$impOrderDetails = \Entity\ImportedOrder::getDetailsByOrderExtSys($this->request->orderIdSystem, $idExtSystem, false);
		$this->response->idEntity = (int)$impOrderDetails['id'];

	}

}
