diff --git a/LICENSE b/LICENSE index 9ea0305..925b115 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015 Krishnaprasad MG +Copyright (c) 2015 Krishnaprasad MG Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/composer.json b/composer.json index 3c1c7f0..b234a21 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ }, "require-dev": { "mockery/mockery": "^0.9.4", - "phpunit/phpunit": "^4.7.6" + "phpunit/phpunit": "^4.7.6", + "mamuz/php-dependency-analysis": "0.*" }, "autoload": { "psr-4": { diff --git a/phpda.svg b/phpda.svg new file mode 100644 index 0000000..1b4f216 --- /dev/null +++ b/phpda.svg @@ -0,0 +1,47 @@ + + + + + + +G + +PhpDependencyAnalysis by Marco Muths (v0.5.3) +cluster_0 + +Sunspikes + +cluster_1 + +Desarrolla2 + + +Sunspikes\\Ratelimit + +Sunspikes\Ratelimit + + +Desarrolla2\\Cache + +Desarrolla2\Cache + + +Sunspikes\\Ratelimit->Desarrolla2\\Cache + + + + +Sunspikes\\RateLimiter + +Sunspikes\RateLimiter + + +Sunspikes\\RateLimiter->Sunspikes\\Ratelimit + + + + + diff --git a/phpda.yml b/phpda.yml new file mode 100644 index 0000000..9562eaa --- /dev/null +++ b/phpda.yml @@ -0,0 +1,22 @@ +mode: 'inheritance' +source: './src' +filePattern: '*.php' +ignore: 'tests' +formatter: 'PhpDA\Writer\Strategy\Svg' +target: './phpda.svg' +referenceValidator: 'Full\Qualified\Namespace\To\ReferenceValidator' +groupLength: 1 +visitor: + - PhpDA\Parser\Visitor\TagCollector + - PhpDA\Parser\Visitor\SuperglobalCollector + - PhpDA\Parser\Visitor\UnsupportedEvalCollector + - PhpDA\Parser\Visitor\UnsupportedFuncCollector + - PhpDA\Parser\Visitor\UnsupportedVarCollector + - PhpDA\Parser\Visitor\UnsupportedGlobalCollector + - PhpDA\Parser\Visitor\NamespacedStringCollector + - PhpDA\Parser\Visitor\IocContainerAccessorCollector +visitorOptions: + PhpDA\Parser\Visitor\Required\DeclaredNamespaceCollector: {minDepth: 2, sliceLength: 2} + PhpDA\Parser\Visitor\Required\MetaNamespaceCollector: {minDepth: 2, sliceLength: 1} + PhpDA\Parser\Visitor\Required\UsedNamespaceCollector: {minDepth: 2, sliceLength: 1} + PhpDA\Parser\Visitor\TagCollector: {minDepth: 2, sliceLength: 1} \ No newline at end of file diff --git a/src/Cache/Adapter/AbstractCacheAdapter.php b/src/Cache/Adapter/AbstractCacheAdapter.php index 6bd0e3b..069079c 100644 --- a/src/Cache/Adapter/AbstractCacheAdapter.php +++ b/src/Cache/Adapter/AbstractCacheAdapter.php @@ -1,4 +1,27 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Cache\Adapter; @@ -6,10 +29,15 @@ class AbstractCacheAdapter implements CacheAdapterContract { + /* @var int */ protected $ttl = 600; // 10 mins + /* @var mixed */ protected $cache; + /** + * @inheritdoc + */ public function get($key) { if ($this->cache->has($key)) { @@ -19,21 +47,33 @@ public function get($key) throw new ItemNotFoundException('Cannot find the item in cache'); } + /** + * @inheritdoc + */ public function set($key, $value, $ttl = null) { $this->cache->set($key, $value, $ttl); } + /** + * @inheritdoc + */ public function delete($key) { $this->cache->delete($key); } + /** + * @inheritdoc + */ public function has($key) { return $this->cache->has($key); } + /** + * @inheritdoc + */ public function clear() { $this->cache->clear(); diff --git a/src/Cache/Adapter/CacheAdapterContract.php b/src/Cache/Adapter/CacheAdapterContract.php index 8152e04..b684423 100644 --- a/src/Cache/Adapter/CacheAdapterContract.php +++ b/src/Cache/Adapter/CacheAdapterContract.php @@ -1,16 +1,70 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Cache\Adapter; interface CacheAdapterContract { + /** + * Get value from cache + * + * @param string $key + * @return mixed + */ public function get($key); + /** + * Set value in cache + * + * @param string $key + * @param mixed $value + * @param int $ttl + * @return mixed + */ public function set($key, $value, $ttl = null); + /** + * Delete value from cache + * + * @param string $key + * @return mixed + */ public function delete($key); + /** + * Check if keyed value exists in cache + * + * @param string $key + * @return bool + */ public function has($key); + /** + * Clear cache + * + * @return void + */ public function clear(); } \ No newline at end of file diff --git a/src/Cache/Adapter/DesarrollaCacheAdapter.php b/src/Cache/Adapter/DesarrollaCacheAdapter.php index 3738ffa..8c09c90 100644 --- a/src/Cache/Adapter/DesarrollaCacheAdapter.php +++ b/src/Cache/Adapter/DesarrollaCacheAdapter.php @@ -1,9 +1,39 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Cache\Adapter; +/** + * Adapter for the cache library Desarrolla2\Cache + */ class DesarrollaCacheAdapter extends AbstractCacheAdapter { + /** + * @param \Desarrolla2\Cache\CacheInterface $cache + * @param int $ttl + */ public function __construct($cache, $ttl) { $this->cache = $cache; diff --git a/src/Cache/Exception/CacheException.php b/src/Cache/Exception/CacheException.php index e531819..b0e1403 100644 --- a/src/Cache/Exception/CacheException.php +++ b/src/Cache/Exception/CacheException.php @@ -1,7 +1,33 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Cache\Exception; +/** + * The base cache exception + */ class CacheException extends \Exception { } \ No newline at end of file diff --git a/src/Cache/Exception/DriverNotFoundException.php b/src/Cache/Exception/DriverNotFoundException.php index dcdc1f7..41f4048 100644 --- a/src/Cache/Exception/DriverNotFoundException.php +++ b/src/Cache/Exception/DriverNotFoundException.php @@ -1,7 +1,33 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Cache\Exception; +/** + * Cache driver not found + */ class DriverNotFoundException extends CacheException { } \ No newline at end of file diff --git a/src/Cache/Exception/InvalidConfigException.php b/src/Cache/Exception/InvalidConfigException.php index cbae076..e24bb5f 100644 --- a/src/Cache/Exception/InvalidConfigException.php +++ b/src/Cache/Exception/InvalidConfigException.php @@ -1,7 +1,33 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Cache\Exception; +/** + * Invalid driver configuration + */ class InvalidConfigException extends CacheException { } \ No newline at end of file diff --git a/src/Cache/Exception/ItemNotFoundException.php b/src/Cache/Exception/ItemNotFoundException.php index f58fc4d..d400404 100644 --- a/src/Cache/Exception/ItemNotFoundException.php +++ b/src/Cache/Exception/ItemNotFoundException.php @@ -1,7 +1,33 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Cache\Exception; +/** + * Item not found in cache + */ class ItemNotFoundException extends CacheException { } \ No newline at end of file diff --git a/src/Cache/Factory/DesarrollaCacheFactory.php b/src/Cache/Factory/DesarrollaCacheFactory.php index a4357fb..5044692 100644 --- a/src/Cache/Factory/DesarrollaCacheFactory.php +++ b/src/Cache/Factory/DesarrollaCacheFactory.php @@ -1 +1 @@ -getDriver($config); return new Cache($driver); } private function getDriver($config) { $driver = $config['driver']; if (is_null($driver)) { throw new \InvalidConfigException('Cache driver is not defined in configuration.'); } $driverCreateMethod = 'create'. ucfirst($driver) .'Driver'; if (method_exists($this, $driverCreateMethod)) { $driver = new $this->{$driverCreateMethod}($config[$driver]); $driver->setOption('ttl', $config['default_ttl']?:DEFAULT_TTL); return $driver; } throw new DriverNotFoundException('Cannot find the driver '. $driver . ' for Desarrolla'); } private function createNotcacheDriver($config) { return new NotCache(); } private function createFileDriver($config) { return new File($config['cache_dir']); } private function createApcDriver($config) { return new Apc(); } private function createMemoryDriver($config) { $memory = new Memory(); $memory->setOption('limit', $config['limit']?:DEFAULT_LIMIT); return $memory; } private function createMongoDriver($config) { return new Mongo($config['server']); } private function createMysqlDriver($config) { return new MySQL($config['host'], $config['username'], $config['password'], $config['port']); } private function createRedisDriver($config) { return new Redis(); } private function createMemcacheDriver($config) { return new MemCache(); } } \ No newline at end of file + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ namespace Sunspikes\Ratelimit\Cache\Factory; use Desarrolla2\Cache\Adapter\Apc; use Desarrolla2\Cache\Adapter\File; use Desarrolla2\Cache\Adapter\MemCache; use Desarrolla2\Cache\Adapter\Memory; use Desarrolla2\Cache\Adapter\Mongo; use Desarrolla2\Cache\Adapter\MySQL; use Desarrolla2\Cache\Adapter\NotCache; use Desarrolla2\Cache\Adapter\Redis; use Desarrolla2\Cache\Cache; use Sunspikes\Ratelimit\Cache\Exception\DriverNotFoundException; use Sunspikes\Ratelimit\Cache\Exception\InvalidConfigException; class DesarrollaCacheFactory implements FactoryContract { /* @const DEFAULT_TTL */ const DEFAULT_TTL = 3600; /* @const DEFAULT_LIMIT */ const DEFAULT_LIMIT = 1000; /** * @inheritdoc */ public function make($config) { $driver = $this->getDriver($config); return new Cache($driver); } /** * Make the driver based on given config * * @param array $config * @return mixed * @throws DriverNotFoundException * @throws InvalidConfigException */ private function getDriver($config) { $driver = $config['driver']; if (is_null($driver)) { throw new InvalidConfigException('Cache driver is not defined in configuration.'); } $driverCreateMethod = 'create'. ucfirst($driver) .'Driver'; if (method_exists($this, $driverCreateMethod)) { $driver = new $this->{$driverCreateMethod}($config[$driver]); $driver->setOption('ttl', $config['default_ttl']?:static::DEFAULT_TTL); return $driver; } throw new DriverNotFoundException('Cannot find the driver '. $driver . ' for Desarrolla'); } /** * Create NotCache driver * * @param array $config * @return NotCache */ private function createNotcacheDriver($config) { return new NotCache(); } /** * Create File driver * * @param array $config * @return File */ private function createFileDriver($config) { return new File($config['cache_dir']); } /** * Create APC driver * * @param array $config * @return Apc */ private function createApcDriver($config) { return new Apc(); } /** * Create Memory driver * * @param array $config * @return Memory * @throws \Desarrolla2\Cache\Adapter\MemoryCacheException */ private function createMemoryDriver($config) { $memory = new Memory(); $memory->setOption('limit', $config['limit']?:static::DEFAULT_LIMIT); return $memory; } /** * Create Mongo driver * * @param array $config * @return Mongo */ private function createMongoDriver($config) { return new Mongo($config['server']); } /** * Create MySQL driver * * @param array $config * @return MySQL */ private function createMysqlDriver($config) { return new MySQL($config['host'], $config['username'], $config['password'], $config['port']); } /** * Create Redis driver * * @param array $config * @return Redis */ private function createRedisDriver($config) { return new Redis(); } /** * Create MemCache driver * * @param array $config * @return MemCache */ private function createMemcacheDriver($config) { return new MemCache(); } } \ No newline at end of file diff --git a/src/Cache/Factory/FactoryContract.php b/src/Cache/Factory/FactoryContract.php index c687416..c41e511 100644 --- a/src/Cache/Factory/FactoryContract.php +++ b/src/Cache/Factory/FactoryContract.php @@ -1,8 +1,37 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Cache\Factory; interface FactoryContract { + /** + * Create a cache driver adapter + * + * @param array $config + * @return mixed + */ public function make($config); } \ No newline at end of file diff --git a/src/RateLimiter.php b/src/RateLimiter.php index 214286d..c70a190 100644 --- a/src/RateLimiter.php +++ b/src/RateLimiter.php @@ -1,4 +1,27 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes; @@ -9,9 +32,15 @@ class RateLimiter { + /* @var \Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterContract */ private $adapter; + /* @var array */ private $throttlers; + /** + * @param array $config + * @throws \InvalidArgumentException + */ public function __construct(array $config) { if ('desarrolla' == $config['adapter']) @@ -23,6 +52,15 @@ public function __construct(array $config) throw new \InvalidArgumentException('No adapter found, please check your config.'); } + /** + * Build the throttler for given data + * + * @param mixed $data + * @param int $limit + * @param int $ttl + * @return mixed + * @throws \InvalidArgumentException + */ public function get($data, $limit, $ttl) { if (! empty($data)) diff --git a/src/Throttle/Entity/Data.php b/src/Throttle/Entity/Data.php index 8d22e29..ee08203 100644 --- a/src/Throttle/Entity/Data.php +++ b/src/Throttle/Entity/Data.php @@ -1,17 +1,49 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Throttle\Entity; class Data { + /* @var string */ protected $data; + /* @var int */ protected $limit; + /* @var int */ protected $ttl; + /* @var string */ protected $key; + /** + * @param string $data + * @param int $limit + * @param int $ttl + */ public function __construct($data, $limit, $ttl) { $this->data = $data; @@ -19,21 +51,41 @@ public function __construct($data, $limit, $ttl) $this->ttl = $ttl; } + /** + * Get data + * + * @return string + */ public function getData() { return $this->data; } + /** + * Get limit + * + * @return int + */ public function getLimit() { return $this->limit; } + /** + * Get TTL + * + * @return int + */ public function getTtl() { return $this->ttl; } + /** + * Get key + * + * @return string + */ public function getKey() { if (is_null($this->key)) { diff --git a/src/Throttle/Factory/FactoryContract.php b/src/Throttle/Factory/FactoryContract.php index 6e20946..442886e 100644 --- a/src/Throttle/Factory/FactoryContract.php +++ b/src/Throttle/Factory/FactoryContract.php @@ -1,4 +1,27 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Throttle\Factory; @@ -7,5 +30,12 @@ interface FactoryContract { + /** + * Create the throttler + * + * @param Data $data + * @param CacheAdapterContract $cache + * @return \Sunspikes\Ratelimit\Throttle\Throttler\ThrottlerContract + */ public function make(Data $data, CacheAdapterContract $cache); } \ No newline at end of file diff --git a/src/Throttle/Factory/ThrottlerFactory.php b/src/Throttle/Factory/ThrottlerFactory.php index fbde61f..4f918b7 100644 --- a/src/Throttle/Factory/ThrottlerFactory.php +++ b/src/Throttle/Factory/ThrottlerFactory.php @@ -1,4 +1,27 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Throttle\Factory; @@ -8,13 +31,16 @@ class ThrottlerFactory implements FactoryContract { + /** + * @inheritdoc + */ public function make(Data $data, CacheAdapterContract $cache) { $throttler = new CacheThrottler( $cache, - $data->getKey(), - $data->getLimit(), - $data->getTtl() + (string) $data->getKey(), + (int) $data->getLimit(), + (int) $data->getTtl() ); return $throttler; diff --git a/src/Throttle/Hydrator/ArrayHydrator.php b/src/Throttle/Hydrator/ArrayHydrator.php index 0d77106..ea175ed 100644 --- a/src/Throttle/Hydrator/ArrayHydrator.php +++ b/src/Throttle/Hydrator/ArrayHydrator.php @@ -1,4 +1,27 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Throttle\Hydrator; @@ -6,6 +29,9 @@ class ArrayHydrator implements DataHydratorContract { + /** + * @inheritdoc + */ public function hydrate($data, $limit, $ttl) { $string = implode('', $data); diff --git a/src/Throttle/Hydrator/DataHydratorContract.php b/src/Throttle/Hydrator/DataHydratorContract.php index 8519398..1ae4ba0 100644 --- a/src/Throttle/Hydrator/DataHydratorContract.php +++ b/src/Throttle/Hydrator/DataHydratorContract.php @@ -1,8 +1,40 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Throttle\Hydrator; interface DataHydratorContract { + /** + * Hydrate the given data + * + * @param mixed $data + * @param int $limit + * @param int $ttl + * + * @return \Sunspikes\Ratelimit\Throttle\Entity\Data + */ public function hydrate($data, $limit, $ttl); } \ No newline at end of file diff --git a/src/Throttle/Hydrator/StringHydrator.php b/src/Throttle/Hydrator/StringHydrator.php index f3d5c9c..4c2e56d 100644 --- a/src/Throttle/Hydrator/StringHydrator.php +++ b/src/Throttle/Hydrator/StringHydrator.php @@ -1,4 +1,27 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Throttle\Hydrator; @@ -6,6 +29,9 @@ class StringHydrator implements DataHydratorContract { + /** + * @inheritdoc + */ public function hydrate($data, $limit, $ttl) { new Data($data, $limit, $ttl); diff --git a/src/Throttle/Throttler/CacheThrottler.php b/src/Throttle/Throttler/CacheThrottler.php index 6143e2f..fa607fd 100644 --- a/src/Throttle/Throttler/CacheThrottler.php +++ b/src/Throttle/Throttler/CacheThrottler.php @@ -1,4 +1,27 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Throttle\Throttler; @@ -6,16 +29,29 @@ class CacheThrottler implements ThrottlerContract, \Countable { + /* @var \Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterContract */ protected $cache; + /* @var string */ protected $key; + /* @var int */ protected $limit; + /* @var int */ protected $ttl; + /* @var int */ protected $counter; + /** + * Short description for Function + * + * @param \Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterContract $cache + * @param string $key + * @param int $limit + * @param int $ttl + */ public function __contruct($cache, $key, $limit, $ttl) { $this->cache = $cache; @@ -24,6 +60,9 @@ public function __contruct($cache, $key, $limit, $ttl) $this->ttl = $ttl; } + /** + * @inheritdoc + */ public function access() { $status = $this->check(); @@ -33,6 +72,9 @@ public function access() return $status; } + /** + * @inheritdoc + */ public function hit() { $this->counter = $this->count() + 1; @@ -42,6 +84,9 @@ public function hit() return $this; } + /** + * @inheritdoc + */ public function clear() { $this->counter = 0; @@ -51,6 +96,9 @@ public function clear() return $this; } + /** + * @inheritdoc + */ public function count() { if (! is_null($this->counter)) { @@ -67,11 +115,19 @@ public function count() return $this->counter; } + /** + * @inheritdoc + */ public function check() { return ($this->count() < $this->limit); } + /** + * Get the cache adapter + * + * @return \Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterContract + */ public function getCache() { return $this->cache; diff --git a/src/Throttle/Throttler/ThrottlerContract.php b/src/Throttle/Throttler/ThrottlerContract.php index 556a534..75f12d1 100644 --- a/src/Throttle/Throttler/ThrottlerContract.php +++ b/src/Throttle/Throttler/ThrottlerContract.php @@ -1,16 +1,64 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace Sunspikes\Ratelimit\Throttle\Throttler; interface ThrottlerContract { + /** + * Access the resource and return status + * + * @return bool + */ public function access(); + /** + * Register a hit for the resource + * + * @return mixed + */ public function hit(); + /** + * Clear the hit counter + * + * @return mixed + */ public function clear(); + /** + * Get the hit count + * + * @return int + */ public function count(); + /** + * Check the throttle status + * + * @return bool + */ public function check(); } \ No newline at end of file