-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
554 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,28 @@ | ||
<?php | ||
namespace Ackintosh\Ganesha; | ||
|
||
use Ackintosh\Ganesha; | ||
use InvalidArgumentException; | ||
use Ackintosh\Ganesha\Strategy; | ||
|
||
/** | ||
* A front end of the strategy specific builders | ||
* | ||
* @package Ackintosh\Ganesha | ||
*/ | ||
class Builder | ||
{ | ||
/** | ||
* @param array $params | ||
* @return Ganesha | ||
* @return Strategy\Rate\Builder | ||
*/ | ||
public static function build(array $params): Ganesha | ||
public static function withRateStrategy(): Strategy\Rate\Builder | ||
{ | ||
$params['strategyClass'] = '\Ackintosh\Ganesha\Strategy\Rate'; | ||
return self::perform($params); | ||
return new Strategy\Rate\Builder(); | ||
} | ||
|
||
/** | ||
* @param array $params | ||
* @return Ganesha | ||
* @return Strategy\Count\Builder | ||
*/ | ||
public static function buildWithCountStrategy(array $params): Ganesha | ||
public static function withCountStrategy(): Strategy\Count\Builder | ||
{ | ||
$params['strategyClass'] = '\Ackintosh\Ganesha\Strategy\Count'; | ||
return self::perform($params); | ||
} | ||
|
||
/** | ||
* @param array $params | ||
* @return Ganesha | ||
* @throws InvalidArgumentException | ||
*/ | ||
private static function perform(array $params): Ganesha | ||
{ | ||
call_user_func([$params['strategyClass'], 'validate'], $params); | ||
|
||
$configuration = new Configuration($params); | ||
$ganesha = new Ganesha( | ||
call_user_func( | ||
[$configuration['strategyClass'], 'create'], | ||
$configuration | ||
) | ||
); | ||
|
||
return $ganesha; | ||
return new Strategy\Count\Builder(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,99 @@ | ||
<?php | ||
namespace Ackintosh\Ganesha; | ||
|
||
use Ackintosh\Ganesha\Storage\AdapterInterface; | ||
use Ackintosh\Ganesha\Storage\StorageKeys; | ||
use Ackintosh\Ganesha\Storage\StorageKeysInterface; | ||
|
||
class Configuration implements \ArrayAccess | ||
class Configuration | ||
{ | ||
// Configuration keys | ||
const ADAPTER = 'adapter'; | ||
const TIME_WINDOW = 'timeWindow'; | ||
const FAILURE_RATE_THRESHOLD = 'failureRateThreshold'; | ||
const FAILURE_COUNT_THRESHOLD = 'failureCountThreshold'; | ||
const MINIMUM_REQUESTS = 'minimumRequests'; | ||
const INTERVAL_TO_HALF_OPEN = 'intervalToHalfOpen'; | ||
const STORAGE_KEYS = 'storageKeys'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $params; | ||
|
||
public function __construct($params) | ||
{ | ||
if (!isset($params['storageKeys'])) { | ||
$params['storageKeys'] = new StorageKeys(); | ||
if (!isset($params[self::STORAGE_KEYS])) { | ||
$params[self::STORAGE_KEYS] = new StorageKeys(); | ||
} | ||
$this->params = $params; | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
public function adapter(): AdapterInterface | ||
{ | ||
return $this->params[self::ADAPTER]; | ||
} | ||
|
||
public function timeWindow(): int | ||
{ | ||
return $this->params[self::TIME_WINDOW]; | ||
} | ||
|
||
public function failureRateThreshold(): int | ||
{ | ||
return $this->params[self::FAILURE_RATE_THRESHOLD]; | ||
} | ||
|
||
public function failureCountThreshold(): int | ||
{ | ||
return $this->params[self::FAILURE_COUNT_THRESHOLD]; | ||
} | ||
|
||
public function minimumRequests(): int | ||
{ | ||
$this->params[$offset] = $value; | ||
return $this->params[self::MINIMUM_REQUESTS]; | ||
} | ||
|
||
public function offsetExists($offset) | ||
public function intervalToHalfOpen(): int | ||
{ | ||
return isset($this->params[$offset]); | ||
return $this->params[self::INTERVAL_TO_HALF_OPEN]; | ||
} | ||
|
||
public function offsetUnset($offset) | ||
public function storageKeys(): StorageKeysInterface | ||
{ | ||
unset($this->params[$offset]); | ||
return $this->params[self::STORAGE_KEYS]; | ||
} | ||
|
||
public function offsetGet($offset) | ||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function validate(): void | ||
{ | ||
return isset($this->params[$offset]) ? $this->params[$offset] : null; | ||
if (isset($this->params[self::ADAPTER]) && !$this->params[self::ADAPTER] instanceof AdapterInterface) { | ||
throw new \InvalidArgumentException(get_class($this->params[self::ADAPTER]) . ' should be an instance of AdapterInterface'); | ||
} | ||
|
||
if (isset($this->params[self::STORAGE_KEYS]) && !$this->params[self::STORAGE_KEYS] instanceof StorageKeysInterface) { | ||
throw new \InvalidArgumentException(get_class($this->params[self::STORAGE_KEYS]) . ' should be an instance of StorageKeysInterface'); | ||
} | ||
|
||
foreach ([ | ||
self::TIME_WINDOW, | ||
self::FAILURE_RATE_THRESHOLD, | ||
self::FAILURE_COUNT_THRESHOLD, | ||
self::MINIMUM_REQUESTS, | ||
self::INTERVAL_TO_HALF_OPEN | ||
] as $name) { | ||
if (isset($this->params[$name])) { | ||
$v = $this->params[$name]; | ||
if (!is_int($v) || $v < 1) { | ||
throw new \InvalidArgumentException($name . ' should be an positive integer'); | ||
} | ||
} | ||
} | ||
|
||
if (isset($this->params[self::FAILURE_RATE_THRESHOLD]) && $this->params[self::FAILURE_RATE_THRESHOLD] > 100) { | ||
throw new \InvalidArgumentException(self::FAILURE_RATE_THRESHOLD . ' should be equal or less than 100'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.