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

CopyClosingsToTable::handle()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 1
nop 0
dl 0
loc 27
rs 9.5222
c 1
b 0
f 0
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\Closing;
8
use Siak\Tontine\Model\Tontine;
9
10
class CopyClosingsToTable extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'closing:copy-to-table';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Copy the closings from the tontine attributes to their own table';
25
26
    /**
27
     * Execute the console command.
28
     *
29
      * @return int
30
     */
31
    public function handle()
32
    {
33
        DB::transaction(function() {
34
            foreach(Tontine::cursor() as $tontine)
35
            {
36
                $closings = $tontine->properties['closings'] ?? [];
37
                foreach($closings as $sessionId => $funds)
38
                {
39
                    foreach($funds as $fundId => $profitAmount)
40
                    {
41
                        Closing::updateOrCreate([
42
                            'type' => Closing::TYPE_ROUND,
43
                            'session_id' => $sessionId,
44
                            'fund_id' => $fundId ?: $tontine->default_fund->id,
45
                        ], [
46
                            'options' => [
47
                                'profit' => [
48
                                    'amount' => $profitAmount,
49
                                ],
50
                            ],
51
                        ]);
52
                    }
53
                }
54
            }
55
        });
56
57
        return Command::SUCCESS;
58
    }
59
}
60