diff --git a/src/Balancer.php b/src/Balancer.php index e9c66d8..ec47641 100644 --- a/src/Balancer.php +++ b/src/Balancer.php @@ -20,6 +20,26 @@ public function add($name, array $percentages) $this->features[$feature->name()] = $feature; } + /** + * {@inheritdoc} + */ + public function setConfig(array $config) + { + $this->features = []; + + foreach ($config as $feature => $percentages) { + $this->add($feature, $percentages); + } + } + + /** + * {@inheritdoc} + */ + public function getConfig() + { + return $this->features; + } + /** * {@inheritdoc} */ @@ -51,6 +71,6 @@ private function seed($seed = null) */ function jsonSerialize() { - return $this->features; + return $this->getConfig(); } } diff --git a/src/BalancerBuilder.php b/src/BalancerBuilder.php index 92360e1..f393648 100644 --- a/src/BalancerBuilder.php +++ b/src/BalancerBuilder.php @@ -45,7 +45,7 @@ class BalancerBuilder public function create(array $config = []) { $balancer = new Balancer(); - $this->addConfig($balancer, $config); + $balancer->setConfig($config); if ($this->monitor instanceof Monitor) { $balancer = new MonitoringDecorator($balancer, $this->monitor, $this->metric); @@ -107,17 +107,4 @@ public function withoutExceptions() return $this; } - - /** - * Add the features to the balancer - * - * @param BalancerInterface $balancer - * @param array $config - */ - private function addConfig(BalancerInterface $balancer, array $config) - { - foreach ($config as $feature => $percentages) { - $balancer->add($feature, $percentages); - } - } } diff --git a/src/BalancerInterface.php b/src/BalancerInterface.php index b25b67e..f5aca51 100644 --- a/src/BalancerInterface.php +++ b/src/BalancerInterface.php @@ -12,9 +12,27 @@ interface BalancerInterface extends \JsonSerializable * * @param string $name * @param array $percentages + * + * @throws InvalidArgumentException When any parameter given is not acceptable */ public function add($name, array $percentages); + /** + * Sets the features configuration + * + * @param array $config + * + * @throws InvalidArgumentException When any parameter given is not acceptable + */ + public function setConfig(array $config); + + /** + * Gets the features configuration + * + * @return array + */ + public function getConfig(); + /** * Gets the path that a seed has to follow for a given feature * diff --git a/src/Decorator/BalancerDecorator.php b/src/Decorator/BalancerDecorator.php index 4b37bfd..aa3ad81 100644 --- a/src/Decorator/BalancerDecorator.php +++ b/src/Decorator/BalancerDecorator.php @@ -27,6 +27,22 @@ public function add($name, array $percentages) $this->balancer->add($name, $percentages); } + /** + * {@inheritdoc} + */ + public function setConfig(array $config) + { + $this->balancer->config($config); + } + + /** + * {@inheritdoc} + */ + public function getConfig() + { + return $this->balancer->getConfig(); + } + /** * {@inheritdoc} */