55namespace Bavix \Wallet \Internal \Service ;
66
77use Bavix \Wallet \Internal \Exceptions \ExceptionInterface ;
8+ use Bavix \Wallet \Internal \Exceptions \LockProviderNotFoundException ;
89use Bavix \Wallet \Internal \Exceptions \RecordNotFoundException ;
910use Illuminate \Cache \CacheManager ;
1011use Illuminate \Config \Repository as ConfigRepository ;
1112use Illuminate \Contracts \Cache \Repository as CacheRepository ;
1213
1314final class StorageService implements StorageServiceInterface
1415{
16+ private LockServiceInterface $ lockService ;
17+ private MathServiceInterface $ mathService ;
1518 private CacheRepository $ cache ;
1619
17- private LockServiceInterface $ lock ;
18-
19- private MathServiceInterface $ math ;
20-
2120 public function __construct (
2221 CacheManager $ cacheManager ,
2322 ConfigRepository $ config ,
24- LockServiceInterface $ lock ,
25- MathServiceInterface $ math
23+ LockServiceInterface $ lockService ,
24+ MathServiceInterface $ mathService
2625 ) {
27- $ this ->math = $ math ;
28- $ this ->lock = $ lock ;
26+ $ this ->mathService = $ mathService ;
27+ $ this ->lockService = $ lockService ;
2928 $ this ->cache = $ cacheManager ->driver (
3029 $ config ->get ('wallet.cache.driver ' , 'array ' )
3130 );
@@ -41,6 +40,7 @@ public function missing(string $key): bool
4140 return $ this ->cache ->forget ($ key );
4241 }
4342
43+ /** @throws RecordNotFoundException */
4444 public function get (string $ key ): string
4545 {
4646 $ value = $ this ->cache ->get ($ key );
@@ -51,23 +51,29 @@ public function get(string $key): string
5151 );
5252 }
5353
54- return $ this ->math ->round ($ value );
54+ return $ this ->mathService ->round ($ value );
5555 }
5656
5757 public function sync (string $ key , $ value ): bool
5858 {
5959 return $ this ->cache ->set ($ key , $ value );
6060 }
6161
62+ /**
63+ * @param float|int|string $value
64+ *
65+ * @throws LockProviderNotFoundException
66+ * @throws RecordNotFoundException
67+ */
6268 public function increase (string $ key , $ value ): string
6369 {
64- return $ this ->lock ->block (
70+ return $ this ->lockService ->block (
6571 $ key .'::increase ' ,
6672 function () use ($ key , $ value ): string {
67- $ result = $ this ->math ->add ($ this ->get ($ key ), $ value );
73+ $ result = $ this ->mathService ->add ($ this ->get ($ key ), $ value );
6874 $ this ->sync ($ key , $ result );
6975
70- return $ this ->math ->round ($ result );
76+ return $ this ->mathService ->round ($ result );
7177 }
7278 );
7379 }
0 commit comments