Passed
Push — main ( 082d50...301c01 )
by Thierry
06:28
created

PoolService::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
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 Illuminate\Support\Facades\DB;
9
use Siak\Tontine\Model\Pool;
10
use Siak\Tontine\Model\Round;
11
use Siak\Tontine\Service\DataSyncService;
12
use Siak\Tontine\Service\TenantService;
13
14
class PoolService
15
{
16
    use SessionTrait;
0 ignored issues
show
Bug introduced by
The trait Siak\Tontine\Service\Planning\SessionTrait requires the property $day_date which is not provided by Siak\Tontine\Service\Planning\PoolService.
Loading history...
17
18
    /**
19
     * @param TenantService $tenantService
20
     * @param DataSyncService $dataSyncService
21
     */
22
    public function __construct(protected TenantService $tenantService,
23
        private DataSyncService $dataSyncService)
24
    {}
25
26
    /**
27
     * @param Round $round
28
     * @param bool $filter|null
29
     *
30
     * @return Builder|Relation
31
     */
32
    public function getQuery(Round $round, ?bool $filter): Builder|Relation
33
    {
34
        return $this->tenantService->guild()->pools()
35
            ->when($filter === true, function(Builder $query) use($round) {
36
                return $query->whereHas('pools', fn($q) => $q->ofRound($round));
37
            })
38
            ->when($filter === false, function(Builder $query) use($round) {
39
                return $query->whereDoesntHave('pools', fn($q) => $q->ofRound($round));
40
            });
41
    }
42
43
    /**
44
     * Get a paginated list of pools.
45
     *
46
     * @param Round $round
47
     * @param bool $filter|null
48
     * @param int $page
49
     *
50
     * @return Collection
51
     */
52
    public function getPoolDefs(Round $round, ?bool $filter, int $page = 0): Collection
53
    {
54
        return $this->getQuery($round, $filter)
55
            ->with([
56
                'pools' => fn($query) => $query->ofRound($round),
57
            ])
58
            ->page($page, $this->tenantService->getLimit())
59
            ->get();
60
    }
61
62
    /**
63
     * Get the number of pools.
64
     *
65
     * @param Round $round
66
     * @param bool $filter|null
67
     *
68
     * @return int
69
     */
70
    public function getPoolDefCount(Round $round, ?bool $filter): int
71
    {
72
        return $this->getQuery($round, $filter)->count();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getQuery($round, $filter)->count() could return the type Illuminate\Database\Eloq...uent\Relations\Relation which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    /**
76
     * @param int $defId
77
     *
78
     * @return void
79
     */
80
    public function enablePool(Round $round, int $defId): void
81
    {
82
        $def = $this->tenantService->guild()->pools()
83
            ->withCount([
84
                'pools' => fn($query) => $query->ofRound($round),
85
            ])
86
            ->find($defId);
87
        if(!$def || $def->pools_count > 0)
88
        {
89
            return;
90
        }
91
92
        // Create the pool
93
        $def->pools()->create([
94
            'round_id' => $round->id,
95
            'start_sid' => $round->start->id,
0 ignored issues
show
Bug introduced by
The property start does not seem to exist on Siak\Tontine\Model\Round. 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...
96
            'end_sid' => $round->end->id,
0 ignored issues
show
Bug introduced by
The property end does not seem to exist on Siak\Tontine\Model\Round. 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...
97
        ]);
98
    }
99
100
    /**
101
     * @param int $defId
102
     *
103
     * @return void
104
     */
105
    public function disablePool(Round $round, int $defId): void
106
    {
107
        $def = $this->tenantService->guild()->pools()
108
            ->withCount([
109
                'pools' => fn($query) => $query->ofRound($round),
110
            ])
111
            ->find($defId);
112
        if(!$def || $def->pools_count === 0)
113
        {
114
            return;
115
        }
116
117
        // Delete the pool
118
        $poolIds = $def->pools()->ofRound($round)->pluck('id');
119
        DB::table('pool_session_disabled')->whereIn('pool_id', $poolIds)->delete();
120
        Pool::whereIn('id', $poolIds)->delete();
121
    }
122
123
    /**
124
     * Get a paginated list of pools.
125
     *
126
     * @param int $page
127
     *
128
     * @return Collection
129
     */
130
    public function getPools(Round $round, int $page = 0): Collection
131
    {
132
        return Pool::ofRound($round)
133
            ->page($page, $this->tenantService->getLimit())
134
            ->get();
135
    }
136
137
    /**
138
     * Get the number of pools.
139
     *
140
     * @return int
141
     */
142
    public function getPoolCount(Round $round): int
143
    {
144
        return Pool::ofRound($round)->count();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Siak\Tontine\Mode...fRound($round)->count() could return the type Illuminate\Database\Eloq...gHasThroughRelationship which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
145
    }
146
147
    /**
148
     * Get a pool.
149
     *
150
     * @return Pool|null
151
     */
152
    public function getPool(Round $round, int $poolId): ?Pool
153
    {
154
        return Pool::ofRound($round)
155
            ->with(['sessions', 'disabled_sessions'])
156
            ->find($poolId);
157
    }
158
159
    /**
160
     * Get the sessions enabled for a pool.
161
     *
162
     * @param Pool $pool
163
     *
164
     * @return Collection
165
     */
166
    public function getActiveSessions(Pool $pool): Collection
167
    {
168
        return $pool->sessions()->orderBy('day_date')->get();
169
    }
170
171
    /**
172
     * Get the number of sessions enabled for a pool.
173
     *
174
     * @param Pool $pool
175
     *
176
     * @return int
177
     */
178
    public function getActiveSessionCount(Pool $pool): int
179
    {
180
        return $pool->sessions()->count();
181
    }
182
183
    /**
184
     * Save the pool sessions.
185
     *
186
     * @param Pool $pool
187
     * @param array $values
188
     *
189
     * @return void
190
     */
191
    public function saveSessions(Pool $pool, array $values)
192
    {
193
        DB::transaction(function() use($pool, $values) {
194
            $pool->update($values);
195
196
            // $this->dataSyncService->syncPool($pool, true);
197
        });
198
    }
199
200
    /**
201
     * Enable or disable a session for a pool.
202
     *
203
     * @param Pool $pool
204
     * @param int $sessionId    The session id
205
     *
206
     * @return void
207
     */
208
    public function enableSession(Pool $pool, int $sessionId)
209
    {
210
        // When the remitments are planned, don't enable or disable a session
211
        // if receivables already exist on the pool.
212
        // if($pool->remit_planned &&
213
        //     $pool->subscriptions()->whereHas('receivables')->count() > 0)
214
        // {
215
        //     return;
216
        // }
217
        $session = $pool->disabled_sessions()->find($sessionId);
218
        if(!$session)
219
        {
220
            return;
221
        }
222
223
        // Enable the session for the pool.
224
        $pool->disabled_sessions()->detach($session->id);
225
    }
226
227
    /**
228
     * Enable or disable a session for a pool.
229
     *
230
     * @param Pool $pool
231
     * @param int $sessionId    The session id
232
     *
233
     * @return void
234
     */
235
    public function disableSession(Pool $pool, int $sessionId)
236
    {
237
        // When the remitments are planned, don't enable or disable a session
238
        // if receivables already exist on the pool.
239
        // if($pool->remit_planned &&
240
        //     $pool->subscriptions()->whereHas('receivables')->count() > 0)
241
        // {
242
        //     return;
243
        // }
244
        $session = $pool->sessions()->find($sessionId);
245
        if(!$session)
246
        {
247
            return;
248
        }
249
250
        // Disable the session for the pool.
251
        DB::transaction(function() use($pool, $session) {
252
            $pool->disabled_sessions()->attach($session->id);
253
254
            // $this->dataSyncService->syncPool($pool, true);
255
        });
256
    }
257
}
258