Passed
Push — main ( 7be313...891842 )
by Thierry
19:01 queued 53s
created

SessionTrait::getSessions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Service\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Illuminate\Support\Collection;
8
use Siak\Tontine\Model\Session;
9
10
use function tap;
11
use function trans;
12
13
trait SessionTrait
14
{
15
    use WithTrait;
16
17
    /**
18
     * @var bool
19
     */
20
    private bool $filterActive = false;
21
22
    /**
23
     * @param bool $filter
24
     *
25
     * @return self
26
     */
27
    public function active(bool $filter = true): self
28
    {
29
        $this->filterActive = $filter;
30
        return $this;
31
    }
32
33
    private function getRoundSessionQuery(): Relation
34
    {
35
        return $this->tenantService->round()->sessions()
36
            ->when($this->filterActive, fn(Builder $query) => $query->active());
37
    }
38
39
    /**
40
     * Find a session.
41
     *
42
     * @param int $sessionId    The session id
43
     *
44
     * @return Session|null
45
     */
46
    public function getSession(int $sessionId): ?Session
47
    {
48
        return tap($this->getRoundSessionQuery(), fn($query) => $this->addWith($query))
49
            ->find($sessionId);
50
    }
51
52
    /**
53
     * Get the number of sessions in the selected round.
54
     *
55
     * @return int
56
     */
57
    public function getSessionCount(): int
58
    {
59
        return $this->getRoundSessionQuery()->count();
60
    }
61
62
    /**
63
     * Get a paginated list of sessions in the selected round.
64
     *
65
     * @param int $page
66
     * @param bool $orderAsc
67
     *
68
     * @return Collection
69
     */
70
    public function getSessions(int $page = 0, bool $orderAsc = true): Collection
71
    {
72
        return tap($this->getRoundSessionQuery(), fn($query) => $this->addWith($query))
73
            ->orderBy('start_at', $orderAsc ? 'asc' : 'desc')
74
            ->page($page, $this->tenantService->getLimit())
75
            ->get();
76
    }
77
78
    /**
79
     * @param int $page
80
     * @param bool $orderAsc
81
     *
82
     * @return Collection
83
     */
84
    public function getRoundSessions(int $page = 0, bool $orderAsc = true): Collection
85
    {
86
        return $this->getSessions($page, $orderAsc);
87
    }
88
89
    /**
90
     * Find a session.
91
     *
92
     * @param int $sessionId    The session id
93
     *
94
     * @return Session|null
95
     */
96
    public function getTontineSession(int $sessionId): ?Session
97
    {
98
        return $this->tenantService->tontine()->sessions()
99
            ->when($this->filterActive, fn(Builder $query) => $query->active())
100
            ->find($sessionId);
101
    }
102
103
    /**
104
     * @param int $page
105
     * @param bool $orderAsc
106
     *
107
     * @return Collection
108
     */
109
    public function getTontineSessions(int $page = 0, bool $orderAsc = true): Collection
110
    {
111
        return $this->tenantService->tontine()->sessions()
112
            ->when($this->filterActive, fn(Builder $query) => $query->active())
113
            ->orderBy('start_at', $orderAsc ? 'asc' : 'desc')
114
            ->page($page, $this->tenantService->getLimit())
115
            ->get();
116
    }
117
118
    /**
119
     * @param Builder|Relation $query
120
     * @param Session|null $currSession
121
     * @param bool $getAfter Get the sessions after or before the provided one
122
     * @param bool $withCurr Keep the provided session in the list
123
     *
124
     * @return Builder|Relation
125
     */
126
    private function getSessionsQuery($query, ?Session $currSession, bool $getAfter, bool $withCurr): Builder|Relation
127
    {
128
        $operator = $getAfter ? ($withCurr ? '>=' : '>') : ($withCurr ? '<=' : '<');
129
        $currSessionDate = !$currSession ? '' : $currSession->start_at->format('Y-m-d');
130
        return $query->when($currSession !== null, fn(Builder $query) =>
131
            $query->whereDate('start_at', $operator, $currSessionDate));
132
    }
133
134
    /**
135
     * @param Session|null $currSession
136
     * @param bool $getAfter Get the sessions after or before the provided one
137
     * @param bool $withCurr Keep the provided session in the list
138
     *
139
     * @return Builder|Relation
140
     */
141
    private function getRoundSessionsQuery(?Session $currSession, bool $getAfter, bool $withCurr): Builder|Relation
142
    {
143
        return $this->getSessionsQuery($this->getRoundSessionQuery(), $currSession, $getAfter, $withCurr);
144
    }
145
146
    /**
147
     * @param Session|null $session
148
     * @param bool $getAfter Get the sessions after or before the provided one
149
     * @param bool $withCurr Keep the provided session in the list
150
     * @param bool $orderAsc
151
     *
152
     * @return Collection
153
     */
154
    public function getRoundSessionIds(?Session $currSession = null,
155
        bool $getAfter = false, bool $withCurr = true): Collection
156
    {
157
        return $this->getRoundSessionsQuery($currSession, $getAfter, $withCurr)
158
            ->pluck('sessions.id');
159
    }
160
161
    /**
162
     * @param Session|null $currSession
163
     * @param bool $getAfter Get the sessions after or before the provided one
164
     * @param bool $withCurr Keep the provided session in the list
165
     *
166
     * @return int
167
     */
168
    public function getRoundSessionCount(?Session $currSession = null,
169
        bool $getAfter = false, bool $withCurr = true): int
170
    {
171
        return $this->getRoundSessionsQuery($currSession, $getAfter, $withCurr)->count();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRoundSe...er, $withCurr)->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...
172
    }
173
174
    /**
175
     * @param Session|null $currSession
176
     * @param bool $getAfter Get the sessions after or before the provided one
177
     * @param bool $withCurr Keep the provided session in the list
178
     *
179
     * @return Builder|Relation
180
     */
181
    private function getTontineSessionsQuery(?Session $currSession, bool $getAfter, bool $withCurr): Builder|Relation
182
    {
183
        $query = $this->tenantService->tontine()->sessions();
184
        return $this->getSessionsQuery($query, $currSession, $getAfter, $withCurr);
185
    }
186
187
    /**
188
     * @param Session|null $session
189
     * @param bool $getAfter Get the sessions after or before the provided one
190
     * @param bool $withCurr Keep the provided session in the list
191
     * @param bool $orderAsc
192
     *
193
     * @return Collection
194
     */
195
    public function getTontineSessionIds(?Session $currSession = null,
196
        bool $getAfter = false, bool $withCurr = true): Collection
197
    {
198
        return $this->getTontineSessionsQuery($currSession, $getAfter, $withCurr)
199
            ->pluck('sessions.id');
200
    }
201
202
    /**
203
     * @param Session|null $currSession
204
     * @param bool $getAfter Get the sessions after or before the provided one
205
     * @param bool $withCurr Keep the provided session in the list
206
     *
207
     * @return int
208
     */
209
    public function getTontineSessionCount(?Session $currSession = null,
210
        bool $getAfter = false, bool $withCurr = true): int
211
    {
212
        return $this->getTontineSessionsQuery($currSession, $getAfter, $withCurr)->count();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getTontine...er, $withCurr)->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...
213
    }
214
215
    /**
216
     * Get the session statuses
217
     *
218
     * @return array
219
     */
220
    public function getSessionStatuses(): array
221
    {
222
        return [
223
            Session::STATUS_PENDING => trans('tontine.session.status.pending'),
224
            Session::STATUS_OPENED => trans('tontine.session.status.opened'),
225
            Session::STATUS_CLOSED => trans('tontine.session.status.closed'),
226
        ];;
227
    }
228
}
229