Skip to content
This repository was archived by the owner on Mar 29, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

namespace MPWAR\Module\Economy\Application\Service;

use iter;
use MPWAR\Module\Economy\Domain\Account;
use MPWAR\Module\Economy\Domain\AccountOwner;
use MPWAR\Module\Economy\Domain\AccountRepository;
use MPWAR\Module\Economy\Domain\VirtualMoney;
use SimpleBus\Message\Bus\MessageBus;
use SimpleBus\Message\Type\Event;

final class TransactionProcessor
{
private $repository;
private $eventBus;

public function __construct(AccountRepository $repository)
public function __construct(AccountRepository $repository, MessageBus $eventBus)
{
$this->repository = $repository;
$this->eventBus = $eventBus;
}

public function __invoke(AccountOwner $owner, VirtualMoney $money)
Expand All @@ -22,5 +28,20 @@ public function __invoke(AccountOwner $owner, VirtualMoney $money)
$account->add($money);

$this->repository->save($account);

$this->publishDomainEvents($account);
}

private function publishDomainEvents(Account $account)
{
iter\apply($this->handleEvent(), $account->recordedMessages());
$account->eraseMessages();
}

private function handleEvent()
{
return function (Event $event) {
$this->eventBus->handle($event);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace MPWAR\Module\Economy\Contract\DomainEvent;

use DateTimeImmutable;
use SimpleBus\Message\Type\Event;

final class AccountBalanceChanged implements Event
{
private $aggregateId;
private $occurredOn;
private $amount;
private $currency;

public function __construct($aggregateId, $amount, $currency)
{
$this->aggregateId = $aggregateId;
$this->occurredOn = new DateTimeImmutable();
$this->amount = $amount;
$this->currency = $currency;
}
}
5 changes: 5 additions & 0 deletions src/MPWAR/Module/Economy/Domain/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MPWAR\Module\Economy\Domain;

use DateTimeImmutable;
use MPWAR\Module\Economy\Contract\DomainEvent\AccountBalanceChanged;
use MPWAR\Module\Economy\Contract\DomainEvent\AccountOpened;
use SimpleBus\Message\Recorder\PrivateMessageRecorderCapabilities;
use SimpleBus\Message\Recorder\RecordsMessages;
Expand Down Expand Up @@ -42,5 +43,9 @@ public function balance()
public function add(VirtualMoney $money)
{
$this->balance = $this->balance()->add($money);

$this->record(
new AccountBalanceChanged($this->owner()->value(), $money->amount(), $money->currency()->value())
);
}
}
10 changes: 10 additions & 0 deletions src/MPWAR/Module/Economy/Domain/VirtualMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public function add(VirtualMoney $other)
return new self($this->amount + $other->amount, $this->currency);
}

public function amount()
{
return $this->amount;
}

public function currency()
{
return $this->currency;
}

private function guard($amount)
{
if (!is_int($amount) || 0 > $amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ services:
class: MPWAR\Module\Economy\Application\Service\TransactionProcessor
arguments:
- @mpwar.economy.infrastructure.account_repository
- @event_bus
public: false