Passed
Push — main ( 1c458a...5a987a )
by Thierry
10:56 queued 05:26
created

FundSyncService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 148
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A sessionUpdated() 0 2 1
A updateFunds() 0 15 3
A roundDeleted() 0 7 1
A updateDefaultFund() 0 16 2
A sessionCreated() 0 9 1
A sessionDeleted() 0 30 4
A savePoolFund() 0 10 1
1
<?php
2
3
namespace Siak\Tontine\Service\Planning;
4
5
use Illuminate\Support\Collection;
6
use Siak\Tontine\Model\Fund;
7
use Siak\Tontine\Model\Pool;
8
use Siak\Tontine\Model\Round;
9
use Siak\Tontine\Model\Session;
10
11
class FundSyncService
12
{
13
    use SessionSyncTrait;
0 ignored issues
show
Bug introduced by
The trait Siak\Tontine\Service\Planning\SessionSyncTrait requires the property $day_date which is not provided by Siak\Tontine\Service\Planning\FundSyncService.
Loading history...
14
15
    /**
16
     * @param Round $round
17
     * @param Pool $pool
18
     *
19
     * @return void
20
     */
21
    public function savePoolFund(Round $round, Pool $pool): void
22
    {
23
        Fund::updateOrCreate([
24
            'pool_id' => $pool->id,
25
        ], [
26
            'def_id' => $round->guild->default_fund->id,
27
            'round_id' => $round->id,
28
            'start_sid' => $round->start->id,
0 ignored issues
show
Bug introduced by
The property start does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
29
            'end_sid' => $round->end->id,
0 ignored issues
show
Bug introduced by
The property end does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
30
            'interest_sid' => $round->end->id,
31
        ]);
32
    }
33
34
    /**
35
     * @param Round $round
36
     *
37
     * @return void
38
     */
39
    private function updateDefaultFund(Round $round): void
40
    {
41
        $fundDef = $round->guild->default_fund;
42
        if(!$round->add_default_fund)
0 ignored issues
show
Bug introduced by
The property add_default_fund does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
43
        {
44
            $fundDef->funds()->real()->where('round_id', $round->id)->delete();
45
            return;
46
        }
47
48
        Fund::updateOrCreate([
49
            'def_id' => $fundDef->id,
50
            'round_id' => $round->id,
51
        ], [
52
            'start_sid' => $round->start->id,
0 ignored issues
show
Bug introduced by
The property start does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
53
            'end_sid' => $round->end->id,
0 ignored issues
show
Bug introduced by
The property end does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
54
            'interest_sid' => $round->end->id,
55
        ]);
56
    }
57
58
    /**
59
     * @param Round $round
60
     *
61
     * @return void
62
     */
63
    private function updateFunds(Round $round): void
64
    {
65
        if(!$round->start || !$round->end)
0 ignored issues
show
Bug introduced by
The property start does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property end does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
66
        {
67
            return;
68
        }
69
70
        // Create the fund that will be used to lend the money in the pools.
71
        Pool::ofRound($round)
72
            ->whereHas('def', fn($q) => $q->depositLendable())
73
            ->get()
74
            ->each(fn($pool) => $this->savePoolFund($round, $pool));
75
76
        // Create the default savings fund.
77
        $this->updateDefaultFund($round);
78
    }
79
80
    /**
81
     * @param Round $round
82
     * @param Collection|array $sessions
83
     *
84
     * @return void
85
     */
86
    public function sessionCreated(Round $round, Collection|array $sessions): void
0 ignored issues
show
Unused Code introduced by
The parameter $sessions is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
    public function sessionCreated(Round $round, /** @scrutinizer ignore-unused */ Collection|array $sessions): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        $this->updateFunds($round);
89
90
        // Update the start and end sessions.
91
        $round->funds()->update([
92
            'start_sid' => $round->start->id,
0 ignored issues
show
Bug introduced by
The property start does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
93
            'end_sid' => $round->end->id,
0 ignored issues
show
Bug introduced by
The property end does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
94
            'interest_sid' => $round->end->id,
95
        ]);
96
    }
97
98
    /**
99
     * @param Round $round
100
     *
101
     * @return void
102
     */
103
    public function sessionUpdated(Round $round): void
0 ignored issues
show
Unused Code introduced by
The parameter $round is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

103
    public function sessionUpdated(/** @scrutinizer ignore-unused */ Round $round): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        // Not necessary
106
        // $this->updateFunds($round);
107
    }
108
109
    /**
110
     * @param Session $session
111
     *
112
     * @return void
113
     */
114
    public function sessionDeleted(Session $session): void
115
    {
116
        $round = $session->round;
117
        if($round->sessions()->count() === 1)
118
        {
119
            // The last session is being deleted.
120
            $round->funds()->delete();
121
            return;
122
        }
123
124
        // Update the start sessions.
125
        $nextSession = $this->getNextSession($round, $session);
126
        if($nextSession !== null)
127
        {
128
            $round->funds()
129
                ->where('start_sid', $session->id)
130
                ->update(['start_sid' => $nextSession->id]);
131
        }
132
133
        // Update the end sessions.
134
        $prevSession = $this->getPrevSession($round, $session);
135
        if($prevSession !== null)
136
        {
137
            $round->funds()
138
                ->where('end_sid', $session->id)
139
                ->update(['end_sid' => $prevSession->id]);
140
141
            $round->funds()
142
                ->where('interest_sid', $session->id)
143
                ->update(['interest_sid' => $prevSession->id]);
144
        }
145
    }
146
147
    /**
148
     * @param Round $round
149
     *
150
     * @return void
151
     */
152
    public function roundDeleted(Round $round): void
153
    {
154
        // Delete the funds (default and for pools) that was automatically created.
155
        $round->guild->default_fund
156
            ->funds()
157
            ->where('round_id', $round->id)
158
            ->delete();
159
    }
160
}
161