Skip to content

Commit c7f2ad3

Browse files
committed
optimize code + getCurrencyAttribute
1 parent 90bec79 commit c7f2ad3

File tree

16 files changed

+97
-41
lines changed

16 files changed

+97
-41
lines changed

config/config.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,68 @@
11
<?php
22

3+
use Bavix\Wallet\Services\ExchangeService;
4+
use Bavix\Wallet\Services\CommonService;
5+
use Bavix\Wallet\Services\ProxyService;
6+
use Bavix\Wallet\Services\WalletService;
7+
use Bavix\Wallet\Models\Transaction;
8+
use Bavix\Wallet\Models\Transfer;
9+
use Bavix\Wallet\Models\Wallet;
10+
use Bavix\Wallet\Simple\Rate;
11+
312
return [
13+
/**
14+
* The parameter is used for fast packet overload.
15+
* You do not need to search for the desired class by code, the library will do it itself.
16+
*/
417
'package' => [
5-
'rateable' => \Bavix\Wallet\Simple\Rate::class,
18+
'rateable' => Rate::class,
619
],
20+
21+
/**
22+
* Sometimes a slug may not match the currency and you need the ability to add an exception.
23+
* The main thing is that there are not many exceptions)
24+
*
25+
* Syntax:
26+
* 'slug' => 'currency'
27+
*
28+
* @example
29+
* 'my-usd' => 'USD'
30+
*/
31+
'currencies' => [],
32+
33+
/**
34+
* Services are the main core of the library and sometimes they need to be improved.
35+
* This configuration will help you to quickly customize the library.
36+
*/
737
'services' => [
8-
'exchange' => \Bavix\Wallet\Services\ExchangeService::class,
9-
'common' => \Bavix\Wallet\Services\CommonService::class,
10-
'proxy' => \Bavix\Wallet\Services\ProxyService::class,
11-
'wallet' => \Bavix\Wallet\Services\WalletService::class,
38+
'exchange' => ExchangeService::class,
39+
'common' => CommonService::class,
40+
'proxy' => ProxyService::class,
41+
'wallet' => WalletService::class,
1242
],
43+
44+
/**
45+
* Transaction model configuration.
46+
*/
1347
'transaction' => [
1448
'table' => 'transactions',
15-
'model' => \Bavix\Wallet\Models\Transaction::class,
49+
'model' => Transaction::class,
1650
],
51+
52+
/**
53+
* Transfer model configuration.
54+
*/
1755
'transfer' => [
1856
'table' => 'transfers',
19-
'model' => \Bavix\Wallet\Models\Transfer::class,
57+
'model' => Transfer::class,
2058
],
59+
60+
/**
61+
* Wallet model configuration.
62+
*/
2163
'wallet' => [
2264
'table' => 'wallets',
23-
'model' => \Bavix\Wallet\Models\Wallet::class,
65+
'model' => Wallet::class,
2466
'default' => [
2567
'name' => 'Default Wallet',
2668
'slug' => 'default',

src/Commands/RefreshBalance.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Bavix\Wallet\Commands;
44

5+
use Illuminate\Console\Command;
56
use Illuminate\Database\Query\JoinClause;
67
use Illuminate\Support\Facades\DB;
7-
use Illuminate\Console\Command;
88
use function config;
99

1010
/**
@@ -34,7 +34,7 @@ class RefreshBalance extends Command
3434
*/
3535
public function handle(): void
3636
{
37-
DB::transaction(static function() {
37+
DB::transaction(static function () {
3838
$wallet = config('wallet.wallet.table');
3939
$trans = config('wallet.transaction.table');
4040

@@ -43,7 +43,7 @@ public function handle(): void
4343
->where('confirmed', true)
4444
->groupBy('wallet_id');
4545

46-
$joinClause = static function(JoinClause $join) use ($wallet) {
46+
$joinClause = static function (JoinClause $join) use ($wallet) {
4747
$join->on("$wallet.id", '=', 'b.wallet_id');
4848
};
4949

src/Models/Wallet.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @property int $balance
2626
* @property int $decimal_places
2727
* @property \Bavix\Wallet\Interfaces\Wallet $holder
28+
* @property-read string $currency
2829
*/
2930
class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchangeable
3031
{
@@ -111,4 +112,13 @@ public function holder(): MorphTo
111112
return $this->morphTo();
112113
}
113114

115+
/**
116+
* @return string
117+
*/
118+
public function getCurrencyAttribute(): string
119+
{
120+
$currencies = config('wallet.currencies', []);
121+
return $currencies[$this->slug] ?? Str::upper($this->slug);
122+
}
123+
114124
}

src/Objects/Cart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Bavix\Wallet\Interfaces\Product;
77
use Bavix\Wallet\Models\Transfer;
88
use Countable;
9+
use function array_unique;
910
use function count;
1011
use function get_class;
11-
use function array_unique;
1212

1313
class Cart implements Countable
1414
{

src/Services/CommonService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function multiOperation(Wallet $self, array $operations): array
173173
public function assemble(array $brings): array
174174
{
175175
$self = $this;
176-
return DB::transaction(static function() use ($self, $brings) {
176+
return DB::transaction(static function () use ($self, $brings) {
177177
return $self->multiBrings($brings);
178178
});
179179
}

src/Services/WalletService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function fee(Wallet $wallet, int $amount): int
3535
$result = 0;
3636

3737
if ($wallet instanceof Taxable) {
38-
$result = (int) ($amount * $wallet->getFeePercent() / 100);
38+
$result = (int)($amount * $wallet->getFeePercent() / 100);
3939

4040
/**
4141
* Added minimum commission condition
@@ -97,7 +97,7 @@ public function getBalance(Wallet $object): int
9797
$wallet = $this->getWallet($object);
9898
$proxy = app(ProxyService::class);
9999
if (!$proxy->has($wallet->getKey())) {
100-
$proxy->set($wallet->getKey(), (int) $wallet->getOriginal('balance', 0));
100+
$proxy->set($wallet->getKey(), (int)$wallet->getOriginal('balance', 0));
101101
}
102102

103103
return $proxy[$wallet->getKey()];

src/Traits/CanConfirm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait CanConfirm
2020
public function confirm(Transaction $transaction): bool
2121
{
2222
$self = $this;
23-
return DB::transaction(static function() use ($self, $transaction) {
23+
return DB::transaction(static function () use ($self, $transaction) {
2424
$wallet = app(WalletService::class)
2525
->getWallet($self);
2626

@@ -63,7 +63,7 @@ public function safeConfirm(Transaction $transaction): bool
6363
public function forceConfirm(Transaction $transaction): bool
6464
{
6565
$self = $this;
66-
return DB::transaction(static function() use ($self, $transaction) {
66+
return DB::transaction(static function () use ($self, $transaction) {
6767

6868
$wallet = app(WalletService::class)
6969
->getWallet($self);

src/Traits/CanExchange.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function forceExchange(Wallet $to, int $amount, ?array $meta = null): Tra
4949
*/
5050
$from = app(WalletService::class)->getWallet($this);
5151

52-
return DB::transaction(static function() use ($from, $to, $amount, $meta) {
52+
return DB::transaction(static function () use ($from, $to, $amount, $meta) {
5353
$rate = app(ExchangeService::class)->rate($from, $to);
5454
$fee = app(WalletService::class)->fee($to, $amount);
5555

src/Traits/CartPay.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function payFreeCart(Cart $cart): array
3232
->verifyWithdraw($this, 0);
3333

3434
$self = $this;
35-
return DB::transaction(static function() use ($self, $cart) {
35+
return DB::transaction(static function () use ($self, $cart) {
3636
$results = [];
3737
foreach ($cart->getItems() as $product) {
3838
$results[] = app(CommonService::class)->forceTransfer(
@@ -75,7 +75,7 @@ public function payCart(Cart $cart, bool $force = null): array
7575
}
7676

7777
$self = $this;
78-
return DB::transaction(static function() use ($self, $cart, $force) {
78+
return DB::transaction(static function () use ($self, $cart, $force) {
7979
$results = [];
8080
foreach ($cart->getItems() as $product) {
8181
if ($force) {
@@ -138,7 +138,7 @@ public function safeRefundCart(Cart $cart, bool $force = null, bool $gifts = nul
138138
public function refundCart(Cart $cart, bool $force = null, bool $gifts = null): bool
139139
{
140140
$self = $this;
141-
return DB::transaction(static function() use ($self, $cart, $force, $gifts) {
141+
return DB::transaction(static function () use ($self, $cart, $force, $gifts) {
142142
$results = [];
143143
$transfers = $cart->alreadyBuy($self, $gifts);
144144
if (count($transfers) !== count($cart)) {

src/Traits/HasGift.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Illuminate\Support\Facades\DB;
1212
use Throwable;
1313
use function app;
14-
use function get_class;
1514

1615
/**
1716
* Trait HasGift
@@ -62,7 +61,7 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer
6261
* I think it is wrong to make the "assemble" method public.
6362
* That's why I address him like this!
6463
*/
65-
return DB::transaction(static function() use ($santa, $to, $product, $force) {
64+
return DB::transaction(static function () use ($santa, $to, $product, $force) {
6665
$amount = $product->getAmountProduct();
6766
$meta = $product->getMetaProduct();
6867
$fee = app(WalletService::class)

0 commit comments

Comments
 (0)