-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query bus #6
Open
danizord
wants to merge
1
commit into
master
Choose a base branch
from
feature/query-bus
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Query bus #6
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace ZfrCommandBus\Container; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use ZfrCommandBus\QueryBusInterface; | ||
use ZfrCommandBus\SimpleQueryBus; | ||
|
||
/** | ||
* @author Daniel Gimenes | ||
*/ | ||
final class SimpleQueryBusFactory | ||
{ | ||
/** | ||
* @param ContainerInterface $container | ||
* | ||
* @return QueryBusInterface | ||
*/ | ||
public function __invoke(ContainerInterface $container): QueryBusInterface | ||
{ | ||
/** @var array $config */ | ||
$config = $container->get('config'); | ||
|
||
return new SimpleQueryBus($container, $config['zfr_command_bus']['query_handlers'] ?? []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace ZfrCommandBus; | ||
|
||
/** | ||
* @author Daniel Gimenes | ||
*/ | ||
interface MessageBusInterface | ||
{ | ||
/** | ||
* @param object $message | ||
* | ||
* @return array|void | ||
*/ | ||
public function dispatch($message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace ZfrCommandBus; | ||
|
||
/** | ||
* @author Daniel Gimenes | ||
*/ | ||
interface QueryBusInterface extends MessageBusInterface | ||
{ | ||
/** | ||
* @param object $query | ||
* | ||
* @return array | ||
*/ | ||
public function dispatch($query): array; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can typehint on object on PHP 7.1 no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope :/ |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace ZfrCommandBus; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use ZfrCommandBus\Exception\InvalidArgumentException; | ||
use ZfrCommandBus\Exception\RuntimeException; | ||
|
||
/** | ||
* @author Daniel Gimenes | ||
*/ | ||
final class SimpleQueryBus implements QueryBusInterface | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $queryMap; | ||
|
||
/** | ||
* @param ContainerInterface $container | ||
* @param array $queryMap | ||
*/ | ||
public function __construct(ContainerInterface $container, array $queryMap = []) | ||
{ | ||
$this->container = $container; | ||
$this->queryMap = $queryMap; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
public function dispatch($query): array | ||
{ | ||
if (!is_object($query)) { | ||
throw new InvalidArgumentException(sprintf( | ||
'$query must be an object, %s given', | ||
gettype($query) | ||
)); | ||
} | ||
|
||
$queryClass = get_class($query); | ||
$queryHandlerName = $this->queryMap[$queryClass] ?? "{$queryClass}Handler"; | ||
$queryHandler = $this->container->get($queryHandlerName); | ||
|
||
assert(is_callable($queryHandler), 'Make sure your query handler is callable'); | ||
|
||
return $queryHandler($query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace ZfrCommandBusTest\Asset; | ||
|
||
/** | ||
* @author Daniel Gimenes | ||
*/ | ||
final class TestQuery | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace ZfrCommandBusTest\Asset; | ||
|
||
/** | ||
* @author Daniel Gimenes | ||
*/ | ||
final class TestQueryHandler | ||
{ | ||
public function __invoke(TestQuery $query): array | ||
{ | ||
return ['foo' => 'bar']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace ZfrCommandBusTest\Container; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use ZfrCommandBus\Container\SimpleQueryBusFactory; | ||
|
||
/** | ||
* @author Daniel Gimenes | ||
*/ | ||
final class SimpleQueryBusFactoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testUsesEmptyHandlerMapByDefault() | ||
{ | ||
$container = $this->prophesize(ContainerInterface::class); | ||
$factory = new SimpleQueryBusFactory(); | ||
|
||
$container->get('config')->shouldBeCalled()->willReturn([]); | ||
|
||
$factory($container->reveal()); | ||
} | ||
|
||
public function testCreatesInstanceOfSimpleQueryBus() | ||
{ | ||
$container = $this->prophesize(ContainerInterface::class); | ||
$factory = new SimpleQueryBusFactory(); | ||
|
||
$container->get('config')->shouldBeCalled()->willReturn([ | ||
'zfr_command_bus' => [ | ||
'query_handlers' => [], | ||
], | ||
]); | ||
|
||
$factory($container->reveal()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace ZfrCommandBusTest; | ||
|
||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Container\ContainerInterface; | ||
use stdClass; | ||
use ZfrCommandBus\Exception\InvalidArgumentException; | ||
use ZfrCommandBus\SimpleQueryBus; | ||
use ZfrCommandBusTest\Asset\TestQuery; | ||
use ZfrCommandBusTest\Asset\TestQueryHandler; | ||
|
||
/** | ||
* @author Daniel Gimenes | ||
*/ | ||
final class SimpleQueryBusTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ObjectProphecy|ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
private $queryHandler; | ||
|
||
protected function setUp() | ||
{ | ||
$this->container = $this->prophesize(ContainerInterface::class); | ||
$this->queryHandler = function (stdClass $query) { | ||
return ['foo' => 'bar']; | ||
}; | ||
} | ||
|
||
public function testThrowsExceptionIfInvalidQueryType() | ||
{ | ||
$queryBus = new SimpleQueryBus($this->container->reveal()); | ||
|
||
$this->setExpectedException(InvalidArgumentException::class); | ||
|
||
$queryBus->dispatch('string'); | ||
} | ||
|
||
public function testDispatchesQueryToMappedQueryHandler() | ||
{ | ||
$queryBus = new SimpleQueryBus($this->container->reveal(), [stdClass::class => 'My\TestQueryHandler']); | ||
|
||
$this->container->get('My\TestQueryHandler')->shouldBeCalled()->willReturn($this->queryHandler); | ||
|
||
$result = $queryBus->dispatch(new stdClass()); | ||
|
||
$this->assertSame(['foo' => 'bar'], $result); | ||
} | ||
|
||
public function testTriesToGuessQueryHandlerNameWithSuffix() | ||
{ | ||
$queryBus = new SimpleQueryBus($this->container->reveal()); | ||
|
||
$this->container->get(TestQueryHandler::class)->shouldBeCalled()->willReturn(new TestQueryHandler()); | ||
|
||
$result = $queryBus->dispatch(new TestQuery()); | ||
|
||
$this->assertSame(['foo' => 'bar'], $result); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the "dispatch" should return typehint void?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This project is still at 7.0 yet, so we don't have void typehint.