Issues (196)

src/Service/Guild/GuildService.php (5 issues)

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
The expression return Siak\Tontine\Mode...ion(...) { /* ... */ }) could return the type Illuminate\Database\Eloq...iak\Tontine\Model\Guild which is incompatible with the type-hinted return Illuminate\Database\Eloq...uent\Relations\Relation. Consider adding an additional type-check to rule them out.
Loading history...
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
Bug Best Practice introduced by
The expression return $this->getGuestGu...sQuery($user)->exists() could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
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
Bug Best Practice introduced by
The expression return $this->getGuestGuildsQuery($user)->count() could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
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),
129
            fn($guild) => $guild !== null && $guild->isGuest = true);
130
    }
131
132
    /**
133
     * Get a single guild.
134
     *
135
     * @param User $user
136
     * @param int $guildId    The guild id
137
     *
138
     * @return Guild|null
139
     */
140
    public function getUserOrGuestGuild(User $user, int $guildId): ?Guild
141
    {
142
        return $this->getGuild($user, $guildId) ??
143
            $this->getGuestGuild($user, $guildId);
144
    }
145
146
    /**
147
     * @param User $user
148
     *
149
     * @return Guild|null
150
     */
151
    public function getFirstGuild(User $user): ?Guild
152
    {
153
        return $this->getGuilds($user)->first() ??
154
            $this->getGuestGuilds($user)->first();
155
    }
156
157
    /**
158
     * Add a new guild.
159
     *
160
     * @param User $user
161
     * @param array $values
162
     *
163
     * @return Guild|null
164
     */
165
    public function createGuild(User $user, array $values): ?Guild
166
    {
167
        return DB::transaction(function() use($user, $values) {
168
            $guild = $user->guilds()->create($values);
169
170
            // Also create the default savings fund for the new guild.
171
            $guild->funds()->create([
172
                'type' => FundDef::TYPE_AUTO,
173
                'title' => '',
174
                'active' => true,
175
            ]);
176
177
            return $guild;
178
        });
179
    }
180
181
    /**
182
     * Update a guild.
183
     *
184
     * @param User $user
185
     * @param int $guildId
186
     * @param array $values
187
     *
188
     * @return bool
189
     */
190
    public function updateGuild(User $user, int $guildId, array $values): bool
191
    {
192
        return $user->guilds()->where('id', $guildId)->update($values);
193
    }
194
195
    /**
196
     * Delete a guild.
197
     *
198
     * @param User $user
199
     * @param int $guildId
200
     *
201
     * @return void
202
     */
203
    public function deleteGuild(User $user, int $guildId)
204
    {
205
        if(!($guild = $user->guilds()->find($guildId)))
206
        {
207
            return;
208
        }
209
210
        DB::transaction(function() use($guild) {
211
            $guild->funds()->delete();
212
            $guild->members()->delete();
213
            $guild->rounds()->delete();
214
            $guild->charges()->delete();
215
            $guild->categories()->delete();
216
            $guild->invites()->detach();
217
            $guild->delete();
218
        });
219
    }
220
221
    /**
222
     * Get the guild options
223
     *
224
     * @param Guild $guild
225
     *
226
     * @return array
227
     */
228
    public function getGuildOptions(Guild $guild): array
229
    {
230
        return $guild->properties;
0 ignored issues
show
The property properties does not seem to exist on Siak\Tontine\Model\Guild. 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...
231
    }
232
233
    /**
234
     * Get the report template name
235
     *
236
     * @return string
237
     */
238
    public function getReportTemplate(Guild $guild): string
239
    {
240
        $options = $this->getGuildOptions($guild);
241
        return $options['reports']['template'] ??
242
            config('tontine.templates.report', 'default');
243
    }
244
245
    /**
246
     * Save the guild options
247
     *
248
     * @param Guild $guild
249
     * @param array $options
250
     *
251
     * @return void
252
     */
253
    public function saveGuildOptions(Guild $guild, array $options)
254
    {
255
        $properties = $guild->properties;
0 ignored issues
show
The property properties does not seem to exist on Siak\Tontine\Model\Guild. 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...
256
        $properties['reports'] = $options['reports'];
257
        $guild->saveProperties($properties);
258
    }
259
}
260