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
@@ -0,0 +1,26 @@
<?php

namespace MPWAR\Module\Economy\Application\Service;

use MPWAR\Module\Economy\Domain\AccountOwner;
use MPWAR\Module\Economy\Domain\AccountRepository;
use MPWAR\Module\Economy\Domain\VirtualMoney;

final class TransactionProcessor
{
private $repository;

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

public function __invoke(AccountOwner $owner, VirtualMoney $money)
{
$account = $this->repository->search($owner);

$account->add($money);

$this->repository->save($account);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace MPWAR\Module\Economy\Contract\Exception;

use DomainException;

final class VirtualMoneyOperationWithDifferentCurrenciesNotAllowedException extends DomainException
{
public function __construct()
{
parent::__construct('Not allowed Virtual Money operation between different currencies');

$this->code = 'economy_virtual_money_operation_not_valid';
}
}
17 changes: 11 additions & 6 deletions src/MPWAR/Module/Economy/Domain/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function __construct(AccountOwner $owner, VirtualMoney $balance)
$this->balance = $balance;
}

public static function open(AccountOwner $owner)
{
$account = new self($owner, VirtualMoney::coins(0));

$account->record(new AccountOpened($owner->value(), new DateTimeImmutable()));

return $account;
}

public function owner()
{
return $this->owner;
Expand All @@ -30,12 +39,8 @@ public function balance()
return $this->balance;
}

public static function open(AccountOwner $owner)
public function add(VirtualMoney $money)
{
$account = new self($owner, VirtualMoney::coins(0));

$account->record(new AccountOpened($owner->value(), new DateTimeImmutable()));

return $account;
$this->balance = $this->balance()->add($money);
}
}
5 changes: 5 additions & 0 deletions src/MPWAR/Module/Economy/Domain/VirtualCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function value()
return $this->value;
}

public function equals(VirtualCurrency $other)
{
return $this->value === $other->value;
}

private function guard($value)
{
if (!in_array($value, self::$allowed)) {
Expand Down
15 changes: 15 additions & 0 deletions src/MPWAR/Module/Economy/Domain/VirtualMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MPWAR\Module\Economy\Domain;

use MPWAR\Module\Economy\Contract\Exception\VirtualMoneyNotValidException;
use MPWAR\Module\Economy\Contract\Exception\VirtualMoneyOperationWithDifferentCurrenciesNotAllowedException;

final class VirtualMoney
{
Expand All @@ -22,10 +23,24 @@ public static function coins($amount)
return new self($amount, VirtualCurrency::coin());
}

public function add(VirtualMoney $other)
{
$this->guardCurrency($other->currency);

return new self($this->amount + $other->amount, $this->currency);
}

private function guard($amount)
{
if (!is_int($amount) || 0 > $amount) {
throw new VirtualMoneyNotValidException($amount);
}
}

private function guardCurrency(VirtualCurrency $other)
{
if (!$this->currency->equals($other)) {
throw new VirtualMoneyOperationWithDifferentCurrenciesNotAllowedException();
}
}
}