Passed
Pull Request — main (#55)
by Thierry
05:38
created

SessionTrait::getRoundSessionIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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