Skip to content

Commit 4a379a4

Browse files
committed
transaction created event
1 parent 197ab38 commit 4a379a4

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

config/config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
use Bavix\Wallet\Internal\Assembler\BalanceUpdatedEventAssembler;
77
use Bavix\Wallet\Internal\Assembler\ExtraDtoAssembler;
88
use Bavix\Wallet\Internal\Assembler\OptionDtoAssembler;
9+
use Bavix\Wallet\Internal\Assembler\TransactionCreatedEventAssembler;
910
use Bavix\Wallet\Internal\Assembler\TransactionDtoAssembler;
1011
use Bavix\Wallet\Internal\Assembler\TransactionQueryAssembler;
1112
use Bavix\Wallet\Internal\Assembler\TransferDtoAssembler;
1213
use Bavix\Wallet\Internal\Assembler\TransferLazyDtoAssembler;
1314
use Bavix\Wallet\Internal\Assembler\TransferQueryAssembler;
1415
use Bavix\Wallet\Internal\Events\BalanceUpdatedEvent;
16+
use Bavix\Wallet\Internal\Events\TransactionCreatedEvent;
1517
use Bavix\Wallet\Internal\Events\WalletCreatedEvent;
1618
use Bavix\Wallet\Internal\Repository\TransactionRepository;
1719
use Bavix\Wallet\Internal\Repository\TransferRepository;
@@ -139,6 +141,7 @@
139141
'transaction' => TransactionDtoAssembler::class,
140142
'transfer_lazy' => TransferLazyDtoAssembler::class,
141143
'transfer' => TransferDtoAssembler::class,
144+
'transaction_created_event' => TransactionCreatedEventAssembler::class,
142145
'transaction_query' => TransactionQueryAssembler::class,
143146
'transfer_query' => TransferQueryAssembler::class,
144147
],
@@ -149,6 +152,7 @@
149152
'events' => [
150153
'balance_updated' => BalanceUpdatedEvent::class,
151154
'wallet_created' => WalletCreatedEvent::class,
155+
'transaction_created' => TransactionCreatedEvent::class,
152156
],
153157

154158
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Assembler;
6+
7+
use Bavix\Wallet\Internal\Events\TransactionCreatedEvent;
8+
use Bavix\Wallet\Internal\Events\TransactionCreatedEventInterface;
9+
use Bavix\Wallet\Internal\Service\ClockServiceInterface;
10+
use Bavix\Wallet\Models\Transaction;
11+
12+
final class TransactionCreatedEventAssembler implements TransactionCreatedEventAssemblerInterface
13+
{
14+
public function __construct(
15+
private ClockServiceInterface $clockService
16+
) {
17+
}
18+
19+
public function create(Transaction $transaction): TransactionCreatedEventInterface
20+
{
21+
return new TransactionCreatedEvent(
22+
(int) $transaction->getKey(),
23+
$transaction->type,
24+
$transaction->wallet_id,
25+
$this->clockService->now(),
26+
);
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Assembler;
6+
7+
use Bavix\Wallet\Internal\Events\TransactionCreatedEventInterface;
8+
use Bavix\Wallet\Models\Transaction;
9+
10+
interface TransactionCreatedEventAssemblerInterface
11+
{
12+
public function create(Transaction $transaction): TransactionCreatedEventInterface;
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Events;
6+
7+
use DateTimeImmutable;
8+
9+
final class TransactionCreatedEvent implements TransactionCreatedEventInterface
10+
{
11+
public function __construct(
12+
private int $id,
13+
private string $type,
14+
private int $walletId,
15+
private DateTimeImmutable $createdAt,
16+
) {
17+
}
18+
19+
public function getId(): int
20+
{
21+
return $this->id;
22+
}
23+
24+
public function getType(): string
25+
{
26+
return $this->type;
27+
}
28+
29+
public function getWalletId(): int
30+
{
31+
return $this->walletId;
32+
}
33+
34+
public function getCreatedAt(): DateTimeImmutable
35+
{
36+
return $this->createdAt;
37+
}
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Events;
6+
7+
use DateTimeImmutable;
8+
9+
interface TransactionCreatedEventInterface extends EventInterface
10+
{
11+
public function getId(): int;
12+
13+
public function getType(): string;
14+
15+
public function getWalletId(): int;
16+
17+
public function getCreatedAt(): DateTimeImmutable;
18+
}

src/Services/TransactionService.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
namespace Bavix\Wallet\Services;
66

77
use Bavix\Wallet\Interfaces\Wallet;
8+
use Bavix\Wallet\Internal\Assembler\TransactionCreatedEventAssemblerInterface;
89
use Bavix\Wallet\Internal\Dto\TransactionDtoInterface;
910
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
1011
use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException;
12+
use Bavix\Wallet\Internal\Service\DispatcherServiceInterface;
1113
use Bavix\Wallet\Models\Transaction;
1214

1315
final class TransactionService implements TransactionServiceInterface
1416
{
1517
public function __construct(
18+
private TransactionCreatedEventAssemblerInterface $transactionCreatedEventAssembler,
19+
private DispatcherServiceInterface $dispatcherService,
1620
private AssistantServiceInterface $assistantService,
1721
private RegulatorServiceInterface $regulatorService,
1822
private PrepareServiceInterface $prepareService,
@@ -69,6 +73,10 @@ public function apply(array $wallets, array $objects): array
6973
$this->regulatorService->increase($object, $total);
7074
}
7175

76+
foreach ($transactions as $transaction) {
77+
$this->dispatcherService->dispatch($this->transactionCreatedEventAssembler->create($transaction));
78+
}
79+
7280
return $transactions;
7381
}
7482
}

src/WalletServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Bavix\Wallet\Internal\Assembler\ExtraDtoAssemblerInterface;
1414
use Bavix\Wallet\Internal\Assembler\OptionDtoAssembler;
1515
use Bavix\Wallet\Internal\Assembler\OptionDtoAssemblerInterface;
16+
use Bavix\Wallet\Internal\Assembler\TransactionCreatedEventAssembler;
17+
use Bavix\Wallet\Internal\Assembler\TransactionCreatedEventAssemblerInterface;
1618
use Bavix\Wallet\Internal\Assembler\TransactionDtoAssembler;
1719
use Bavix\Wallet\Internal\Assembler\TransactionDtoAssemblerInterface;
1820
use Bavix\Wallet\Internal\Assembler\TransactionQueryAssembler;
@@ -27,6 +29,8 @@
2729
use Bavix\Wallet\Internal\Assembler\WalletCreatedEventAssemblerInterface;
2830
use Bavix\Wallet\Internal\Events\BalanceUpdatedEvent;
2931
use Bavix\Wallet\Internal\Events\BalanceUpdatedEventInterface;
32+
use Bavix\Wallet\Internal\Events\TransactionCreatedEvent;
33+
use Bavix\Wallet\Internal\Events\TransactionCreatedEventInterface;
3034
use Bavix\Wallet\Internal\Events\WalletCreatedEvent;
3135
use Bavix\Wallet\Internal\Events\WalletCreatedEventInterface;
3236
use Bavix\Wallet\Internal\Repository\TransactionRepository;
@@ -290,6 +294,11 @@ private function assemblers(array $configure): void
290294
WalletCreatedEventAssemblerInterface::class,
291295
$configure['wallet_created_event'] ?? WalletCreatedEventAssembler::class
292296
);
297+
298+
$this->app->singleton(
299+
TransactionCreatedEventAssemblerInterface::class,
300+
$configure['transaction_created_event'] ?? TransactionCreatedEventAssembler::class
301+
);
293302
}
294303

295304
private function transformers(array $configure): void
@@ -316,6 +325,11 @@ private function events(array $configure): void
316325
WalletCreatedEventInterface::class,
317326
$configure['wallet_created'] ?? WalletCreatedEvent::class
318327
);
328+
329+
$this->app->bind(
330+
TransactionCreatedEventInterface::class,
331+
$configure['transaction_created'] ?? TransactionCreatedEvent::class
332+
);
319333
}
320334

321335
private function bindObjects(array $configure): void

0 commit comments

Comments
 (0)