Skip to content

Commit

Permalink
Easily change the config
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilari Moragrega committed Dec 6, 2017
1 parent a45aca6 commit e49b546
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
22 changes: 21 additions & 1 deletion src/Balancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -51,6 +71,6 @@ private function seed($seed = null)
*/
function jsonSerialize()
{
return $this->features;
return $this->getConfig();
}
}
15 changes: 1 addition & 14 deletions src/BalancerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
18 changes: 18 additions & 0 deletions src/BalancerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
16 changes: 16 additions & 0 deletions src/Decorator/BalancerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down

0 comments on commit e49b546

Please sign in to comment.