<?php
namespace OfficeBrain\Bundle\CoreBundle\Cache;

class Utility{
	private $request;
	private $stashCache;
	private $cacheEnable;
	private $cachePrefix;
	
	 
	public function __construct($stashCache,$cacheEnable,$cachePrefix){
		$this->stashCache = $stashCache;
		$this->cacheEnable = $cacheEnable;
		$this->cachePrefix = $cachePrefix;
	}
	
	public function removeAllCacheOfProject(){
		$item = $this->stashCache->getItem($this->cachePrefix);
		$item->clear();
	}
	
	public function removeAllCache($key){		
		$item = $this->stashCache->getItem($this->cachePrefix."/".$key);
		$item->clear();
	}
	
	public function setCacheData($key,$data){
		if(!$this->cacheEnable){
			return false;
		}
		
		$item = $this->stashCache->getItem($this->cachePrefix."/".$key);
		$item->lock();
		$item->set($data);
	}
	
	public function getCacheData($key){
		if(!$this->cacheEnable){
			return false;
		}
		
		$item = $this->stashCache->getItem($this->cachePrefix."/".$key);		
		$data = $item->get();
		
		if(!$item->isMiss()){
			return $data;
		}
		
		return false;
	}
	
	public function createKey($function_args){
		$function_args = (array) $function_args;
		return implode("#__#",$function_args);
	}
}