1 | <?php |
||||
2 | |||||
3 | namespace Siak\Tontine\Service\Planning; |
||||
4 | |||||
5 | use Illuminate\Database\Eloquent\Builder; |
||||
6 | use Illuminate\Database\Eloquent\Relations\Relation; |
||||
7 | use Illuminate\Support\Collection; |
||||
8 | use Siak\Tontine\Model\Fund; |
||||
9 | use Siak\Tontine\Model\FundDef; |
||||
10 | use Siak\Tontine\Model\Round; |
||||
11 | use Siak\Tontine\Service\TenantService; |
||||
12 | |||||
13 | class FundService |
||||
14 | { |
||||
15 | use SessionTrait; |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
16 | |||||
17 | /** |
||||
18 | * @param TenantService $tenantService |
||||
19 | */ |
||||
20 | public function __construct(protected TenantService $tenantService) |
||||
21 | {} |
||||
22 | |||||
23 | /** |
||||
24 | * @param Round $round |
||||
25 | * @param bool $filter|null |
||||
26 | * |
||||
27 | * @return Relation |
||||
28 | */ |
||||
29 | private function getQuery(Round $round, ?bool $filter): Relation |
||||
30 | { |
||||
31 | $onRoundFilter = fn(Builder|Relation $q) => $q->ofRound($round); |
||||
32 | return $round->guild->funds() |
||||
33 | ->when($filter === true, fn(Builder|Relation $query) => $query |
||||
34 | ->whereHas('funds', $onRoundFilter)) |
||||
35 | ->when($filter === false, fn(Builder|Relation $query) => $query |
||||
36 | ->whereDoesntHave('funds', $onRoundFilter)) |
||||
37 | ->when(!$round->add_default_fund, fn($query) => $query->user()); |
||||
0 ignored issues
–
show
|
|||||
38 | } |
||||
39 | |||||
40 | /** |
||||
41 | * Get a paginated list of funds. |
||||
42 | * |
||||
43 | * @param Round $round |
||||
44 | * @param bool $filter|null |
||||
45 | * @param int $page |
||||
46 | * |
||||
47 | * @return Collection |
||||
48 | */ |
||||
49 | public function getFundDefs(Round $round, ?bool $filter, int $page = 0): Collection |
||||
50 | { |
||||
51 | return $this->getQuery($round, $filter) |
||||
52 | ->with([ |
||||
53 | 'funds' => fn(Builder|Relation $q) => $q->where('round_id', $round->id), |
||||
54 | ]) |
||||
55 | ->withCount([ |
||||
56 | 'funds as funds_in_round_count' => fn(Builder|Relation $q) => $q->ofRound($round), |
||||
57 | ]) |
||||
58 | ->page($page, $this->tenantService->getLimit()) |
||||
59 | ->get(); |
||||
60 | } |
||||
61 | |||||
62 | /** |
||||
63 | * Get the number of funds. |
||||
64 | * |
||||
65 | * @param Round $round |
||||
66 | * @param bool $filter|null |
||||
67 | * |
||||
68 | * @return int |
||||
69 | */ |
||||
70 | public function getFundDefCount(Round $round, ?bool $filter): int |
||||
71 | { |
||||
72 | return $this->getQuery($round, $filter)->count(); |
||||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * Get a fund. |
||||
77 | * |
||||
78 | * @param Round $round |
||||
79 | * @param int $fundId |
||||
80 | * |
||||
81 | * @return Fund|null |
||||
82 | */ |
||||
83 | public function getFund(Round $round, int $fundId): ?Fund |
||||
84 | { |
||||
85 | return Fund::ofRound($round)->with(['sessions'])->find($fundId); |
||||
86 | } |
||||
87 | |||||
88 | /** |
||||
89 | * @param Round $round |
||||
90 | * @param int $defId |
||||
91 | * |
||||
92 | * @return FundDef|null |
||||
93 | */ |
||||
94 | public function getFundDef(Round $round, int $defId): ?FundDef |
||||
95 | { |
||||
96 | return $round->guild |
||||
97 | ->funds() |
||||
98 | ->user() |
||||
99 | ->withCount([ |
||||
100 | 'funds' => fn(Builder|Relation $q) => $q->where('round_id', $round->id), |
||||
101 | ]) |
||||
102 | ->find($defId); |
||||
103 | } |
||||
104 | |||||
105 | /** |
||||
106 | * @param Round $round |
||||
107 | * @param int $defId |
||||
108 | * |
||||
109 | * @return void |
||||
110 | */ |
||||
111 | public function enableFund(Round $round, int $defId): void |
||||
112 | { |
||||
113 | $def = $this->getFundDef($round, $defId); |
||||
114 | if(!$def || $def->funds_count > 0) |
||||
0 ignored issues
–
show
|
|||||
115 | { |
||||
116 | return; // Todo: throw an exception |
||||
117 | } |
||||
118 | |||||
119 | $itemQuery = Fund::withoutGlobalScopes()->where('def_id', $defId); |
||||
120 | $startSession = $this->getStartSession($round, $itemQuery); |
||||
0 ignored issues
–
show
It seems like
$itemQuery can also be of type Illuminate\Database\Eloq...gHasThroughRelationship ; however, parameter $itemQuery of Siak\Tontine\Service\Pla...vice::getStartSession() does only seem to accept Illuminate\Database\Eloquent\Builder , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
121 | $endSession = $this->getEndSession($round, $itemQuery); |
||||
0 ignored issues
–
show
It seems like
$itemQuery can also be of type Illuminate\Database\Eloq...gHasThroughRelationship ; however, parameter $itemQuery of Siak\Tontine\Service\Pla...ervice::getEndSession() does only seem to accept Illuminate\Database\Eloquent\Builder , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
122 | if($endSession->day_date <= $startSession->day_date) |
||||
123 | { |
||||
124 | return; // Todo: throw an exception |
||||
125 | } |
||||
126 | |||||
127 | // Create the fund |
||||
128 | $def->funds()->create([ |
||||
129 | 'round_id' => $round->id, |
||||
130 | 'start_sid' => $startSession->id, |
||||
131 | 'end_sid' => $endSession->id, |
||||
132 | 'interest_sid' => $endSession->id, |
||||
133 | ]); |
||||
134 | } |
||||
135 | |||||
136 | /** |
||||
137 | * @param Round $round |
||||
138 | * @param int $defId |
||||
139 | * |
||||
140 | * @return void |
||||
141 | */ |
||||
142 | public function disableFund(Round $round, int $defId): void |
||||
143 | { |
||||
144 | $def = $this->getFundDef($round, $defId); |
||||
145 | if(!$def || $def->funds_count === 0) |
||||
0 ignored issues
–
show
|
|||||
146 | { |
||||
147 | return; |
||||
148 | } |
||||
149 | |||||
150 | // Delete the fund |
||||
151 | $def->funds()->where('round_id', $round->id)->delete(); |
||||
152 | } |
||||
153 | |||||
154 | /** |
||||
155 | * Save the fund sessions. |
||||
156 | * |
||||
157 | * @param Fund $fund |
||||
158 | * @param array $values |
||||
159 | * |
||||
160 | * @return void |
||||
161 | */ |
||||
162 | public function saveSessions(Fund $fund, array $values) |
||||
163 | { |
||||
164 | $fund->update($values); |
||||
165 | } |
||||
166 | |||||
167 | /** |
||||
168 | * Get the number of active funds in the round. |
||||
169 | * |
||||
170 | * @param Round $round |
||||
171 | * |
||||
172 | * @return int |
||||
173 | */ |
||||
174 | public function getFundCount(Round $round): int |
||||
175 | { |
||||
176 | return $round->funds()->real()->user()->count(); |
||||
177 | } |
||||
178 | } |
||||
179 |