Skip to content

Commit

Permalink
use command/query types
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Oct 5, 2019
1 parent f371bbb commit 9f93eb6
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 17 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ require './vendor/autoload.php';
```php
use Gears\CQRS\Tactician\CommandBus;
use Gears\CQRS\Tactician\CommandHandlerMiddleware;
use Gears\CQRS\Tactician\CommandInflector;
use League\Tactician\CommandBus as TacticianBus;
use League\Tactician\Handler\CommandHandlerMiddleware as TacticianHandlerMiddleware;
use League\Tactician\Handler\Locator\InMemoryLocator;
use League\Tactician\Plugins\LockingMiddleware;

Expand All @@ -59,9 +57,7 @@ $commandBus->handle($command);
```php
use Gears\CQRS\Tactician\QueryBus;
use Gears\CQRS\Tactician\QueryHandlerMiddleware;
use Gears\CQRS\Tactician\QueryInflector;
use League\Tactician\CommandBus as TacticianBus;
use League\Tactician\Handler\CommandHandlerMiddleware as TacticianHandlerMiddleware;
use League\Tactician\Handler\Locator\InMemoryLocator;
use League\Tactician\Plugins\LockingMiddleware;

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"require": {
"php": "^7.1",
"league/tactician": "^1.0",
"phpgears/cqrs": "~0.3"
"phpgears/cqrs": "~0.3.1"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.1",
Expand Down
43 changes: 43 additions & 0 deletions src/CommandExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* cqrs-tactician (https://github.com/phpgears/cqrs-tactician).
* CQRS implementation with League Tactician.
*
* @license MIT
* @link https://github.com/phpgears/cqrs-tactician
* @author Julián Gutiérrez <juliangut@gmail.com>
*/

declare(strict_types=1);

namespace Gears\CQRS\Tactician;

use Gears\CQRS\Command;
use Gears\CQRS\Exception\InvalidCommandException;
use League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor;

final class CommandExtractor implements CommandNameExtractor
{
/**
* Extract the name from a command.
*
* @param mixed $command
*
* @throws InvalidCommandException
*
* @return string
*/
public function extract($command)
{
if (!$command instanceof Command) {
throw new InvalidCommandException(\sprintf(
'Command must implement "%s" interface, "%s" given',
Command::class,
\is_object($command) ? \get_class($command) : \gettype($command)
));
}

return $command->getCommandType();
}
}
3 changes: 1 addition & 2 deletions src/CommandHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Gears\CQRS\Tactician;

use League\Tactician\Handler\CommandHandlerMiddleware as TacticianHandlerMiddleware;
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
use League\Tactician\Handler\Locator\HandlerLocator;

final class CommandHandlerMiddleware extends TacticianHandlerMiddleware
Expand All @@ -27,7 +26,7 @@ final class CommandHandlerMiddleware extends TacticianHandlerMiddleware
public function __construct(HandlerLocator $handlerLocator)
{
parent::__construct(
new ClassNameExtractor(),
new CommandExtractor(),
$handlerLocator,
new CommandInflector()
);
Expand Down
4 changes: 2 additions & 2 deletions src/CommandInflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public function inflect($command, $handler): string
{
if (!$command instanceof Command) {
throw new InvalidCommandException(\sprintf(
'Command must implement %s interface, %s given',
'Command must implement "%s" interface, "%s" given',
Command::class,
\is_object($command) ? \get_class($command) : \gettype($command)
));
}

if (!$handler instanceof CommandHandler) {
throw new InvalidCommandHandlerException(\sprintf(
'Command handler must implement %s interface, %s given',
'Command handler must implement "%s" interface, "%s" given',
CommandHandler::class,
\is_object($handler) ? \get_class($handler) : \gettype($handler)
));
Expand Down
43 changes: 43 additions & 0 deletions src/QueryExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* cqrs-tactician (https://github.com/phpgears/cqrs-tactician).
* CQRS implementation with League Tactician.
*
* @license MIT
* @link https://github.com/phpgears/cqrs-tactician
* @author Julián Gutiérrez <juliangut@gmail.com>
*/

declare(strict_types=1);

namespace Gears\CQRS\Tactician;

use Gears\CQRS\Exception\InvalidQueryException;
use Gears\CQRS\Query;
use League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor;

final class QueryExtractor implements CommandNameExtractor
{
/**
* Extract the name from a query.
*
* @param mixed $query
*
* @throws InvalidQueryException
*
* @return string
*/
public function extract($query)
{
if (!$query instanceof Query) {
throw new InvalidQueryException(\sprintf(
'Query must implement "%s" interface, "%s" given',
Query::class,
\is_object($query) ? \get_class($query) : \gettype($query)
));
}

return $query->getQueryType();
}
}
3 changes: 1 addition & 2 deletions src/QueryHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Gears\CQRS\Tactician;

use League\Tactician\Handler\CommandHandlerMiddleware as TacticianHandlerMiddleware;
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
use League\Tactician\Handler\Locator\HandlerLocator;

final class QueryHandlerMiddleware extends TacticianHandlerMiddleware
Expand All @@ -27,7 +26,7 @@ final class QueryHandlerMiddleware extends TacticianHandlerMiddleware
public function __construct(HandlerLocator $handlerLocator)
{
parent::__construct(
new ClassNameExtractor(),
new QueryExtractor(),
$handlerLocator,
new QueryInflector()
);
Expand Down
4 changes: 2 additions & 2 deletions src/QueryInflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public function inflect($command, $handler): string
{
if (!$command instanceof Query) {
throw new InvalidQueryException(\sprintf(
'Query must implement %s interface, %s given',
'Query must implement "%s" interface, "%s" given',
Query::class,
\is_object($command) ? \get_class($command) : \gettype($command)
));
}

if (!$handler instanceof QueryHandler) {
throw new InvalidQueryHandlerException(\sprintf(
'Query handler must implement %s interface, %s given',
'Query handler must implement "%s" interface, "%s" given',
QueryHandler::class,
\is_object($handler) ? \get_class($handler) : \gettype($handler)
));
Expand Down
40 changes: 40 additions & 0 deletions tests/Tactician/CommandExtractorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* cqrs-tactician (https://github.com/phpgears/cqrs-tactician).
* CQRS implementation with League Tactician.
*
* @license MIT
* @link https://github.com/phpgears/cqrs-tactician
* @author Julián Gutiérrez <juliangut@gmail.com>
*/

declare(strict_types=1);

namespace Gears\CQRS\Tactician\Tests;

use Gears\CQRS\Exception\InvalidCommandException;
use Gears\CQRS\Tactician\CommandExtractor;
use Gears\CQRS\Tactician\Tests\Stub\CommandStub;
use PHPUnit\Framework\TestCase;

/**
* Command extractor test.
*/
class CommandExtractorTest extends TestCase
{
public function testInvalidCommand(): void
{
$this->expectException(InvalidCommandException::class);
$this->expectExceptionMessage('Command must implement "Gears\CQRS\Command" interface, "stdClass" given');

(new CommandExtractor())->extract(new \stdClass());
}

public function testExtract(): void
{
$type = (new CommandExtractor())->extract(CommandStub::instance());

static::assertSame(CommandStub::class, $type);
}
}
4 changes: 2 additions & 2 deletions tests/Tactician/CommandInflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CommandInflectorTest extends TestCase
public function testInvalidCommand(): void
{
$this->expectException(InvalidCommandException::class);
$this->expectExceptionMessage('Command must implement Gears\CQRS\Command interface, string given');
$this->expectExceptionMessage('Command must implement "Gears\CQRS\Command" interface, "string" given');

(new CommandInflector())->inflect('', '');
}
Expand All @@ -37,7 +37,7 @@ public function testInvalidCommandHandler(): void
{
$this->expectException(InvalidCommandHandlerException::class);
$this->expectExceptionMessage(
'Command handler must implement Gears\CQRS\CommandHandler interface, string given'
'Command handler must implement "Gears\CQRS\CommandHandler" interface, "string" given'
);

(new CommandInflector())->inflect(CommandStub::instance(), '');
Expand Down
40 changes: 40 additions & 0 deletions tests/Tactician/QueryExtractorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* cqrs-tactician (https://github.com/phpgears/cqrs-tactician).
* CQRS implementation with League Tactician.
*
* @license MIT
* @link https://github.com/phpgears/cqrs-tactician
* @author Julián Gutiérrez <juliangut@gmail.com>
*/

declare(strict_types=1);

namespace Gears\CQRS\Tactician\Tests;

use Gears\CQRS\Exception\InvalidQueryException;
use Gears\CQRS\Tactician\QueryExtractor;
use Gears\CQRS\Tactician\Tests\Stub\QueryStub;
use PHPUnit\Framework\TestCase;

/**
* Query extractor test.
*/
class QueryExtractorTest extends TestCase
{
public function testInvalidQuery(): void
{
$this->expectException(InvalidQueryException::class);
$this->expectExceptionMessage('Query must implement "Gears\CQRS\Query" interface, "stdClass" given');

(new QueryExtractor())->extract(new \stdClass());
}

public function testExtract(): void
{
$type = (new QueryExtractor())->extract(QueryStub::instance());

static::assertSame(QueryStub::class, $type);
}
}
6 changes: 4 additions & 2 deletions tests/Tactician/QueryInflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ class QueryInflectorTest extends TestCase
public function testInvalidCommand(): void
{
$this->expectException(InvalidQueryException::class);
$this->expectExceptionMessage('Query must implement Gears\CQRS\Query interface, string given');
$this->expectExceptionMessage('Query must implement "Gears\CQRS\Query" interface, "string" given');

(new QueryInflector())->inflect('', '');
}

public function testInvalidCommandHandler(): void
{
$this->expectException(InvalidQueryHandlerException::class);
$this->expectExceptionMessage('Query handler must implement Gears\CQRS\QueryHandler interface, string given');
$this->expectExceptionMessage(
'Query handler must implement "Gears\CQRS\QueryHandler" interface, "string" given'
);

(new QueryInflector())->inflect(QueryStub::instance(), '');
}
Expand Down

0 comments on commit 9f93eb6

Please sign in to comment.