From b249c1cda9dcb37b0952e7391901dee250a78136 Mon Sep 17 00:00:00 2001 From: MannikJ Date: Thu, 6 Jun 2019 12:33:56 +0200 Subject: [PATCH] Configure and use facade and service; add unbiased --- composer.json | 2 +- config/config.php | 21 +++++++++++----- src/Jobs/RecalculateWalletBalance.php | 3 ++- src/Models/Transaction.php | 20 +++++++-------- src/Models/Wallet.php | 13 +++------- src/Observers/TransactionObserver.php | 3 --- src/Observers/WalletObserver.php | 11 ++++---- src/Services/Wallet.php | 36 +++++++++++++++++++++++++++ src/WalletServiceProvider.php | 7 ++++-- tests/TestCase.php | 8 ++++++ 10 files changed, 86 insertions(+), 38 deletions(-) create mode 100644 src/Services/Wallet.php diff --git a/composer.json b/composer.json index 7114773..ebe7c5c 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,7 @@ "MannikJ\\Laravel\\Wallet\\WalletServiceProvider" ], "aliases": { - "Wallet": "MannikJ\\Laravel\\Wallet\\WalletFacade" + "Wallet": "MannikJ\\Laravel\\Wallet\\Facades\\WalletFacade" } } }, diff --git a/config/config.php b/config/config.php index 08b2000..621489c 100644 --- a/config/config.php +++ b/config/config.php @@ -3,11 +3,11 @@ use MannikJ\Laravel\Wallet\Models\Transaction; return [ -/** - * Disable auto-loading of the vendor migrations - * You can then publish the migrations and - * change them for more flexibility - */ + /** + * Disable auto-loading of the vendor migrations + * You can then publish the migrations and + * change them for more flexibility + */ 'load_migrations' => env('WALLET_LOAD_MIGRATIONS', true), /** * Change this to specify the money amount column types @@ -43,4 +43,13 @@ * All amounts will be converted to a negative value */ 'subtracting_transaction_types' => explode(',', env('WALLET_SUBTRACTING_TRANSACTION_TYPES', 'withdraw,payout')), -]; \ No newline at end of file + + /** + * Transaction types that can be positive or negative + * Per default all types that are not explicitly specified + * as positive or negative are treated as unbiased types. + * It still might be convenient to specify your the available + * unbiased types here explicitly so you can show the options + */ + 'unbiased_transaction_types' => explode(',', env('WALLET_UNBIASED_TRANSACTION_TYPES', '')), +]; diff --git a/src/Jobs/RecalculateWalletBalance.php b/src/Jobs/RecalculateWalletBalance.php index 79a01f8..fda8ffc 100644 --- a/src/Jobs/RecalculateWalletBalance.php +++ b/src/Jobs/RecalculateWalletBalance.php @@ -17,6 +17,7 @@ class RecalculateWalletBalance implements ShouldQueue use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $wallet; + const CACHE_PREFIX = 'recalculate:wallet:'; @@ -52,4 +53,4 @@ public static function buildCacheKey(Wallet $wallet = id, $name = null) $base .= $wallet ? $wallet->id : ''; return $base .= $name ? ':' . $name : ''; } -} \ No newline at end of file +} diff --git a/src/Models/Transaction.php b/src/Models/Transaction.php index 3deefe0..79fa6f6 100644 --- a/src/Models/Transaction.php +++ b/src/Models/Transaction.php @@ -101,8 +101,8 @@ public function setAmountAttribute($amount) public function getAmountWithSign($amount = null, $type = null) { - $amount = $amount ? : array_get($this->attributes, 'amount'); - $type = $type ? : $this->type; + $amount = $amount ?: array_get($this->attributes, 'amount'); + $type = $type ?: $this->type; $amount = $this->shouldConvertToAbsoluteAmount() ? abs($amount) : $amount; if (in_array($type, config('wallet.subtracting_transaction_types', []))) { return $amount * -1; @@ -112,9 +112,9 @@ public function getAmountWithSign($amount = null, $type = null) public function shouldConvertToAbsoluteAmount($type = null) { - $type = $type ? : $this->type; - return in_array($type, config('wallet.subtracting_transaction_types', [])) || - in_array($type, config('wallet.adding_transaction_types', [])); + $type = $type ?: $this->type; + return in_array($type, \Wallet::subtractingTransactionTypes()) || + in_array($type, \Wallet::addingTransactionTypes()); } public function getTotalAmount() @@ -128,18 +128,18 @@ public function getTotalAmount() public static function getSignedAmountRawSql($table = null) { - $table = $table ? : (new static())->getTable(); + $table = $table ?: (new static())->getTable(); $subtractingTypes = implode(',', array_map( function ($type) { return "'{$type}'"; }, - config('wallet.subtracting_transaction_types') + \Wallet::subtractingTransactionTypes() )); $addingTypes = implode(',', array_map( function ($type) { return "'{$type}'"; }, - config('wallet.adding_transaction_types') + \Wallet::addingTransactionTypes() )); return "CASE WHEN {$table}.type @@ -191,6 +191,4 @@ public function getTotalAmountAttribute() $totalAmount = array_get($this->attributes, 'total_amount', $this->getTotalAmount()); return $totalAmount; } - - -} \ No newline at end of file +} diff --git a/src/Models/Wallet.php b/src/Models/Wallet.php index 9d04bee..0cf05fb 100644 --- a/src/Models/Wallet.php +++ b/src/Models/Wallet.php @@ -5,7 +5,6 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Carbon; -use Exception; use MannikJ\Laravel\Wallet\Exceptions\UnacceptedTransactionException; class Wallet extends Model @@ -164,17 +163,14 @@ public function setBalance($amount, $comment = 'Manual offset transaction') public function actualBalance(bool $save = false) { $undefined = $this->transactions() - ->whereNotIn('type', array_merge( - config('wallet.adding_transaction_types'), - config('wallet.subtracting_transaction_types') - )) + ->whereNotIn('type', \Wallet::biasedTransactionTypes()) ->sum('amount'); $credits = $this->transactions() - ->whereIn('type', config('wallet.adding_transaction_types')) + ->whereIn('type', \Wallet::addingTransactionTypes()) ->sum(\DB::raw('abs(amount)')); $debits = $this->transactions() - ->whereIn('type', config('wallet.subtracting_transaction_types')) + ->whereIn('type', \Wallet::subtractingTransactionTypes()) ->sum(\DB::raw('abs(amount)')); $balance = $undefined + $credits - $debits; @@ -184,5 +180,4 @@ public function actualBalance(bool $save = false) } return $balance; } - -} \ No newline at end of file +} diff --git a/src/Observers/TransactionObserver.php b/src/Observers/TransactionObserver.php index c0ca0c0..ac06fb1 100644 --- a/src/Observers/TransactionObserver.php +++ b/src/Observers/TransactionObserver.php @@ -2,9 +2,6 @@ namespace MannikJ\Laravel\Wallet\Observers; -use MannikJ\Laravel\Wallet\Models\Wallet; -use MannikJ\Laravel\Wallet\Models\Transaction; - class TransactionObserver { public function creating($transaction) diff --git a/src/Observers/WalletObserver.php b/src/Observers/WalletObserver.php index f186423..a05fbb5 100644 --- a/src/Observers/WalletObserver.php +++ b/src/Observers/WalletObserver.php @@ -10,10 +10,11 @@ class WalletObserver { public function saved(Wallet $wallet) { - if ($wallet->getOriginal('balance') != $wallet->balance - && config('auto_recalculate_balance', false)) { - $job = new RecalculateWalletBalance($wallet); - dispatch($job); + if ( + $wallet->isDirty('balance') + && \Wallet::autoRecalculationActive() + ) { + RecalculateWalletBalance::dispatch($wallet); } } -} \ No newline at end of file +} diff --git a/src/Services/Wallet.php b/src/Services/Wallet.php new file mode 100644 index 0000000..635484b --- /dev/null +++ b/src/Services/Wallet.php @@ -0,0 +1,36 @@ +addingTransactionTypes(), $this->subtractingTransactionTypes()); + } + + public function transactionTypes() + { + return array_merge($this->biasedTransactionTypes(), $this->unbiasedTransactionTypes()); + } + + public function autoRecalculationActive() + { + return config('auto_recalculate_balance', false); + } +} diff --git a/src/WalletServiceProvider.php b/src/WalletServiceProvider.php index 37b1612..f73ce21 100644 --- a/src/WalletServiceProvider.php +++ b/src/WalletServiceProvider.php @@ -3,10 +3,9 @@ namespace MannikJ\Laravel\Wallet; use Illuminate\Support\ServiceProvider; -use Illuminate\Database\Eloquent\Factory; -use Illuminate\Support\Carbon; use MannikJ\Laravel\Wallet\Observers\WalletObserver; use MannikJ\Laravel\Wallet\Observers\TransactionObserver; +use MannikJ\Laravel\Wallet\Services\Wallet; class WalletServiceProvider extends ServiceProvider { @@ -37,5 +36,9 @@ public function boot() public function register() { $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'wallet'); + + $this->app->singleton('wallet', function () { + return new Wallet; + }); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 93f9706..2ea9e9d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,6 +4,7 @@ use Orchestra\Testbench\TestCase as BaseTestCase; use MannikJ\Laravel\Wallet\WalletServiceProvider; +use MannikJ\Laravel\Wallet\Facades\WalletFacade; class TestCase extends BaseTestCase { @@ -24,4 +25,11 @@ protected function getPackageProviders($app) WalletServiceProvider::class ]; } + + protected function getPackageAliases() + { + return [ + 'Wallet' => WalletFacade::class + ]; + } }