<?php
namespace App\Sabre\CompatibilityWithImportedOrders;

class TaxManipulationsCached extends TaxManipulations
{
	private $CACHE = [];


	/**
	 * Get the parent entity for this service
	 * @param \Entity\SabreService $entity
	 * @return \Entity\SabreService|NULL
	 */
	public function getParentEntity($entity)
	{
		$cacheKey = sprintf("%s:%s", __METHOD__, $entity->getId());


		if (!array_key_exists($cacheKey, $this->CACHE)) {
			$this->CACHE[$cacheKey] = parent::getParentEntity($entity);
		}

		return $this->CACHE[$cacheKey];
	}

	/**
	 * Get the service itinerary, or the service's parent itinerary, if this is a refund cached
	 * @param \Entity\SabreService $entity
	 * @param \Entity\SabreService $parentEntity
	 * @return Array
	 */
	public function TINA_getServiceItinerary($entity, $parentEntity=null)
	{
		$cacheKey = sprintf("%s:%s,%s", __METHOD__, $entity->getId(), ($parentEntity?$parentEntity->getId():null));

		if (!array_key_exists($cacheKey, $this->CACHE)) {
			$this->CACHE[$cacheKey] = parent::TINA_getServiceItinerary($entity, $parentEntity);
		}
		return $this->CACHE[$cacheKey];
	}

	/**
	 * Get TINA offer location cached
	 * This would be better moved in the CompatibilityWithImportedOrders layer instead, as these calculations are "simulated" the same way TINA handles them, and it seems to impact the layer closer to TINA than the GDS layer
	 * @param array $segments
	 */
	public function TINA_getOfferLocation(array $segments)
	{
		$cacheKey = sprintf("%s:%s", __METHOD__, md5(serialize($segments)));

		if (!array_key_exists($cacheKey, $this->CACHE)) {
			$this->CACHE[$cacheKey] = parent::TINA_getOfferLocation($segments);
		}
		return $this->CACHE[$cacheKey];
	}
}
