Passed
Branch main (b90ec4)
by Thierry
20:23 queued 14:04
created

DeleteDefaultFunds   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 36
rs 10
c 1
b 0
f 0
wmc 2
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Siak\Tontine\Model\Fund;
8
use Siak\Tontine\Model\Loan;
9
use Siak\Tontine\Model\Saving;
10
11
class DeleteDefaultFunds extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'fund:delete-defaults';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Delete the default savings fund for all tontines';
26
27
    /**
28
     * Execute the console command.
29
     *
30
      * @return int
31
     */
32
    public function handle()
33
    {
34
        DB::transaction(function() {
35
            foreach(Fund::where('title', '')->cursor() as $fund)
36
            {
37
                // Update the savings and loans with the default fund id.
38
                Loan::where('fund_id', $fund->id)->update(['fund_id' => null]);
39
                Saving::where('fund_id', $fund->id)->update(['fund_id' => null]);
40
41
                // Delete the fund.
42
                $fund->delete();
43
            }
44
        });
45
46
        return Command::SUCCESS;
47
    }
48
}
49