<?php

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

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

	protected $forcedRequestAttributes = ['externalSystem', 'productName', 'serviceCode', 'destinationCountryCode', 'destinationCityId'];

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

		// validate input
		if (!$this->request->externalSystem and !$this->request->productName) {
			throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException('At least one criteria for matching must be set.', 'SupplierProductMatchCriteriaMissing');
		}

		if ($this->request->productName and mb_strlen($this->request->productName) < 3) {
			throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException('The product name value must have at least 3 characters.', 'SupplierProductMatchProductNameMinLength');
		}

	}


	/**
	 * Populated the matching entity object.
	 * @return \App\EntityMatch\BaseAbstract
	 */
	protected function getEntityMatchObj() :\App\EntityMatch\BaseAbstract
	{

		// Supplier Product matching object
		$supplierProductMatchObj = new \App\ImportedOrder\EntityMatch\SupplierProduct('intersect');

		if ($this->request->externalSystem) {
			$supplierProductMatchObj->setIdInExtSys($this->request->externalSystem->idEntityExtSys, $this->request->externalSystem->idSystem);
		}
		if ($this->request->productName) {
			$supplierProductMatchObj->setProductName($this->request->productName);
		}
		if ($this->request->serviceCode) {
			$supplierProductMatchObj->filterByServiceCode($this->request->serviceCode);
		}
		if ($this->request->destinationCountryCode) {
			$supplierProductMatchObj->filterByCountryCode($this->request->destinationCountryCode);
		}
		if ($this->request->destinationCityId) {
			$supplierProductMatchObj->filterByCityId($this->request->destinationCityId);
		}

		return $supplierProductMatchObj;

	}

}
