Skip to content

Commit

Permalink
Splitted balancer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilari Moragrega committed Dec 6, 2017
1 parent e49b546 commit 6053b82
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 60 deletions.
2 changes: 2 additions & 0 deletions spec/BalancerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function let()
function it_is_initializable()
{
$this->shouldHaveType('Cmp\FeatureBalancer\Balancer');
$this->shouldImplement('Cmp\FeatureBalancer\BalancerInterface');
$this->shouldImplement('Cmp\FeatureBalancer\ConfigurableBalancerInterface');
}

function it_can_distribute_hits_across_features_based_on_numeric_seeds()
Expand Down
8 changes: 4 additions & 4 deletions spec/Decorator/ExceptionSilencerDecoratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

namespace spec\Cmp\FeatureBalancer\Decorator;

use Cmp\FeatureBalancer\BalancerInterface;
use Cmp\FeatureBalancer\ConfigurableBalancerInterface;
use Cmp\FeatureBalancer\Exception\OutOfBoundsException;
use PhpSpec\ObjectBehavior;
use Psr\Log\LoggerInterface;
use Webmozart\Assert\Assert;

class ExceptionSilencerDecoratorSpec extends ObjectBehavior
{
function let(BalancerInterface $balancer, LoggerInterface $logger)
function let(ConfigurableBalancerInterface $balancer, LoggerInterface $logger)
{
$this->beConstructedWith($balancer, $logger);
}

function it_silences_exceptions(BalancerInterface $balancer, LoggerInterface $logger)
function it_silences_exceptions(ConfigurableBalancerInterface $balancer, LoggerInterface $logger)
{
$exception = new OutOfBoundsException("feature foo is not set");
$balancer->get("foo", 123456)->willThrow($exception);
Expand All @@ -28,7 +28,7 @@ function it_silences_exceptions(BalancerInterface $balancer, LoggerInterface $lo
])->shouldHaveBeenCalled();
}

function it_can_be_json_serialized(BalancerInterface $balancer)
function it_can_be_json_serialized(ConfigurableBalancerInterface $balancer)
{
$balancer->jsonSerialize()->willReturn(["foo" => "bar"]);

Expand Down
8 changes: 4 additions & 4 deletions spec/Decorator/LoggerDecoratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

namespace spec\Cmp\FeatureBalancer\Decorator;

use Cmp\FeatureBalancer\BalancerInterface;
use Cmp\FeatureBalancer\ConfigurableBalancerInterface;
use PhpSpec\ObjectBehavior;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class LoggerDecoratorSpec extends ObjectBehavior
{
function let(BalancerInterface $balancer, LoggerInterface $logger)
function let(ConfigurableBalancerInterface $balancer, LoggerInterface $logger)
{
$this->beConstructedWith($balancer, $logger, LogLevel::DEBUG);
}

function it_delegates_to_the_inner_balancer_the_calls_to_add_features(BalancerInterface $balancer)
function it_delegates_to_the_inner_balancer_the_calls_to_add_features(ConfigurableBalancerInterface $balancer)
{
$this->add("foo", ["percentages"]);

$balancer->add("foo", ["percentages"])->shouldHaveBeenCalled();
}

function it_logs_when_a_new_feature_path_is_decided(BalancerInterface $balancer, LoggerInterface $logger)
function it_logs_when_a_new_feature_path_is_decided(ConfigurableBalancerInterface $balancer, LoggerInterface $logger)
{
$balancer->get("foo", null)->willReturn("bar");

Expand Down
6 changes: 3 additions & 3 deletions spec/Decorator/MonitoringDecoratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace spec\Cmp\FeatureBalancer\Decorator;

use Cmp\FeatureBalancer\BalancerInterface;
use Cmp\FeatureBalancer\ConfigurableBalancerInterface;
use Cmp\Monitoring\Monitor;
use PhpSpec\ObjectBehavior;

class MonitoringDecoratorSpec extends ObjectBehavior
{
function let(BalancerInterface $balancer, Monitor $monitor)
function let(ConfigurableBalancerInterface $balancer, Monitor $monitor)
{
$this->beConstructedWith($balancer, $monitor, "my_metric");
}

function it_logs_when_a_new_feature_path_is_decided(BalancerInterface $balancer, Monitor $monitor)
function it_logs_when_a_new_feature_path_is_decided(ConfigurableBalancerInterface $balancer, Monitor $monitor)
{
$balancer->get("foo", null)->willReturn("bar");

Expand Down
2 changes: 1 addition & 1 deletion src/Balancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Cmp\FeatureBalancer\Exception\OutOfBoundsException;

class Balancer implements BalancerInterface
class Balancer implements ConfigurableBalancerInterface
{
/**
* @var Feature[]
Expand Down
4 changes: 2 additions & 2 deletions src/BalancerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BalancerBuilder
/**
* @param array $config
*
* @return Balancer|LoggerDecorator|MonitoringDecorator
* @return BalancerInterface|ConfigurableBalancerInterface
*/
public function create(array $config = [])
{
Expand All @@ -63,7 +63,7 @@ public function create(array $config = [])
}

/**
* @return BalancerInterface
* @return BalancerInterface|ConfigurableBalancerInterface
*/
public function createNullBalancer()
{
Expand Down
28 changes: 1 addition & 27 deletions src/BalancerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,8 @@
use Cmp\FeatureBalancer\Exception\InvalidArgumentException;
use Cmp\FeatureBalancer\Exception\OutOfBoundsException;

interface BalancerInterface extends \JsonSerializable
interface BalancerInterface
{
/**
* Adds a new feature in the balancer
*
* @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
34 changes: 34 additions & 0 deletions src/ConfigurableBalancerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Cmp\FeatureBalancer;

use Cmp\FeatureBalancer\Exception\InvalidArgumentException;

interface ConfigurableBalancerInterface extends BalancerInterface, \JsonSerializable
{
/**
* Adds a new feature in the balancer
*
* @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();
}
10 changes: 5 additions & 5 deletions src/Decorator/BalancerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Cmp\FeatureBalancer\Decorator;

use Cmp\FeatureBalancer\BalancerInterface;
use Cmp\FeatureBalancer\ConfigurableBalancerInterface;

abstract class BalancerDecorator implements BalancerInterface
abstract class BalancerDecorator implements ConfigurableBalancerInterface
{
/**
* @var BalancerInterface
* @var ConfigurableBalancerInterface
*/
protected $balancer;

/**
* @param BalancerInterface $balancer
* @param ConfigurableBalancerInterface $balancer
*/
public function __construct(BalancerInterface $balancer)
public function __construct(ConfigurableBalancerInterface $balancer)
{
$this->balancer = $balancer;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Decorator/ExceptionSilencerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace Cmp\FeatureBalancer\Decorator;

use Cmp\FeatureBalancer\BalancerInterface;
use Cmp\FeatureBalancer\ConfigurableBalancerInterface;
use Cmp\FeatureBalancer\Exception\BalancerException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class ExceptionSilencerDecorator extends BalancerDecorator implements BalancerInterface
class ExceptionSilencerDecorator extends BalancerDecorator
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* @param BalancerInterface $balancer
* @param LoggerInterface|null $logger
* @param ConfigurableBalancerInterface $balancer
* @param LoggerInterface|null $logger
*/
public function __construct(BalancerInterface $balancer, LoggerInterface $logger = null)
public function __construct(ConfigurableBalancerInterface $balancer, LoggerInterface $logger = null)
{
parent::__construct($balancer);
$this->logger = $logger ?: new NullLogger();
Expand Down
11 changes: 8 additions & 3 deletions src/Decorator/LoggerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Cmp\FeatureBalancer\Decorator;

use Cmp\FeatureBalancer\BalancerInterface;
use Cmp\FeatureBalancer\ConfigurableBalancerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;

class LoggerDecorator extends BalancerDecorator implements BalancerInterface
class LoggerDecorator extends BalancerDecorator
{
/**
* @var LoggerInterface
Expand All @@ -19,7 +19,12 @@ class LoggerDecorator extends BalancerDecorator implements BalancerInterface
*/
private $level;

public function __construct(BalancerInterface $balancer, LoggerInterface $logger = null, $level = LogLevel::INFO)
/**
* @param ConfigurableBalancerInterface $balancer
* @param LoggerInterface|null $logger
* @param string $level
*/
public function __construct(ConfigurableBalancerInterface $balancer, LoggerInterface $logger = null, $level = LogLevel::INFO)
{
parent::__construct($balancer);
$this->logger = $logger ?: new NullLogger();
Expand Down
12 changes: 6 additions & 6 deletions src/Decorator/MonitoringDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Cmp\FeatureBalancer\Decorator;

use Cmp\FeatureBalancer\BalancerInterface;
use Cmp\FeatureBalancer\ConfigurableBalancerInterface;
use Cmp\Monitoring\Monitor;

class MonitoringDecorator extends BalancerDecorator implements BalancerInterface
class MonitoringDecorator extends BalancerDecorator
{
/**
* @var Monitor
Expand All @@ -20,11 +20,11 @@ class MonitoringDecorator extends BalancerDecorator implements BalancerInterface
/**
* MonitoringDecorator constructor.
*
* @param BalancerInterface $balancer
* @param Monitor $monitor
* @param string $metric The name of the metric to increment when resolving a feature path
* @param ConfigurableBalancerInterface $balancer
* @param Monitor $monitor
* @param string $metric The name of the metric to increment when resolving a feature path
*/
public function __construct(BalancerInterface $balancer, Monitor $monitor, $metric)
public function __construct(ConfigurableBalancerInterface $balancer, Monitor $monitor, $metric)
{
parent::__construct($balancer);
$this->monitor = $monitor;
Expand Down

0 comments on commit 6053b82

Please sign in to comment.