Skip to content

Commit

Permalink
#2: LoadBalancer setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Bastien committed Jul 10, 2016
1 parent 82dbf03 commit 2f59086
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ cs: checkstyle
checkstyle:
vendor/bin/phpcs --extensions=php -n --standard=PSR2 --report=full src

## Test
test: phpunit

phpunit:
vendor/bin/phpunit
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
}
],
"require": {
"php": "^5.4||^7.0"
"php": "^5.4||^7.0",
"symfony/http-foundation": "^3.1"
},
"require-dev": {
"phpunit/phpunit": "^5.0||^4.0",
Expand Down
12 changes: 12 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="LoadBalancer Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
16 changes: 16 additions & 0 deletions src/Chooser/ChooserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace NBN\LoadBalancer\Chooser;

/**
* @author Nicolas Bastien <[email protected]>
*/
interface ChooserInterface
{
/**
* @param Request $request
* @param array $hosts
* @return Response
*/
public function handleRequest(Request $request, array $hosts);
}
10 changes: 10 additions & 0 deletions src/Exception/NoRegisteredHostException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace NBN\LoadBalancer\Exception;

/**
* @author Nicolas Bastien <[email protected]>
*/
class NoRegisteredHostException extends \RuntimeException
{
}
22 changes: 22 additions & 0 deletions src/Host/HostInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace NBN\LoadBalancer\Host;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Nicolas Bastien <[email protected]>
*/
interface HostInterface
{
/**
* @return float
*/
public function getLoad();

/**
* @return Response
*/
public function handleRequest(Request $request);
}
53 changes: 53 additions & 0 deletions src/LoadBalancer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace NBN\LoadBalancer;

use NBN\LoadBalancer\Chooser\ChooserInterface;
use NBN\LoadBalancer\Exception\NoAvailableHostException;
use NBN\LoadBalancer\Exception\NoRegisterdHostException;
use NBN\LoadBalancer\Exception\NoRegisteredHostException;
use NBN\LoadBalancer\Host\HostInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Nicolas Bastien <[email protected]>
*/
class LoadBalancer implements LoadBalancerInterface
{
/**
* @var array|HostInterface[]
*/
protected $hosts;

/**
* @var ChooserInterface
*/
protected $chooser;

/**
* @param array|HostInterface[] $hosts
* @param ChooserInterface $chooser
*/
public function __construct(array $hosts, ChooserInterface $chooser)
{
$this->hosts = $hosts;
$this->chooser = $chooser;
}

public function addHost(HostInterface $host)
{
$this->hosts[] = $host;
}

/**
* {@inheritdoc}
*/
public function handleRequest(Request $request)
{
if (count($this->hosts) == 0) {
throw new NoRegisteredHostException();
}

}
}
19 changes: 19 additions & 0 deletions src/LoadBalancerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace NBN\LoadBalancer;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Nicolas Bastien <[email protected]>
*/
interface LoadBalancerInterface
{
/**
* Load balance the request according to loadbalancer configuration
*
* @return Response
*/
public function handleRequest(Request $request);
}
27 changes: 27 additions & 0 deletions tests/Unit/LoadBalancerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace NBN\LoadBalancer;

use NBN\LoadBalancer\Exception\NoRegisteredHostException;

/**
* vendor/bin/phpunit tests/Unit/LoadBalancerTest.php
*
* @author Nicolas Bastien <[email protected]>
*/
class LoadBalancerTest extends \PHPUnit_Framework_TestCase
{
/**
* @test LoadBalancer::handleRequest()
*/
public function testHandleRequestNoHostException()
{
$chooser = $this->prophesize('NBN\LoadBalancer\Chooser\ChooserInterface');
$resquest = $this->prophesize('Symfony\Component\HttpFoundation\Request');

$loadBalancer = new LoadBalancer([], $chooser->reveal());

$this->expectException(NoRegisteredHostException::class);
$loadBalancer->handleRequest($resquest->reveal());
}
}

0 comments on commit 2f59086

Please sign in to comment.