GuildService::createGuild()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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 in_array;
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
     * @param User $user
134
     *
135
     * @return Guild|null
136
     */
137
    public function getFirstGuild(User $user): ?Guild
138
    {
139
        return $this->getGuilds($user)->first() ??
140
            $this->getGuestGuilds($user)->first();
141
    }
142
143
    /**
144
     * Add a new guild.
145
     *
146
     * @param User $user
147
     * @param array $values
148
     *
149
     * @return Guild|null
150
     */
151
    public function createGuild(User $user, array $values): ?Guild
152
    {
153
        return DB::transaction(function() use($user, $values) {
154
            $guild = $user->guilds()->create($values);
155
156
            // Also create the default savings fund for the new guild.
157
            $guild->funds()->create([
158
                'type' => FundDef::TYPE_AUTO,
159
                'title' => '',
160
                'active' => true,
161
            ]);
162
163
            return $guild;
164
        });
165
    }
166
167
    /**
168
     * Update a guild.
169
     *
170
     * @param User $user
171
     * @param int $guildId
172
     * @param array $values
173
     *
174
     * @return bool
175
     */
176
    public function updateGuild(User $user, int $guildId, array $values): bool
177
    {
178
        return $user->guilds()->where('id', $guildId)->update($values);
179
    }
180
181
    /**
182
     * Delete a guild.
183
     *
184
     * @param User $user
185
     * @param int $guildId
186
     *
187
     * @return void
188
     */
189
    public function deleteGuild(User $user, int $guildId)
190
    {
191
        if(!($guild = $user->guilds()->find($guildId)))
192
        {
193
            return;
194
        }
195
196
        DB::transaction(function() use($guild) {
197
            $guild->funds()->delete();
198
            $guild->members()->delete();
199
            $guild->rounds()->delete();
200
            $guild->charges()->delete();
201
            $guild->categories()->delete();
202
            $guild->invites()->detach();
203
            $guild->delete();
204
        });
205
    }
206
207
    /**
208
     * Get the guild options
209
     *
210
     * @param Guild $guild
211
     *
212
     * @return array
213
     */
214
    public function getGuildOptions(Guild $guild): array
215
    {
216
        return $guild->properties;
0 ignored issues
show
Bug introduced by
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...
217
    }
218
219
    /**
220
     * Get the report template name
221
     *
222
     * @return string
223
     */
224
    public function getReportTemplate(Guild $guild): string
225
    {
226
        $options = $this->getGuildOptions($guild);
227
        $template = $options['reports']['template'] ?? 'raptor';
228
        return in_array($template, ['raptor', 'legacy']) ? $template : 'raptor';
229
    }
230
231
    /**
232
     * Save the guild options
233
     *
234
     * @param Guild $guild
235
     * @param array $options
236
     *
237
     * @return void
238
     */
239
    public function saveGuildOptions(Guild $guild, array $options)
240
    {
241
        $properties = $guild->properties;
0 ignored issues
show
Bug introduced by
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...
242
        $properties['reports'] = $options['reports'];
243
        $guild->saveProperties($properties);
244
    }
245
}
246