Skip to content

Commit

Permalink
Merged branch master into translate-template
Browse files Browse the repository at this point in the history
  • Loading branch information
axelitus committed Sep 23, 2016
2 parents c124146 + d4972f0 commit fc561c0
Show file tree
Hide file tree
Showing 288 changed files with 9,323 additions and 8,988 deletions.
67 changes: 16 additions & 51 deletions Behavioral/ChainOfResponsibilities/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,42 @@

namespace DesignPatterns\Behavioral\ChainOfResponsibilities;

/**
* Handler is a generic handler in the chain of responsibilities.
*
* Yes you could have a lighter CoR with a simpler handler but if you want your CoR
* to be extendable and decoupled, it's a better idea to do things like that in real
* situations. Usually, a CoR is meant to be changed everytime and evolves, that's
* why we slice the workflow in little bits of code.
*/
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

abstract class Handler
{
/**
* @var Handler
* @var Handler|null
*/
private $successor = null;

/**
* Append a responsibility to the end of chain.
*
* A prepend method could be done with the same spirit
*
* You could also send the successor in the constructor but in PHP that is a
* bad idea because you have to remove the type-hint of the parameter because
* the last handler has a null successor.
*
* And if you override the constructor, that Handler can no longer have a
* successor. One solution is to provide a NullObject (see pattern).
* It is more preferable to keep the constructor "free" to inject services
* you need with the DiC of symfony2 for example.
*
* @param Handler $handler
*/
final public function append(Handler $handler)
public function __construct(Handler $handler = null)
{
if (is_null($this->successor)) {
$this->successor = $handler;
} else {
$this->successor->append($handler);
}
$this->successor = $handler;
}

/**
* Handle the request.
*
* This approach by using a template method pattern ensures you that
* each subclass will not forget to call the successor. Besides, the returned
* boolean value indicates you if the request have been processed or not.
* each subclass will not forget to call the successor
*
* @param Request $req
* @param RequestInterface $request
*
* @return bool
* @return string|null
*/
final public function handle(Request $req)
final public function handle(RequestInterface $request)
{
$req->forDebugOnly = get_called_class();
$processed = $this->processing($req);
if (!$processed) {
$processed = $this->processing($request);

if ($processed === null) {
// the request has not been processed by this handler => see the next
if (!is_null($this->successor)) {
$processed = $this->successor->handle($req);
if ($this->successor !== null) {
$processed = $this->successor->handle($request);
}
}

return $processed;
}

/**
* Each concrete handler has to implement the processing of the request.
*
* @param Request $req
*
* @return bool true if the request has been processed
*/
abstract protected function processing(Request $req);
abstract protected function processing(RequestInterface $request);
}
6 changes: 0 additions & 6 deletions Behavioral/ChainOfResponsibilities/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ Code

You can also find these code on `GitHub`_

Request.php

.. literalinclude:: Request.php
:language: php
:linenos:

Handler.php

.. literalinclude:: Handler.php
Expand Down
19 changes: 0 additions & 19 deletions Behavioral/ChainOfResponsibilities/Request.php

This file was deleted.

40 changes: 0 additions & 40 deletions Behavioral/ChainOfResponsibilities/Responsible/FastStorage.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;

use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
use Psr\Http\Message\RequestInterface;

class HttpInMemoryCacheHandler extends Handler
{
/**
* @var array
*/
private $data;

/**
* @param array $data
* @param Handler|null $successor
*/
public function __construct(array $data, Handler $successor = null)
{
parent::__construct($successor);

$this->data = $data;
}

/**
* @param RequestInterface $request
*
* @return string|null
*/
protected function processing(RequestInterface $request)
{
$key = sprintf(
'%s?%s',
$request->getUri()->getPath(),
$request->getUri()->getQuery()
);

if ($request->getMethod() == 'GET' && isset($this->data[$key])) {
return $this->data[$key];
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;

use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
use Psr\Http\Message\RequestInterface;

class SlowDatabaseHandler extends Handler
{
/**
* @param RequestInterface $request
*
* @return string|null
*/
protected function processing(RequestInterface $request)
{
// this is a mockup, in production code you would ask a slow (compared to in-memory) DB for the results

return 'Hello World!';
}
}
44 changes: 0 additions & 44 deletions Behavioral/ChainOfResponsibilities/Responsible/SlowStorage.php

This file was deleted.

84 changes: 27 additions & 57 deletions Behavioral/ChainOfResponsibilities/Tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,50 @@

namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Tests;

use DesignPatterns\Behavioral\ChainOfResponsibilities\Request;
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage;
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage;
use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\HttpInMemoryCacheHandler;
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowDatabaseHandler;

/**
* ChainTest tests the CoR.
*/
class ChainTest extends \PHPUnit_Framework_TestCase
{
/**
* @var FastStorage
* @var Handler
*/
protected $chain;
private $chain;

protected function setUp()
{
$this->chain = new FastStorage(array('bar' => 'baz'));
$this->chain->append(new SlowStorage(array('bar' => 'baz', 'foo' => 'bar')));
}

public function makeRequest()
{
$request = new Request();
$request->verb = 'get';

return array(
array($request),
$this->chain = new HttpInMemoryCacheHandler(
['/foo/bar?index=1' => 'Hello In Memory!'],
new SlowDatabaseHandler()
);
}

/**
* @dataProvider makeRequest
*/
public function testFastStorage($request)
public function testCanRequestKeyInFastStorage()
{
$request->key = 'bar';
$ret = $this->chain->handle($request);

$this->assertTrue($ret);
$this->assertObjectHasAttribute('response', $request);
$this->assertEquals('baz', $request->response);
// despite both handle owns the 'bar' key, the FastStorage is responding first
$className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage';
$this->assertEquals($className, $request->forDebugOnly);
}
$uri = $this->createMock('Psr\Http\Message\UriInterface');
$uri->method('getPath')->willReturn('/foo/bar');
$uri->method('getQuery')->willReturn('index=1');

/**
* @dataProvider makeRequest
*/
public function testSlowStorage($request)
{
$request->key = 'foo';
$ret = $this->chain->handle($request);
$request = $this->createMock('Psr\Http\Message\RequestInterface');
$request->method('getMethod')
->willReturn('GET');
$request->method('getUri')->willReturn($uri);

$this->assertTrue($ret);
$this->assertObjectHasAttribute('response', $request);
$this->assertEquals('bar', $request->response);
// FastStorage has no 'foo' key, the SlowStorage is responding
$className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';
$this->assertEquals($className, $request->forDebugOnly);
$this->assertEquals('Hello In Memory!', $this->chain->handle($request));
}

/**
* @dataProvider makeRequest
*/
public function testFailure($request)
public function testCanRequestKeyInSlowStorage()
{
$request->key = 'kurukuku';
$ret = $this->chain->handle($request);
$uri = $this->createMock('Psr\Http\Message\UriInterface');
$uri->method('getPath')->willReturn('/foo/baz');
$uri->method('getQuery')->willReturn('');

$request = $this->createMock('Psr\Http\Message\RequestInterface');
$request->method('getMethod')
->willReturn('GET');
$request->method('getUri')->willReturn($uri);

$this->assertFalse($ret);
// the last responsible :
$className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';
$this->assertEquals($className, $request->forDebugOnly);
$this->assertEquals('Hello World!', $this->chain->handle($request));
}
}
Loading

0 comments on commit fc561c0

Please sign in to comment.