Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
Empty file modified .vscode/settings.json
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified composer.json
100644 → 100755
Empty file.
34 changes: 16 additions & 18 deletions composer.lock
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified phpcs.xml
100644 → 100755
Empty file.
Empty file modified phpunit.xml
100644 → 100755
Empty file.
7 changes: 4 additions & 3 deletions src/CollectionInterface.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public function get(string $index, $defaultValue = null);
/**
* Adds a value to the collection
*
* @param string $index
* @param mixed $value
* @param string $index
* @param mixed $value
* @param int|null $expire
* @return void
*/
public function set(string $index, $value);
public function set(string $index, $value, int $expire = null);

/**
* Checks whether the collection has the given index
Expand Down
91 changes: 91 additions & 0 deletions src/FileCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Live\Collection;

/**
* File collection
*
* @package Live\Collection
*/
class FileCollection
{
/**
* Index
*
* @var string
*/
const INDEX = 'file_collection';

/**
* @var MemoryCollection
*/
protected $memoryCollection;

/**
* FileCollection constructor.
* @param string $file
* @param int $expires In seconds
* @return void
*/
public function __construct(string $file, int $expires)
{
$this->memoryCollection = new MemoryCollection();

$this->set($file, $expires);
}

/**
* Get
*
* @param mixed|null $defaultValue
* @return string|null
*/
public function get($defaultValue = null)
{
return $this->memoryCollection->get(self::INDEX, $defaultValue);
}

/**
* Set file
*
* @param string $file
* @param int $expires
*/
public function set(string $file, int $expires)
{
if (empty($file)) {
return ;
}
$this->memoryCollection->set(self::INDEX, $file, $expires);
}

/**
* Count
*
* @return int
*/
public function count()
{
return $this->memoryCollection->count();
}

/**
* Clean
*
* @return void
*/
public function clean()
{
$this->memoryCollection->clean();
}

/**
* File available
*
* @return bool
*/
public function has()
{
return $this->memoryCollection->has(self::INDEX);
}
}
74 changes: 69 additions & 5 deletions src/MemoryCollection.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@
*/
class MemoryCollection implements CollectionInterface
{
/**
* Default file expires
*
* @var int Default in seconds
*/
const EXPIRES = 60;

/**
* Collection data
*
* @var array
*/
protected $data;

/**
* Expires in seconds
*
* @var string
*/
protected $expires = self::EXPIRES;

/**
* Constructor
*/
Expand All @@ -33,31 +47,48 @@ public function get(string $index, $defaultValue = null)
return $defaultValue;
}

return $this->data[$index];
return $this->data[$index]['value'];
}

/**
* {@inheritDoc}
*/
public function set(string $index, $value)
public function set(string $index, $value, int $expires = null)
{
$this->data[$index] = $value;
if (empty($index) || empty($value)) {
return ;
}

$this->setExpires($expires);

$this->data[$index]['value'] = $value;
$this->data[$index]['expires'] = $this->getExpires();
}

/**
* {@inheritDoc}
*/
public function has(string $index)
{
return array_key_exists($index, $this->data);
if (!array_key_exists($index, $this->data)) {
return false;
}

$data = $this->data[$index];

$dateExpires = new \DateTime();
$dateExpires->setTimestamp($data['expires']);
$dateNow = new \DateTime();

return $dateExpires >= $dateNow;
}

/**
* {@inheritDoc}
*/
public function count(): int
{
return count($this->data) + 1;
return count($this->data);
}

/**
Expand All @@ -67,4 +98,37 @@ public function clean()
{
$this->data = [];
}

/**
* Set expiration
*
* @param int $seconds
* @return void
*/
protected function setExpires(int $seconds = null)
{
if (!is_null($seconds) && $seconds > 0) {
$this->expires = $seconds;
} else {
$this->expires = self::EXPIRES;
}
}

/**
* Get expires
*
* @return int timestamp
* @throws \Exception
*/
protected function getExpires(): int
{
try {
$date = new \DateTime();
$date->add(new \DateInterval('PT' . $this->expires . 'S'));
return $date->getTimestamp();
} catch (\Exception $ex) {
$date = new \DateTime();
return $date->getTimestamp();
}
}
}
Loading