1313use Bavix \Wallet \Internal \Exceptions \RecordNotFoundException ;
1414use Bavix \Wallet \Internal \Exceptions \TransactionFailedException ;
1515use Bavix \Wallet \Internal \Service \DatabaseServiceInterface ;
16+ use Bavix \Wallet \Internal \Service \MathServiceInterface ;
1617use Bavix \Wallet \Models \Transaction ;
1718use Bavix \Wallet \Models \Transfer ;
1819use Bavix \Wallet \Models \Wallet as WalletModel ;
@@ -26,25 +27,31 @@ final class CommonServiceLegacy
2627 private DatabaseServiceInterface $ databaseService ;
2728 private AssistantServiceInterface $ assistantService ;
2829 private PrepareServiceInterface $ prepareService ;
29- private BookkeeperServiceInterface $ bookkeeper ;
30+ private RegulatorServiceInterface $ regulatorService ;
3031 private TransferDtoAssemblerInterface $ transferDtoAssembler ;
32+ private StateServiceInterface $ stateService ;
33+ private MathServiceInterface $ mathService ;
3134
3235 public function __construct (
3336 CastServiceInterface $ castService ,
34- BookkeeperServiceInterface $ bookkeeper ,
3537 AssistantServiceInterface $ satisfyService ,
3638 DatabaseServiceInterface $ databaseService ,
3739 PrepareServiceInterface $ prepareService ,
3840 TransferDtoAssemblerInterface $ transferDtoAssembler ,
39- AtmServiceInterface $ atmService
41+ RegulatorServiceInterface $ regulatorService ,
42+ StateServiceInterface $ stateService ,
43+ AtmServiceInterface $ atmService ,
44+ MathServiceInterface $ mathService
4045 ) {
4146 $ this ->atmService = $ atmService ;
4247 $ this ->castService = $ castService ;
43- $ this ->bookkeeper = $ bookkeeper ;
4448 $ this ->assistantService = $ satisfyService ;
4549 $ this ->databaseService = $ databaseService ;
4650 $ this ->prepareService = $ prepareService ;
51+ $ this ->regulatorService = $ regulatorService ;
4752 $ this ->transferDtoAssembler = $ transferDtoAssembler ;
53+ $ this ->stateService = $ stateService ;
54+ $ this ->mathService = $ mathService ;
4855 }
4956
5057 /**
@@ -131,7 +138,8 @@ public function addBalance(Wallet $wallet, $amount): bool
131138 return $ this ->databaseService ->transaction (function () use ($ wallet , $ amount ) {
132139 /** @var WalletModel $wallet */
133140 $ walletObject = $ this ->castService ->getWallet ($ wallet );
134- $ balance = $ this ->bookkeeper ->increase ($ walletObject , $ amount );
141+ $ balance = $ this ->regulatorService ->increase ($ walletObject , $ amount );
142+ $ this ->stateService ->persist ($ wallet );
135143 $ result = 0 ;
136144
137145 try {
@@ -143,7 +151,7 @@ public function addBalance(Wallet $wallet, $amount): bool
143151 $ walletObject ->fill (['balance ' => $ balance ])->syncOriginalAttribute ('balance ' );
144152 } finally {
145153 if ($ result === 0 ) {
146- $ this ->bookkeeper -> missing ($ walletObject );
154+ $ this ->regulatorService -> increase ($ walletObject, $ this -> mathService -> negative ( $ amount ) );
147155 }
148156 }
149157
@@ -196,7 +204,9 @@ public function applyTransactions(array $wallets, array $objects): array
196204 $ object = $ this ->castService ->getWallet ($ wallet );
197205 assert ((int ) $ object ->getKey () === $ walletId );
198206
199- $ balance = $ this ->bookkeeper ->increase ($ object , $ total );
207+ $ object ->getBalanceAttribute ();
208+ $ balance = $ this ->regulatorService ->increase ($ object , $ total );
209+ $ this ->stateService ->persist ($ object );
200210
201211 $ object ->newQuery ()->whereKey ($ object ->getKey ())->update (['balance ' => $ balance ]); // ?qN
202212 $ object ->fill (['balance ' => $ balance ])->syncOriginalAttribute ('balance ' );
0 commit comments