Skip to content

Commit

Permalink
changed parameter order
Browse files Browse the repository at this point in the history
  • Loading branch information
othillo committed Apr 8, 2020
1 parent 71f32de commit 07c03da
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
12 changes: 1 addition & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
# Changelog

## v0.5.x
## 0.9.x

The major change in this version, is that `sagas` are now handled with
the `DomainMessage` instead of the `event`. This makes it more consistent with the
rest of the broadway framework, where all event listeners are handled with the
`DomainMessage`.


#### BC breaks

- The `Saga::handle*()` arguments are reordered with `State`, `event`, `DomainMessage`

#### Other changes

- `StaticallyConfiguredSagaInterface::configuration` array of callables gets a second argument which holds the `DomainMessage`. This way it is not necessary to put the identifier in each `event` because you can fetch it from the `DomainMessage`

6 changes: 3 additions & 3 deletions examples/ReservationSaga.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function configuration()
];
}

public function handleOrderPlaced(State $state, OrderPlaced $event)
public function handleOrderPlaced(OrderPlaced $event, State $state)
{
// keep the order id, for reference in `handleReservationAccepted()` and `handleReservationRejected()`
$state->set('orderId', $event->orderId());
Expand All @@ -70,7 +70,7 @@ public function handleOrderPlaced(State $state, OrderPlaced $event)
return $state;
}

public function handleReservationAccepted(State $state, ReservationAccepted $event)
public function handleReservationAccepted(ReservationAccepted $event, State $state)
{
// the seat reservation for the given order is has been accepted, mark the order as booked
$command = new MarkOrderAsBooked($state->get('orderId'));
Expand All @@ -82,7 +82,7 @@ public function handleReservationAccepted(State $state, ReservationAccepted $eve
return $state;
}

public function handleReservationRejected(State $state, ReservationRejected $event)
public function handleReservationRejected(ReservationRejected $event, State $state)
{
// the seat reservation for the given order is has been rejected, reject the order as well
$command = new RejectOrder($state->get('orderId'));
Expand Down
9 changes: 4 additions & 5 deletions src/Saga.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ abstract class Saga implements SagaInterface
*/
public function handle(DomainMessage $domainMessage, State $state)
{
$event = $domainMessage->getPayload();
$method = $this->getHandleMethod($event);
$method = $this->getHandleMethod($domainMessage);

if (!method_exists($this, $method)) {
throw new \BadMethodCallException(sprintf("No handle method '%s' for event '%s'.", $method, get_class($event)));
}

return $this->$method($state, $event, $domainMessage);
return $this->$method($domainMessage->getPayload(), $state);
}

private function getHandleMethod($event)
private function getHandleMethod(DomainMessage $domainMessage)
{
$classParts = explode('\\', get_class($event));
$classParts = explode('\\', get_class($domainMessage->getPayload()));

return 'handle'.end($classParts);
}
Expand Down

0 comments on commit 07c03da

Please sign in to comment.