1 | <?php |
||
2 | |||
3 | namespace Siak\Tontine\Service\Guild; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Builder; |
||
6 | use Illuminate\Database\Eloquent\Relations\Relation; |
||
7 | use Illuminate\Support\Collection; |
||
8 | use Illuminate\Support\Facades\DB; |
||
9 | use Siak\Tontine\Model\FundDef; |
||
10 | use Siak\Tontine\Model\Guild; |
||
11 | use Siak\Tontine\Model\User; |
||
12 | use Siak\Tontine\Service\TenantService; |
||
13 | |||
14 | use function config; |
||
15 | use function tap; |
||
16 | |||
17 | class GuildService |
||
18 | { |
||
19 | /** |
||
20 | * @param TenantService $tenantService |
||
21 | */ |
||
22 | public function __construct(protected TenantService $tenantService) |
||
23 | {} |
||
24 | |||
25 | /** |
||
26 | * Get a paginated list of guilds in the selected round. |
||
27 | * |
||
28 | * @param User $user |
||
29 | * @param int $page |
||
30 | * |
||
31 | * @return Collection |
||
32 | */ |
||
33 | public function getGuilds(User $user, int $page = 0): Collection |
||
34 | { |
||
35 | return $user->guilds() |
||
36 | ->page($page, $this->tenantService->getLimit()) |
||
37 | ->get(); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Get the number of guilds in the selected round. |
||
42 | * |
||
43 | * @param User $user |
||
44 | * |
||
45 | * @return int |
||
46 | */ |
||
47 | public function getGuildCount(User $user): int |
||
48 | { |
||
49 | return $user->guilds()->count(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Get a single guild. |
||
54 | * |
||
55 | * @param User $user |
||
56 | * @param int $guildId The guild id |
||
57 | * |
||
58 | * @return Guild|null |
||
59 | */ |
||
60 | public function getGuild(User $user, int $guildId): ?Guild |
||
61 | { |
||
62 | return $user->guilds()->find($guildId); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param User $user |
||
67 | * |
||
68 | * @return Builder|Relation |
||
69 | */ |
||
70 | public function getGuestGuildsQuery(User $user): Builder|Relation |
||
71 | { |
||
72 | return Guild::whereHas('invites', fn(Builder $query) => |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
73 | $query->where('guest_id', $user->id)); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Check if the user has guest guilds |
||
78 | * |
||
79 | * @param User $user |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function hasGuestGuilds(User $user): bool |
||
84 | { |
||
85 | return $this->getGuestGuildsQuery($user)->exists(); |
||
0 ignored issues
–
show
|
|||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get a paginated list of guest guilds. |
||
90 | * |
||
91 | * @param User $user |
||
92 | * @param int $page |
||
93 | * |
||
94 | * @return Collection |
||
95 | */ |
||
96 | public function getGuestGuilds(User $user, int $page = 0): Collection |
||
97 | { |
||
98 | return $this->getGuestGuildsQuery($user) |
||
99 | ->with(['user']) |
||
100 | ->orderBy('guilds.id') |
||
101 | ->page($page, $this->tenantService->getLimit()) |
||
102 | ->get() |
||
103 | ->each(fn($guild) => $guild->isGuest = true); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Get the number of guest guilds. |
||
108 | * |
||
109 | * @param User $user |
||
110 | * |
||
111 | * @return int |
||
112 | */ |
||
113 | public function getGuestGuildCount(User $user): int |
||
114 | { |
||
115 | return $this->getGuestGuildsQuery($user)->count(); |
||
0 ignored issues
–
show
|
|||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get a single guild. |
||
120 | * |
||
121 | * @param User $user |
||
122 | * @param int $guildId The guild id |
||
123 | * |
||
124 | * @return Guild|null |
||
125 | */ |
||
126 | public function getGuestGuild(User $user, int $guildId): ?Guild |
||
127 | { |
||
128 | return tap($this->getGuestGuildsQuery($user)->find($guildId), function($guild) { |
||
129 | if($guild !== null) |
||
130 | { |
||
131 | $guild->isGuest = true; |
||
132 | } |
||
133 | }); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Get a single guild. |
||
138 | * |
||
139 | * @param User $user |
||
140 | * @param int $guildId The guild id |
||
141 | * |
||
142 | * @return Guild|null |
||
143 | */ |
||
144 | public function getUserOrGuestGuild(User $user, int $guildId): ?Guild |
||
145 | { |
||
146 | return $this->getGuild($user, $guildId) ?? |
||
147 | $this->getGuestGuild($user, $guildId); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param User $user |
||
152 | * |
||
153 | * @return Guild|null |
||
154 | */ |
||
155 | public function getFirstGuild(User $user): ?Guild |
||
156 | { |
||
157 | return $this->getGuilds($user)->first() ?? |
||
158 | $this->getGuestGuilds($user)->first(); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Add a new guild. |
||
163 | * |
||
164 | * @param User $user |
||
165 | * @param array $values |
||
166 | * |
||
167 | * @return Guild|null |
||
168 | */ |
||
169 | public function createGuild(User $user, array $values): ?Guild |
||
170 | { |
||
171 | return DB::transaction(function() use($user, $values) { |
||
172 | $guild = $user->guilds()->create($values); |
||
173 | // Also create the default savings fund for the new guild. |
||
174 | $fund = $guild->funds()->create(['title' => '', 'active' => true]); |
||
175 | $this->setDefaultFund($guild, $fund); |
||
176 | return $guild; |
||
177 | }); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Update a guild. |
||
182 | * |
||
183 | * @param User $user |
||
184 | * @param int $guildId |
||
185 | * @param array $values |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function updateGuild(User $user, int $guildId, array $values): bool |
||
190 | { |
||
191 | return $user->guilds()->where('id', $guildId)->update($values); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Delete a guild. |
||
196 | * |
||
197 | * @param User $user |
||
198 | * @param int $guildId |
||
199 | * |
||
200 | * @return void |
||
201 | */ |
||
202 | public function deleteGuild(User $user, int $guildId) |
||
203 | { |
||
204 | $guild = $user->guilds()->find($guildId); |
||
205 | if(!$guild) |
||
206 | { |
||
207 | return; |
||
208 | } |
||
209 | DB::transaction(function() use($guild) { |
||
210 | $guild->funds()->delete(); |
||
211 | $guild->members()->delete(); |
||
212 | $guild->rounds()->delete(); |
||
213 | $guild->charges()->delete(); |
||
214 | $guild->categories()->delete(); |
||
215 | $guild->invites()->detach(); |
||
216 | $guild->delete(); |
||
217 | }); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Get the guild options |
||
222 | * |
||
223 | * @param Guild $guild |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | public function getGuildOptions(Guild $guild): array |
||
228 | { |
||
229 | return $guild->properties; |
||
0 ignored issues
–
show
|
|||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Get the report template name |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getReportTemplate(Guild $guild): string |
||
238 | { |
||
239 | $options = $this->getGuildOptions($guild); |
||
240 | return $options['reports']['template'] ?? |
||
241 | config('tontine.templates.report', 'default'); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Save the guild options |
||
246 | * |
||
247 | * @param Guild $guild |
||
248 | * @param array $options |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | public function saveGuildOptions(Guild $guild, array $options) |
||
253 | { |
||
254 | $properties = $guild->properties; |
||
0 ignored issues
–
show
|
|||
255 | $properties['reports'] = $options['reports']; |
||
256 | $guild->saveProperties($properties); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Set the default fund |
||
261 | * |
||
262 | * @param Guild $guild |
||
263 | * @param FundDef $fund |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | public function setDefaultFund(Guild $guild, FundDef $fund): void |
||
268 | { |
||
269 | $properties = $guild->properties; |
||
0 ignored issues
–
show
|
|||
270 | $properties['savings']['fund']['default'] = $fund->id; |
||
271 | $guild->saveProperties($properties); |
||
272 | } |
||
273 | } |
||
274 |