Test Failed
Push — master ( 72daf6...986bed )
by Бабичев
03:30
created

RefreshBalance   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 39
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 19 1
1
<?php
2
3
namespace Bavix\Commands;
4
5
use Illuminate\Database\Query\JoinClause;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Console\Command;
8
use function config;
9
10
class RefreshBalance extends Command
11
{
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'wallet:refresh';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Recalculates all wallets';
26
27
    /**
28
     * @return void
29
     */
30
    public function handle(): void
31
    {
32
        DB::transaction(function () {
33
            $wallet = config('wallet.wallet.table');
34
            $trans = config('wallet.transaction.table');
35
36
            $availableBalance = DB::table($trans)
37
                ->select('wallet_id', DB::raw('sum(amount) balance'))
38
                ->where('confirmed', true)
39
                ->groupBy('wallet_id');
40
41
            $joinClause = function (JoinClause $join) use ($wallet) {
42
                $join->on("$wallet.id", '=', 'b.wallet_id');
43
            };
44
45
            DB::table($wallet)->update(['balance' => 0]);
46
            DB::table($wallet)
47
                ->joinSub($availableBalance, 'b', $joinClause, null, null, 'left')
48
                ->update(['balance' => DB::raw('b.balance')]);
49
        });
50
    }
51
52
}
53