Skip to content

Commit

Permalink
- suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrEssex committed Jun 25, 2020
1 parent e331757 commit e74c165
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
18 changes: 9 additions & 9 deletions src/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,47 @@ public static function directoryDoesNotExistAndCannotBeCreated(string $directory
}

/**
* @param string $key
* @param mixed $key
*
* @return InvalidArgumentException
*/
public static function keyIsNotAString(string $key): InvalidArgumentException
public static function keyIsNotAString($key): InvalidArgumentException
{
return new self(
sprintf(
"The specified key: %s is not a string!",
$key
), 5
), 500
);
}

/**
* @param string $key
* @param mixed|null $key
*
* @return InvalidArgumentException
*/
public static function keyIsEmpty(string $key): InvalidArgumentException
public static function keyIsEmpty($key): InvalidArgumentException
{
return new self(
sprintf(
"The specified key: %s is empty!",
$key
), 5
), 500
);
}

/**
* @param string $key
* @param mixed $key
*
* @return InvalidArgumentException
*/
public static function keyContainsInvalidCharacters(string $key): InvalidArgumentException
public static function keyContainsInvalidCharacters($key): InvalidArgumentException
{
return new self(
sprintf(
"The specified key: %s must only contain [A-Z] [a-z] [0-9] [_] [.] [-] characters!",
$key
), 5
), 500
);
}

Expand Down
58 changes: 30 additions & 28 deletions src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

namespace MrEssex\FileCache;

use MrEssex\FileCache\Exceptions\InvalidArgumentException;
use DateInterval;
use DateTime;
use MrEssex\FileCache\Exceptions\InvalidArgumentException;
use Psr\SimpleCache\CacheInterface;

use function file_exists;
use function str_replace;
use function realpath;

/**
* Class FileCache
*
* @package MrEssex\FileCache\Cache
*/
class FileCache
implements CacheInterface
class FileCache implements CacheInterface
{

public const CACHE_PATH = DIRECTORY_SEPARATOR . '.tmp' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;

/** @var string|null */
protected ?string $_directoryPath;

/** @var int */
protected int $_ttl = 3600;

Expand All @@ -35,10 +37,7 @@ public function __construct(string $directoryPath = null, int $ttl = null)

if ($directoryPath === null) {
// assume we are running from vendor;
$this->_directoryPath = dirname(
__DIR__,
4
) . self::CACHE_PATH;
$this->_directoryPath = dirname(__DIR__, 4) . self::CACHE_PATH;
}

if ($ttl !== null) {
Expand All @@ -52,9 +51,9 @@ public function __construct(string $directoryPath = null, int $ttl = null)
throw InvalidArgumentException::directoryDoesNotExistAndCannotBeCreated($this->_directoryPath);
}

if ((!is_readable($this->_directoryPath) || !is_writable($this->_directoryPath)) || (realpath(
if ((realpath($this->_directoryPath) === false) || (!is_readable($this->_directoryPath) || !is_writable(
$this->_directoryPath
) === false)) {
))) {
throw InvalidArgumentException::directoryDoesNotExistAndCannotBeCreated($this->_directoryPath);
}
}
Expand Down Expand Up @@ -86,12 +85,17 @@ public function get($key, $default = false)
*
* @return string
*/
private function _generateKey(string $key): string
protected function _generateKey(string $key): string
{
return md5($key);
}

private function _validateKey(string $key): bool
/**
* @param mixed $key
*
* @return bool
*/
protected function _validateKey($key): bool
{
if (!is_string($key)) {
throw InvalidArgumentException::keyIsNotAString($key);
Expand Down Expand Up @@ -142,10 +146,8 @@ public function set($key, $value, $ttl = null): bool
*
* @return string
*/
private function _getPath(string $key): string
protected function _getPath(string $key): string
{
$key = str_replace('/', '', $key);

return $this->_directoryPath . $key;
}

Expand All @@ -154,13 +156,11 @@ private function _getPath(string $key): string
*
* @return int
*/
private function _expirationToTimestamp(?int $ttl): int
protected function _expirationToTimestamp(?int $ttl): int
{
if ($ttl instanceof DateInterval) {
$ttl = $ttl->format('%s');
}

if ($ttl instanceof DateTime) {
} elseif ($ttl instanceof DateTime) {
$ttl = $ttl->getTimestamp();
}

Expand Down Expand Up @@ -200,7 +200,7 @@ public function delete($key): bool
*/
public function clear(): bool
{
$files = array_diff(scandir($this->_directoryPath), ['.', '..', '.*']);
$files = array_diff(scandir($this->_directoryPath), ['.', '..', '.*']);
$success = true;

foreach ($files as $file) {
Expand All @@ -216,7 +216,8 @@ public function clear(): bool
* @param iterable $keys A list of keys that can obtained in a single operation.
* @param mixed $default Default value to return for keys that do not exist.
*
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as
* value.
*
* @throws InvalidArgumentException
* MUST be thrown if $keys is neither an array nor a Traversable,
Expand Down Expand Up @@ -280,11 +281,11 @@ public function deleteMultiple($keys)
public function has($key): bool
{
$keyOriginal = $key;
$key = $this->_generateKey($key);
$key = $this->_generateKey($key);
$this->_validateKey($key);
$path = $this->_getPath($key);

if (! $this->_checkFileIsNotAtEndOfLife($path, $keyOriginal)) {
if (!$this->_checkFileIsNotAtEndOfLife($path, $keyOriginal)) {
return false;
}

Expand All @@ -301,17 +302,18 @@ public function has($key): bool
*
* @return bool
*/
private function _checkFileIsNotAtEndOfLife(string $path, string $key): bool {

if(!file_exists($path)) {
protected function _checkFileIsNotAtEndOfLife(string $path, string $key): bool
{
if (!file_exists($path)) {
return false;
}

$timestamp = filemtime($path);
$time = time();
$time = time();

if ($timestamp <= $time) {
$this->delete($key);

return false;
}

Expand Down

0 comments on commit e74c165

Please sign in to comment.