<?php
namespace ModulesAbstract\ImportedOrdersFileManager\Model\Utils;

/**
 * Cache the parent class calls
 * @author Lucian Sirbu
 */
class FilesystemPaginatorAndSorterCached extends FilesystemPaginatorAndSorter
{
	/**
	 * The caching object
	 * @var \Engine\Cache\DiskWithLRU
	 */
	protected $CACHE = null;

	/**
	 *
	 */
	public function __construct()
	{
		$this->CACHE = \Engine\Cache\General::getInstance('disk');
	}

	/**
	 * {@inheritDoc}
	 * @see \ModulesAbstract\ImportedOrdersFileManager\Model\Utils\FilesystemPaginatorAndSorter::sortList()
	 */
	protected function sortList($elements, $orderColumn, $orderAscending=true)
	{
		$firstElement = $elements[0];
		if ($firstElement) {
			/* @var $firstElement \ModulesAbstract\ImportedOrdersFileManager\Model\File */
			$cacheKey = sprintf("%s:%s:%s:%s:%s:%s", __METHOD__, $firstElement->getFullPath(), count($elements), implode(",", $firstElement->getExtractedData()), $this->orderColumn, $this->orderDirection);
			$cachedData = $this->CACHE->get($cacheKey);
			if ($cachedData) {
				return $cachedData;
			}

			$elements = parent::sortList($elements, $orderColumn, $orderAscending);

			$this->CACHE->set($cacheKey, $elements, 60*60*24*7);
		}

		return $elements;
	}
}
