diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 diff --git a/.vscode/settings.json b/.vscode/settings.json old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 diff --git a/composer.lock b/composer.lock old mode 100644 new mode 100755 index c82422e..d194fa7 --- a/composer.lock +++ b/composer.lock @@ -65,20 +65,20 @@ }, { "name": "myclabs/deep-copy", - "version": "1.9.0", + "version": "1.9.1", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "78af75148f9fdd34ea727c8b529a9b4a8f7b740c" + "reference": "e6828efaba2c9b79f4499dae1d66ef8bfa7b2b72" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/78af75148f9fdd34ea727c8b529a9b4a8f7b740c", - "reference": "78af75148f9fdd34ea727c8b529a9b4a8f7b740c", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/e6828efaba2c9b79f4499dae1d66ef8bfa7b2b72", + "reference": "e6828efaba2c9b79f4499dae1d66ef8bfa7b2b72", "shasum": "" }, "require": { - "php": "^7.2" + "php": "^7.1" }, "replace": { "myclabs/deep-copy": "self.version" @@ -86,8 +86,6 @@ "require-dev": { "doctrine/collections": "^1.0", "doctrine/common": "^2.6", - "phpstan/phpstan": "^0.9.2", - "phpstan/phpstan-phpunit": "^0.9.4", "phpunit/phpunit": "^7.1" }, "type": "library", @@ -111,7 +109,7 @@ "object", "object graph" ], - "time": "2018-10-30T00:14:44+00:00" + "time": "2019-04-07T13:18:21+00:00" }, { "name": "phar-io/manifest", @@ -684,16 +682,16 @@ }, { "name": "phpunit/phpunit", - "version": "8.1.2", + "version": "8.1.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "e7450b51b6f5d29edcd645ff72b355ab0633ca35" + "reference": "2a676677982b484ee36b54961f570c02eabbe7e1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e7450b51b6f5d29edcd645ff72b355ab0633ca35", - "reference": "e7450b51b6f5d29edcd645ff72b355ab0633ca35", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2a676677982b484ee36b54961f570c02eabbe7e1", + "reference": "2a676677982b484ee36b54961f570c02eabbe7e1", "shasum": "" }, "require": { @@ -762,7 +760,7 @@ "testing", "xunit" ], - "time": "2019-04-08T16:03:02+00:00" + "time": "2019-04-19T15:52:00+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", @@ -1335,16 +1333,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.4.1", + "version": "3.4.2", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "5b4333b4010625d29580eb4a41f1e53251be6baa" + "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/5b4333b4010625d29580eb4a41f1e53251be6baa", - "reference": "5b4333b4010625d29580eb4a41f1e53251be6baa", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", + "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", "shasum": "" }, "require": { @@ -1382,7 +1380,7 @@ "phpcs", "standards" ], - "time": "2019-03-19T03:22:27+00:00" + "time": "2019-04-10T23:49:02+00:00" }, { "name": "symfony/polyfill-ctype", diff --git a/phpcs.xml b/phpcs.xml old mode 100644 new mode 100755 diff --git a/phpunit.xml b/phpunit.xml old mode 100644 new mode 100755 diff --git a/src/CollectionInterface.php b/src/CollectionInterface.php old mode 100644 new mode 100755 index 16480df..7d7ca2e --- a/src/CollectionInterface.php +++ b/src/CollectionInterface.php @@ -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 diff --git a/src/FileCollection.php b/src/FileCollection.php new file mode 100644 index 0000000..b869816 --- /dev/null +++ b/src/FileCollection.php @@ -0,0 +1,91 @@ +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); + } +} diff --git a/src/MemoryCollection.php b/src/MemoryCollection.php old mode 100644 new mode 100755 index b71984a..c29e89b --- a/src/MemoryCollection.php +++ b/src/MemoryCollection.php @@ -9,6 +9,13 @@ */ class MemoryCollection implements CollectionInterface { + /** + * Default file expires + * + * @var int Default in seconds + */ + const EXPIRES = 60; + /** * Collection data * @@ -16,6 +23,13 @@ class MemoryCollection implements CollectionInterface */ protected $data; + /** + * Expires in seconds + * + * @var string + */ + protected $expires = self::EXPIRES; + /** * Constructor */ @@ -33,15 +47,22 @@ 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(); } /** @@ -49,7 +70,17 @@ public function set(string $index, $value) */ 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; } /** @@ -57,7 +88,7 @@ public function has(string $index) */ public function count(): int { - return count($this->data) + 1; + return count($this->data); } /** @@ -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(); + } + } } diff --git a/tests/src/FileCollectionTest.php b/tests/src/FileCollectionTest.php new file mode 100755 index 0000000..685683a --- /dev/null +++ b/tests/src/FileCollectionTest.php @@ -0,0 +1,93 @@ +assertEquals('dataCanBeRetrieved', $collection->get()); + } + + /** + * @test + * @depends objectCanBeConstructed + */ + public function inexistentIndexShouldReturnDefaultValue() + { + $collection = new FileCollection('', 0); + + $this->assertEquals('defaultValue', $collection->get('defaultValue')); + } + + /** + * @test + * @depends objectCanBeConstructed + */ + public function newCollectionShouldNotContainItems() + { + $collection = new FileCollection('', 1800); + $this->assertEquals(0, $collection->count()); + } + + /** + * @test + * @depends dataCanBeAdded + */ + public function collectionWithItemsShouldReturnValidCount() + { + $collection = new FileCollection('collectionWithItemsShouldReturnValidCount', 240); + $this->assertEquals(1, $collection->count()); + } + + /** + * @test + * @depends collectionWithItemsShouldReturnValidCount + */ + public function collectionCanBeCleaned() + { + $collection = new FileCollection('collectionCanBeCleaned', 240); + $this->assertEquals(1, $collection->count()); + + $collection->clean(); + $this->assertEquals(0, $collection->count()); + } + + /** + * @test + * @depends dataCanBeAdded + */ + public function addedItemShouldExistInCollection() + { + $collection = new FileCollection('addedItemShouldExistInCollection', 300); + $this->assertTrue($collection->has()); + } +} diff --git a/tests/src/MemoryCollectionTest.php b/tests/src/MemoryCollectionTest.php old mode 100644 new mode 100755