Skip to content

Commit

Permalink
Merge b2f549e into 1fa3615
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaleotti committed Apr 8, 2017
2 parents 1fa3615 + b2f549e commit 5d1974f
Show file tree
Hide file tree
Showing 20 changed files with 310 additions and 308 deletions.
18 changes: 16 additions & 2 deletions CHANGELOG.md
Expand Up @@ -2,7 +2,21 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.4.0 TBA
## 0.5.0 TBA

### Added

* PSR-15 support

### Deprecated

* Nothing

### Removed

* Nothing

## 0.4.0 (2017-01-12)

### Added

Expand All @@ -17,7 +31,7 @@ All notable changes to this project will be documented in this file, in reverse

* Nothing

## 0.3.0
## 0.3.0 (2016-05-08)

### Added

Expand Down
8 changes: 6 additions & 2 deletions composer.json
Expand Up @@ -32,7 +32,9 @@
"service bus",
"event sourcing",
"messaging",
"psr7"
"psr7",
"psr-15",
"psr15"
],
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -41,7 +43,9 @@
"prooph/common": "^4.0",
"prooph/service-bus" : "^6.0",
"psr/http-message": "^1.0",
"react/promise" : "^2.2"
"react/promise" : "^2.2",
"http-interop/http-middleware": "^0.4.1",
"fig/http-message-util": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
Expand Down
25 changes: 10 additions & 15 deletions docs/book/zend_expressive.md
Expand Up @@ -9,22 +9,17 @@ example app.
Here is an example for the `AuraRouter` to call the `CommandMiddleware` for the `register-user` command.

```php
return [
'routes' => [
[
'name' => 'command::register-user',
'path' => '/api/commands/register-user',
'middleware' => \Prooph\Psr7Middleware\CommandMiddleware::class,
'allowed_methods' => ['POST'],
'options' => [
'values' => [
// \Prooph\Common\Messaging\FQCNMessageFactory is used here
\Prooph\Psr7Middleware\CommandMiddleware::NAME_ATTRIBUTE => \Prooph\ProophessorDo\Model\User\Command\RegisterUser::class,
]
]
// routes.php

/** @var \Zend\Expressive\Application $app */
$app->post('/api/commands/register-user', [
\Prooph\Psr7Middleware\CommandMiddleware::class,
], 'command::register-user')
->setOptions([
'values' => [
\Prooph\Psr7Middleware\CommandMiddleware::NAME_ATTRIBUTE => \Prooph\ProophessorDo\Model\User\Command\RegisterUser::class,
],
],
];
]);
```

## Metadata Gatherer
Expand Down
46 changes: 25 additions & 21 deletions src/CommandMiddleware.php
Expand Up @@ -12,10 +12,13 @@

namespace Prooph\Psr7Middleware;

use Fig\Http\Message\StatusCodeInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Prooph\Common\Messaging\MessageFactory;
use Prooph\Psr7Middleware\Exception\RuntimeException;
use Prooph\Psr7Middleware\Response\ResponseStrategy;
use Prooph\ServiceBus\CommandBus;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
Expand All @@ -24,14 +27,14 @@
* The CommandBus is designed to dispatch a message to only one handler or message producer. It does not return a
* result. Sending a command means fire and forget and enforces the Tell-Don't-Ask principle.
*/
final class CommandMiddleware implements Middleware
final class CommandMiddleware implements MiddlewareInterface
{
/**
* Identifier to execute specific command
*
* @var string
*/
const NAME_ATTRIBUTE = 'prooph_command_name';
public const NAME_ATTRIBUTE = 'prooph_command_name';

/**
* Dispatches command
Expand All @@ -54,28 +57,33 @@ final class CommandMiddleware implements Middleware
*/
private $metadataGatherer;

/**
* Generate HTTP response with status code
*
* @var ResponseStrategy
*/
private $responseStrategy;

public function __construct(
CommandBus $commandBus,
MessageFactory $commandFactory,
MetadataGatherer $metadataGatherer
MetadataGatherer $metadataGatherer,
ResponseStrategy $responseStrategy
) {
$this->commandBus = $commandBus;
$this->commandFactory = $commandFactory;
$this->metadataGatherer = $metadataGatherer;
$this->responseStrategy = $responseStrategy;
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$commandName = $request->getAttribute(self::NAME_ATTRIBUTE);

if (null === $commandName) {
return $next(
$request,
$response,
new RuntimeException(
sprintf('Command name attribute ("%s") was not found in request.', self::NAME_ATTRIBUTE),
Middleware::STATUS_CODE_BAD_REQUEST
)
throw new RuntimeException(
sprintf('Command name attribute ("%s") was not found in request.', self::NAME_ATTRIBUTE),
StatusCodeInterface::STATUS_BAD_REQUEST
);
}

Expand All @@ -87,16 +95,12 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

$this->commandBus->dispatch($command);

return $response->withStatus(Middleware::STATUS_CODE_ACCEPTED);
return $this->responseStrategy->withStatus(StatusCodeInterface::STATUS_ACCEPTED);
} catch (\Throwable $e) {
return $next(
$request,
$response,
new RuntimeException(
sprintf('An error occurred during dispatching of command "%s"', $commandName),
Middleware::STATUS_CODE_INTERNAL_SERVER_ERROR,
$e
)
throw new RuntimeException(
sprintf('An error occurred during dispatching of command "%s"', $commandName),
StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR,
$e
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Container/CommandMiddlewareFactory.php
Expand Up @@ -42,7 +42,8 @@ public function __invoke(ContainerInterface $container): CommandMiddleware
return new CommandMiddleware(
$container->get($options['command_bus']),
$container->get($options['message_factory']),
$gatherer
$gatherer,
$container->get($options['response_strategy'])
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Container/EventMiddlewareFactory.php
Expand Up @@ -42,7 +42,8 @@ public function __invoke(ContainerInterface $container): EventMiddleware
return new EventMiddleware(
$container->get($options['event_bus']),
$container->get($options['message_factory']),
$gatherer
$gatherer,
$container->get($options['response_strategy'])
);
}

Expand Down
46 changes: 25 additions & 21 deletions src/EventMiddleware.php
Expand Up @@ -12,10 +12,13 @@

namespace Prooph\Psr7Middleware;

use Fig\Http\Message\StatusCodeInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Prooph\Common\Messaging\MessageFactory;
use Prooph\Psr7Middleware\Exception\RuntimeException;
use Prooph\Psr7Middleware\Response\ResponseStrategy;
use Prooph\ServiceBus\EventBus;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
Expand All @@ -24,14 +27,14 @@
* The EventBus is able to dispatch a message to n listeners. Each listener can be a message handler or message
* producer. Like commands the EventBus doesn't return anything.
*/
final class EventMiddleware implements Middleware
final class EventMiddleware implements MiddlewareInterface
{
/**
* Identifier to execute specific event
*
* @var string
*/
const NAME_ATTRIBUTE = 'prooph_event_name';
public const NAME_ATTRIBUTE = 'prooph_event_name';

/**
* Dispatches event
Expand All @@ -54,28 +57,33 @@ final class EventMiddleware implements Middleware
*/
private $metadataGatherer;

/**
* Generate HTTP response with status code
*
* @var ResponseStrategy
*/
private $responseStrategy;

public function __construct(
EventBus $eventBus,
MessageFactory $eventFactory,
MetadataGatherer $metadataGatherer
MetadataGatherer $metadataGatherer,
ResponseStrategy $responseStrategy
) {
$this->eventBus = $eventBus;
$this->eventFactory = $eventFactory;
$this->metadataGatherer = $metadataGatherer;
$this->responseStrategy = $responseStrategy;
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$eventName = $request->getAttribute(self::NAME_ATTRIBUTE);

if (null === $eventName) {
return $next(
$request,
$response,
new RuntimeException(
sprintf('Event name attribute ("%s") was not found in request.', self::NAME_ATTRIBUTE),
Middleware::STATUS_CODE_BAD_REQUEST
)
throw new RuntimeException(
sprintf('Event name attribute ("%s") was not found in request.', self::NAME_ATTRIBUTE),
StatusCodeInterface::STATUS_BAD_REQUEST
);
}

Expand All @@ -87,16 +95,12 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

$this->eventBus->dispatch($event);

return $response->withStatus(Middleware::STATUS_CODE_ACCEPTED);
return $this->responseStrategy->withStatus(StatusCodeInterface::STATUS_ACCEPTED);
} catch (\Throwable $e) {
return $next(
$request,
$response,
new RuntimeException(
sprintf('An error occurred during dispatching of event "%s"', $eventName),
Middleware::STATUS_CODE_INTERNAL_SERVER_ERROR,
$e
)
throw new RuntimeException(
sprintf('An error occurred during dispatching of event "%s"', $eventName),
StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR,
$e
);
}
}
Expand Down

0 comments on commit 5d1974f

Please sign in to comment.