Passed
Push — main ( 5b7914...28aa50 )
by Thierry
05:31
created

TontineService::getFirstTontine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Siak\Tontine\Service\Tontine;
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\Tontine;
10
use Siak\Tontine\Service\TenantService;
11
12
use function config;
13
use function tap;
14
15
class TontineService
16
{
17
    /**
18
     * @param TenantService $tenantService
19
     */
20
    public function __construct(protected TenantService $tenantService)
21
    {}
22
23
    /**
24
     * Get a paginated list of tontines in the selected round.
25
     *
26
     * @param int $page
27
     *
28
     * @return Collection
29
     */
30
    public function getTontines(int $page = 0): Collection
31
    {
32
        return $this->tenantService->user()->tontines()
33
            ->page($page, $this->tenantService->getLimit())
34
            ->get();
35
    }
36
37
    /**
38
     * Get the number of tontines in the selected round.
39
     *
40
     * @return int
41
     */
42
    public function getTontineCount(): int
43
    {
44
        return $this->tenantService->user()->tontines()->count();
45
    }
46
47
    /**
48
     * Get a single tontine.
49
     *
50
     * @param int $tontineId    The tontine id
51
     *
52
     * @return Tontine|null
53
     */
54
    public function getTontine(int $tontineId): ?Tontine
55
    {
56
        return $this->tenantService->user()->tontines()->find($tontineId);
57
    }
58
59
    /**
60
     * @return Builder|Relation
61
     */
62
    public function getGuestTontinesQuery(): Builder|Relation
63
    {
64
        return Tontine::whereHas('invites', function(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...\Database\Query\Builder 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...
65
            $query->where('guest_id', $this->tenantService->user()->id);
66
        });
67
    }
68
69
    /**
70
     * Check if the user has guest tontines
71
     *
72
     * @return bool
73
     */
74
    public function hasGuestOrganisations(): bool
75
    {
76
        return $this->getGuestTontinesQuery()->exists();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getGuestTontinesQuery()->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...
77
    }
78
79
    /**
80
     * Get a paginated list of tontines in the selected round.
81
     *
82
     * @param int $page
83
     *
84
     * @return Collection
85
     */
86
    public function getGuestTontines(int $page = 0): Collection
87
    {
88
        return $this->getGuestTontinesQuery()
89
            ->with(['user'])
90
            ->orderBy('tontines.id')
91
            ->page($page, $this->tenantService->getLimit())
92
            ->get()
93
            ->each(fn($tontine) => $tontine->isGuest = true);
94
    }
95
96
    /**
97
     * Get the number of tontines in the selected round.
98
     *
99
     * @return int
100
     */
101
    public function getGuestTontineCount(): int
102
    {
103
        return $this->getGuestTontinesQuery()->count();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getGuestTontinesQuery()->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...
104
    }
105
106
    /**
107
     * Get a single tontine.
108
     *
109
     * @param int $tontineId    The tontine id
110
     *
111
     * @return Tontine|null
112
     */
113
    public function getGuestTontine(int $tontineId): ?Tontine
114
    {
115
        return tap($this->getGuestTontinesQuery()->find($tontineId), function($tontine) {
116
            if($tontine !== null)
117
            {
118
                $tontine->isGuest = true;
119
            }
120
        });
121
    }
122
123
    /**
124
     * Get a single tontine.
125
     *
126
     * @param int $tontineId    The tontine id
127
     *
128
     * @return Tontine|null
129
     */
130
    public function getUserOrGuestTontine(int $tontineId): ?Tontine
131
    {
132
        return $this->getTontine($tontineId) ?? $this->getGuestTontine($tontineId);
133
    }
134
135
    /**
136
     * @return Tontine|null
137
     */
138
    public function getFirstTontine(): ?Tontine
139
    {
140
        return $this->getTontines()->first() ?? $this->getGuestTontines()->first();
141
    }
142
143
    /**
144
     * Add a new tontine.
145
     *
146
     * @param array $values
147
     *
148
     * @return bool
149
     */
150
    public function createTontine(array $values): bool
151
    {
152
        DB::transaction(function() use($values) {
153
            $tontine = $this->tenantService->user()->tontines()->create($values);
154
            // Also create the default savings fund for the new tontine.
155
            $tontine->funds()->create(['title' => '', 'active' => true]);
156
        });
157
        return true;
158
    }
159
160
    /**
161
     * Update a tontine.
162
     *
163
     * @param int $id
164
     * @param array $values
165
     *
166
     * @return bool
167
     */
168
    public function updateTontine(int $id, array $values): bool
169
    {
170
        return $this->tenantService->user()->tontines()->where('id', $id)->update($values);
171
    }
172
173
    /**
174
     * Delete a tontine.
175
     *
176
     * @param int $id
177
     *
178
     * @return void
179
     */
180
    public function deleteTontine(int $id)
181
    {
182
        $tontine = $this->tenantService->user()->tontines()->find($id);
183
        if(!$tontine)
184
        {
185
            return;
186
        }
187
        DB::transaction(function() use($tontine) {
188
            $tontine->funds()->withoutGlobalScope('user')->delete();
189
            $tontine->members()->delete();
190
            $tontine->rounds()->delete();
191
            $tontine->charges()->delete();
192
            $tontine->categories()->delete();
193
            $tontine->invites()->detach();
194
            $tontine->delete();
195
        });
196
    }
197
198
    /**
199
     * Get the tontine options
200
     *
201
     * @return array
202
     */
203
    public function getTontineOptions(): array
204
    {
205
        return $this->tenantService->tontine()->properties;
0 ignored issues
show
Bug introduced by
The property properties does not seem to exist on Siak\Tontine\Model\Tontine. 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...
206
    }
207
208
    /**
209
     * Get the report template name
210
     *
211
     * @return string
212
     */
213
    public function getReportTemplate(): string
214
    {
215
        $options = $this->getTontineOptions();
216
        return $options['reports']['template'] ?? config('tontine.templates.report', 'default');
217
    }
218
219
    /**
220
     * Save the tontine options
221
     *
222
     * @param array $options
223
     *
224
     * @return void
225
     */
226
    public function saveTontineOptions(array $options)
227
    {
228
        $tontine = $this->tenantService->tontine();
229
        $properties = $tontine->properties;
0 ignored issues
show
Bug introduced by
The property properties does not seem to exist on Siak\Tontine\Model\Tontine. 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...
230
        $properties['reports'] = $options['reports'];
231
        $tontine->saveProperties($properties);
232
    }
233
}
234