Storage/StorageManager.php
<?php
namespace NW\RequestLimitBundle\Storage;
use NW\RequestLimitBundle\Storage\Provider\ProviderInterface;
/**
* Class StorageManager
* @package NW\RequestLimitBundle\Storage
* @author Novikov Viktor
*/
class StorageManager
{
/**
* @var ProviderInterface
*/
public $provider;
/**
* @var integer
*/
private $restrictionTime;
/**
* @param $restrictionTime
*/
public function __construct($restrictionTime)
{
$this->restrictionTime = $restrictionTime;
}
/**
* @param $provider
*/
public function setProvider(ProviderInterface $provider)
{
$this->provider = $provider;
}
/**
* @param $key
* @return bool
*/
public function hasItem($key)
{
return ($this->provider->get($key) && $this->stillRestricted($key));
}
/**
* @param $key
* @return mixed
*/
public function getItem($key)
{
return $this->provider->get($key);
}
/**
* @param $key
* @param null $value
*/
public function setItem($key, $value = null)
{
$value = $value ?: new \DateTime("+{$this->restrictionTime} seconds");
$this->provider->set($key, $value->getTimestamp());
}
/**
* @param $key
*/
public function removeItem($key)
{
$this->provider->remove($key);
}
/**
* @return int
*/
public function getItemsCount()
{
return $this->provider->getItemsCount();
}
/**
* @return array
*/
public function fetchAllItems()
{
return $this->provider->fetchAllItems();
}
/**
* @param $key
* @return bool
*/
private function stillRestricted($key)
{
$timestamp = $this->provider->get($key);
$currentDate = new \DateTime();
$currentTimestamp = $currentDate->getTimestamp();
if ($timestamp < $currentTimestamp) {
$this->removeItem($key);
return false;
}
return true;
}
}