Skip to content

Commit 8a5b0d3

Browse files
committed
add ClockServiceInterface
1 parent ec5dc4a commit 8a5b0d3

File tree

12 files changed

+128
-5
lines changed

12 files changed

+128
-5
lines changed

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Bavix\Wallet\Internal\Events\BalanceUpdatedEvent;
1313
use Bavix\Wallet\Internal\Repository\TransactionRepository;
1414
use Bavix\Wallet\Internal\Repository\TransferRepository;
15+
use Bavix\Wallet\Internal\Service\ClockService;
1516
use Bavix\Wallet\Internal\Service\DatabaseService;
1617
use Bavix\Wallet\Internal\Service\DispatcherService;
1718
use Bavix\Wallet\Internal\Service\JsonService;
@@ -64,6 +65,7 @@
6465
* Internal services that can be overloaded.
6566
*/
6667
'internal' => [
68+
'clock' => ClockService::class,
6769
'database' => DatabaseService::class,
6870
'dispatcher' => DispatcherService::class,
6971
'json' => JsonService::class,

src/Internal/Assembler/BalanceUpdatedEventAssembler.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@
66

77
use Bavix\Wallet\Internal\Events\BalanceUpdatedEvent;
88
use Bavix\Wallet\Internal\Events\BalanceUpdatedEventInterface;
9+
use Bavix\Wallet\Internal\Service\ClockServiceInterface;
910
use Bavix\Wallet\Models\Wallet;
1011

1112
final class BalanceUpdatedEventAssembler implements BalanceUpdatedEventAssemblerInterface
1213
{
13-
public function create(Wallet $wallet, string $balance): BalanceUpdatedEventInterface
14+
private ClockServiceInterface $clockService;
15+
16+
public function __construct(ClockServiceInterface $clockService)
17+
{
18+
$this->clockService = $clockService;
19+
}
20+
21+
public function create(Wallet $wallet): BalanceUpdatedEventInterface
1422
{
15-
return new BalanceUpdatedEvent((int) $wallet->getKey(), $wallet->uuid, $balance);
23+
return new BalanceUpdatedEvent(
24+
(int) $wallet->getKey(),
25+
$wallet->uuid,
26+
$wallet->getOriginalBalanceAttribute(),
27+
$this->clockService->now()
28+
);
1629
}
1730
}

src/Internal/Assembler/BalanceUpdatedEventAssemblerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
interface BalanceUpdatedEventAssemblerInterface
1111
{
12-
public function create(Wallet $wallet, string $balance): BalanceUpdatedEventInterface;
12+
public function create(Wallet $wallet): BalanceUpdatedEventInterface;
1313
}

src/Internal/Events/BalanceUpdatedEvent.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44

55
namespace Bavix\Wallet\Internal\Events;
66

7+
use DateTimeImmutable;
8+
79
final class BalanceUpdatedEvent implements BalanceUpdatedEventInterface
810
{
911
private int $walletId;
1012
private string $walletUuid;
1113
private string $balance;
14+
private DateTimeImmutable $updatedAt;
1215

1316
public function __construct(
1417
int $walletId,
1518
string $walletUuid,
16-
string $balance
19+
string $balance,
20+
DateTimeImmutable $updatedAt
1721
) {
1822
$this->walletId = $walletId;
1923
$this->walletUuid = $walletUuid;
2024
$this->balance = $balance;
25+
$this->updatedAt = $updatedAt;
2126
}
2227

2328
public function getWalletId(): int
@@ -34,4 +39,9 @@ public function getBalance(): string
3439
{
3540
return $this->balance;
3641
}
42+
43+
public function getUpdatedAt(): DateTimeImmutable
44+
{
45+
return $this->updatedAt;
46+
}
3747
}

src/Internal/Events/BalanceUpdatedEventInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace Bavix\Wallet\Internal\Events;
66

7+
use DateTimeImmutable;
8+
79
interface BalanceUpdatedEventInterface extends EventInterface
810
{
911
public function getWalletId(): int;
1012

1113
public function getWalletUuid(): string;
1214

1315
public function getBalance(): string;
16+
17+
public function getUpdatedAt(): DateTimeImmutable;
1418
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Service;
6+
7+
use DateTimeImmutable;
8+
9+
final class ClockService implements ClockServiceInterface
10+
{
11+
public function now(): DateTimeImmutable
12+
{
13+
return new DateTimeImmutable();
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Service;
6+
7+
use DateTimeImmutable;
8+
9+
/**
10+
* @see https://github.com/php-fig/fig-standards/blob/master/proposed/clock.md
11+
*
12+
* @internal
13+
*/
14+
interface ClockServiceInterface
15+
{
16+
public function now(): DateTimeImmutable;
17+
}

src/Services/RegulatorService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function approve(): void
109109
$wallet->newQuery()->whereKey($wallet->getKey())->update(['balance' => $balance]); // ?qN
110110
$wallet->fill(['balance' => $balance])->syncOriginalAttribute('balance');
111111

112-
$event = $this->balanceUpdatedEventAssembler->create($wallet, $balance);
112+
$event = $this->balanceUpdatedEventAssembler->create($wallet);
113113
$this->dispatcherService->dispatch($event);
114114
}
115115

src/WalletServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Bavix\Wallet\Internal\Repository\TransactionRepositoryInterface;
2525
use Bavix\Wallet\Internal\Repository\TransferRepository;
2626
use Bavix\Wallet\Internal\Repository\TransferRepositoryInterface;
27+
use Bavix\Wallet\Internal\Service\ClockService;
28+
use Bavix\Wallet\Internal\Service\ClockServiceInterface;
2729
use Bavix\Wallet\Internal\Service\DatabaseService;
2830
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
2931
use Bavix\Wallet\Internal\Service\DispatcherService;
@@ -191,6 +193,7 @@ private function internal(array $configure): void
191193
{
192194
$this->app->bind(StorageServiceInterface::class, $configure['storage'] ?? StorageService::class);
193195

196+
$this->app->singleton(ClockServiceInterface::class, $configure['clock'] ?? ClockService::class);
194197
$this->app->singleton(DatabaseServiceInterface::class, $configure['database'] ?? DatabaseService::class);
195198
$this->app->singleton(DispatcherServiceInterface::class, $configure['dispatcher'] ?? DispatcherService::class);
196199
$this->app->singleton(JsonServiceInterface::class, $configure['json'] ?? JsonService::class);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Test\Infra\Listeners;
6+
7+
use Bavix\Wallet\Internal\Events\BalanceUpdatedEventInterface;
8+
use Bavix\Wallet\Internal\Exceptions\UnknownEventException;
9+
use DateTimeInterface;
10+
11+
final class BalanceUpdatedThrowDateListener
12+
{
13+
public function handle(BalanceUpdatedEventInterface $balanceChangedEvent): void
14+
{
15+
throw new UnknownEventException(
16+
$balanceChangedEvent->getUpdatedAt()->format(DateTimeInterface::ATOM),
17+
(int) $balanceChangedEvent->getBalance()
18+
);
19+
}
20+
}

0 commit comments

Comments
 (0)