Issues (214)

src/Service/Planning/FundSyncService.php (12 issues)

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
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
    private 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' => $pool->start_sid,
29
            'end_sid' => $pool->end_sid,
30
            'interest_sid' => $pool->end_sid,
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
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
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
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
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...
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 to be used to lend the money in the pools.
71
        $round->pools()
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
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
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
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
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
     * @param Pool $pool
150
     *
151
     * @return void
152
     */
153
    public function poolEnabled(Round $round, Pool $pool): void
154
    {
155
        if($pool->deposit_lendable)
0 ignored issues
show
The property deposit_lendable does not seem to exist on Siak\Tontine\Model\Pool. 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...
156
        {
157
            // Create the fund to be used to lend the money in the pool.
158
            $this->savePoolFund($round, $pool);
159
        }
160
    }
161
162
    /**
163
     * @param Round $round
164
     * @param Pool $pool
165
     *
166
     * @return void
167
     */
168
    public function poolDisabled(Round $round, Pool $pool): void
169
    {
170
        if($pool->deposit_lendable)
0 ignored issues
show
The property deposit_lendable does not seem to exist on Siak\Tontine\Model\Pool. 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...
171
        {
172
            // Delete the fund to be used to lend the money in the pool.
173
            $pool->fund()->where('round_id', $round->id)->delete();
174
        }
175
    }
176
177
    /**
178
     * @param Round $round
179
     *
180
     * @return void
181
     */
182
    public function roundDeleted(Round $round): void
183
    {
184
        // Delete the funds (default and for pools) that was automatically created.
185
        $round->guild->default_fund
186
            ->funds()
187
            ->where('round_id', $round->id)
188
            ->delete();
189
    }
190
}
191