Passed
Push — main ( 936852...10149e )
by Thierry
15:09
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\Support\Collection;
7
use Siak\Tontine\Model\Session;
8
9
trait SessionTrait
10
{
11
    /**
12
     * Find a session.
13
     *
14
     * @param int $sessionId    The session id
15
     *
16
     * @return Session|null
17
     */
18
    public function getSession(int $sessionId): ?Session
19
    {
20
        return $this->tenantService->round()->sessions()->find($sessionId);
21
    }
22
23
    /**
24
     * Get the number of sessions in the selected round.
25
     *
26
     * @return int
27
     */
28
    public function getSessionCount(): int
29
    {
30
        return $this->tenantService->round()->sessions()->count();
31
    }
32
33
    /**
34
     * Get a paginated list of sessions in the selected round.
35
     *
36
     * @param int $page
37
     * @param bool $orderAsc
38
     *
39
     * @return Collection
40
     */
41
    public function getSessions(int $page = 0, bool $orderAsc = true): Collection
42
    {
43
        return $this->tenantService->round()->sessions()
44
            ->orderBy('start_at', $orderAsc ? 'asc' : 'desc')
45
            ->page($page, $this->tenantService->getLimit())
46
            ->get();
47
    }
48
49
    /**
50
     * @param int $page
51
     * @param bool $orderAsc
52
     *
53
     * @return Collection
54
     */
55
    public function getRoundSessions(int $page = 0, bool $orderAsc = true): Collection
56
    {
57
        return $this->getSessions($page, $orderAsc);
58
    }
59
60
    /**
61
     * Find a session.
62
     *
63
     * @param int $sessionId    The session id
64
     *
65
     * @return Session|null
66
     */
67
    public function getTontineSession(int $sessionId): ?Session
68
    {
69
        return $this->tenantService->tontine()->sessions()->find($sessionId);
70
    }
71
72
    /**
73
     * @param int $page
74
     * @param bool $orderAsc
75
     *
76
     * @return Collection
77
     */
78
    public function getTontineSessions(int $page = 0, bool $orderAsc = true): Collection
79
    {
80
        return $this->tenantService->tontine()->sessions()
81
            ->orderBy('start_at', $orderAsc ? 'asc' : 'desc')
82
            ->page($page, $this->tenantService->getLimit())
83
            ->get();
84
    }
85
86
    /**
87
     * @param Builder $query
88
     * @param Session|null $currSession
89
     * @param bool $getAfter Get the sessions after or before the provided one
90
     * @param bool $withCurr Keep the provided session in the list
91
     *
92
     * @return Builder
93
     */
94
    private function getSessionsQuery($query, ?Session $currSession, bool $getAfter, bool $withCurr)
95
    {
96
        $operator = $getAfter ? ($withCurr ? '>=' : '>') : ($withCurr ? '<=' : '<');
97
        $currSessionDate = !$currSession ? '' : $currSession->start_at->format('Y-m-d');
98
        return $query->when($currSession !== null, fn(Builder $query) =>
99
            $query->whereDate('start_at', $operator, $currSessionDate));
100
    }
101
102
    /**
103
     * @param Session|null $currSession
104
     * @param bool $getAfter Get the sessions after or before the provided one
105
     * @param bool $withCurr Keep the provided session in the list
106
     *
107
     * @return Builder
108
     */
109
    private function getRoundSessionsQuery(?Session $currSession, bool $getAfter, bool $withCurr)
110
    {
111
        $query = $this->tenantService->round()->sessions();
112
        return $this->getSessionsQuery($query, $currSession, $getAfter, $withCurr);
113
    }
114
115
    /**
116
     * @param Session|null $session
117
     * @param bool $getAfter Get the sessions after or before the provided one
118
     * @param bool $withCurr Keep the provided session in the list
119
     * @param bool $orderAsc
120
     *
121
     * @return Collection
122
     */
123
    public function getRoundSessionIds(?Session $currSession = null,
124
        bool $getAfter = true, bool $withCurr = true, bool $orderAsc = true): Collection
125
    {
126
        return $this->getRoundSessionsQuery($currSession, $getAfter, $withCurr)
127
            ->orderBy('sessions.start_at', $orderAsc ? 'asc' : 'desc')
128
            ->pluck('sessions.id');
129
    }
130
131
    /**
132
     * @param Session|null $currSession
133
     * @param bool $getAfter Get the sessions after or before the provided one
134
     * @param bool $withCurr Keep the provided session in the list
135
     *
136
     * @return int
137
     */
138
    public function getRoundSessionCount(?Session $currSession = null,
139
        bool $getAfter = true, bool $withCurr = true): int
140
    {
141
        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\Eloquent\Builder which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
142
    }
143
144
    /**
145
     * @param Session|null $currSession
146
     * @param bool $getAfter Get the sessions after or before the provided one
147
     * @param bool $withCurr Keep the provided session in the list
148
     *
149
     * @return Builder
150
     */
151
    private function getTontineSessionsQuery(?Session $currSession, bool $getAfter, bool $withCurr)
152
    {
153
        $query = $this->tenantService->tontine()->sessions();
154
        return $this->getSessionsQuery($query, $currSession, $getAfter, $withCurr);
155
    }
156
157
    /**
158
     * @param Session|null $session
159
     * @param bool $getAfter Get the sessions after or before the provided one
160
     * @param bool $withCurr Keep the provided session in the list
161
     * @param bool $orderAsc
162
     *
163
     * @return Collection
164
     */
165
    public function getTontineSessionIds(?Session $currSession = null,
166
        bool $getAfter = true, bool $withCurr = true, bool $orderAsc = true): Collection
167
    {
168
        return $this->getTontineSessionsQuery($currSession, $getAfter, $withCurr)
169
            ->orderBy('sessions.start_at', $orderAsc ? 'asc' : 'desc')
170
            ->pluck('sessions.id');
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 int
179
     */
180
    public function getTontineSessionCount(?Session $currSession = null,
181
        bool $getAfter = true, bool $withCurr = true): int
182
    {
183
        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\Eloquent\Builder which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
184
    }
185
}
186