lagdo /
tontine
| 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
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 createDefaultFund(Round $round): void |
||||
| 40 | { |
||||
| 41 | $defaultFund = $round->guild->default_fund; |
||||
| 42 | if(!$defaultFund || !$round->add_default_fund || |
||||
|
0 ignored issues
–
show
|
|||||
| 43 | $defaultFund->funds()->where('round_id', $round->id)->exists()) |
||||
| 44 | { |
||||
| 45 | return; |
||||
| 46 | } |
||||
| 47 | |||||
| 48 | Fund::create([ |
||||
| 49 | 'def_id' => $defaultFund->id, |
||||
| 50 | 'round_id' => $round->id, |
||||
| 51 | 'start_sid' => $round->start->id, |
||||
|
0 ignored issues
–
show
|
|||||
| 52 | 'end_sid' => $round->end->id, |
||||
|
0 ignored issues
–
show
|
|||||
| 53 | 'interest_sid' => $round->end->id, |
||||
| 54 | ]); |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * @param Round $round |
||||
| 59 | * |
||||
| 60 | * @return void |
||||
| 61 | */ |
||||
| 62 | private function updateFunds(Round $round): void |
||||
| 63 | { |
||||
| 64 | if(!$round->start || !$round->end) |
||||
|
0 ignored issues
–
show
|
|||||
| 65 | { |
||||
| 66 | return; |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | // Create the fund to be used to lend the money in the pools. |
||||
| 70 | $round->pools() |
||||
| 71 | ->whereHas('def', fn($q) => $q->depositLendable()) |
||||
| 72 | ->get() |
||||
| 73 | ->each(fn($pool) => $this->savePoolFund($round, $pool)); |
||||
| 74 | |||||
| 75 | // Create the default savings fund. |
||||
| 76 | $this->createDefaultFund($round); |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | /** |
||||
| 80 | * @param Round $round |
||||
| 81 | * @param Collection|array $sessions |
||||
| 82 | * |
||||
| 83 | * @return void |
||||
| 84 | */ |
||||
| 85 | 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
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...
|
|||||
| 86 | { |
||||
| 87 | $this->updateFunds($round); |
||||
| 88 | |||||
| 89 | // Update the start and end sessions. |
||||
| 90 | $round->funds()->update([ |
||||
| 91 | 'start_sid' => $round->start->id, |
||||
|
0 ignored issues
–
show
|
|||||
| 92 | 'end_sid' => $round->end->id, |
||||
|
0 ignored issues
–
show
|
|||||
| 93 | 'interest_sid' => $round->end->id, |
||||
| 94 | ]); |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | /** |
||||
| 98 | * @param Round $round |
||||
| 99 | * |
||||
| 100 | * @return void |
||||
| 101 | */ |
||||
| 102 | 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
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...
|
|||||
| 103 | { |
||||
| 104 | // Not necessary |
||||
| 105 | // $this->updateFunds($round); |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * @param Session $session |
||||
| 110 | * |
||||
| 111 | * @return void |
||||
| 112 | */ |
||||
| 113 | public function sessionDeleted(Session $session): void |
||||
| 114 | { |
||||
| 115 | $round = $session->round; |
||||
| 116 | if($round->sessions()->count() === 1) |
||||
| 117 | { |
||||
| 118 | // The last session is being deleted. |
||||
| 119 | $round->funds()->delete(); |
||||
| 120 | return; |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | // Update the start sessions. |
||||
| 124 | $nextSession = $this->getNextSession($round, $session); |
||||
| 125 | if($nextSession !== null) |
||||
| 126 | { |
||||
| 127 | $round->funds() |
||||
| 128 | ->where('start_sid', $session->id) |
||||
| 129 | ->update(['start_sid' => $nextSession->id]); |
||||
| 130 | } |
||||
| 131 | |||||
| 132 | // Update the end sessions. |
||||
| 133 | $prevSession = $this->getPrevSession($round, $session); |
||||
| 134 | if($prevSession !== null) |
||||
| 135 | { |
||||
| 136 | $round->funds() |
||||
| 137 | ->where('end_sid', $session->id) |
||||
| 138 | ->update(['end_sid' => $prevSession->id]); |
||||
| 139 | |||||
| 140 | $round->funds() |
||||
| 141 | ->where('interest_sid', $session->id) |
||||
| 142 | ->update(['interest_sid' => $prevSession->id]); |
||||
| 143 | } |
||||
| 144 | } |
||||
| 145 | |||||
| 146 | /** |
||||
| 147 | * @param Round $round |
||||
| 148 | * @param Pool $pool |
||||
| 149 | * |
||||
| 150 | * @return void |
||||
| 151 | */ |
||||
| 152 | public function poolEnabled(Round $round, Pool $pool): void |
||||
| 153 | { |
||||
| 154 | if($pool->deposit_lendable) |
||||
| 155 | { |
||||
| 156 | // Create the fund to be used to lend the money in the pool. |
||||
| 157 | $this->savePoolFund($round, $pool); |
||||
| 158 | } |
||||
| 159 | } |
||||
| 160 | |||||
| 161 | /** |
||||
| 162 | * @param Round $round |
||||
| 163 | * @param Pool $pool |
||||
| 164 | * |
||||
| 165 | * @return void |
||||
| 166 | */ |
||||
| 167 | public function poolDisabled(Round $round, Pool $pool): void |
||||
| 168 | { |
||||
| 169 | if($pool->deposit_lendable) |
||||
| 170 | { |
||||
| 171 | // Delete the fund to be used to lend the money in the pool. |
||||
| 172 | $pool->fund()->where('round_id', $round->id)->delete(); |
||||
| 173 | } |
||||
| 174 | } |
||||
| 175 | |||||
| 176 | /** |
||||
| 177 | * @param Round $round |
||||
| 178 | * |
||||
| 179 | * @return void |
||||
| 180 | */ |
||||
| 181 | public function roundDeleted(Round $round): void |
||||
| 182 | { |
||||
| 183 | // Delete the funds (default and for pools) that was automatically created. |
||||
| 184 | $round->guild->default_fund |
||||
| 185 | ->funds() |
||||
| 186 | ->where('round_id', $round->id) |
||||
| 187 | ->delete(); |
||||
| 188 | } |
||||
| 189 | } |
||||
| 190 |