Passed
Pull Request — main (#65)
by Thierry
10:34
created

SessionService::closeSession()   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 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Siak\Tontine\Service\Meeting\Session;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Illuminate\Support\Collection;
8
use Siak\Tontine\Model\Round;
9
use Siak\Tontine\Model\Session;
10
use Siak\Tontine\Service\TenantService;
11
use Siak\Tontine\Service\Traits\WithTrait;
12
13
use function tap;
14
use function trans;
15
16
class SessionService
17
{
18
    use WithTrait;
19
20
    /**
21
     * @var bool
22
     */
23
    private bool $filterActive = false;
24
25
    /**
26
     * @param TenantService $tenantService
27
     */
28
    public function __construct(private TenantService $tenantService)
29
    {}
30
31
    /**
32
     * Get the session statuses
33
     *
34
     * @return array
35
     */
36
    public function getSessionStatuses(): array
37
    {
38
        return [
39
            Session::STATUS_PENDING => trans('tontine.session.status.pending'),
40
            Session::STATUS_OPENED => trans('tontine.session.status.opened'),
41
            Session::STATUS_CLOSED => trans('tontine.session.status.closed'),
42
        ];;
43
    }
44
45
    /**
46
     * @param bool $filter
47
     *
48
     * @return self
49
     */
50
    public function active(bool $filter = true): self
51
    {
52
        $this->filterActive = $filter;
53
        return $this;
54
    }
55
56
    /**
57
     * @param Round $round
58
     *
59
     * @return Relation
60
     */
61
    private function getSessionsQuery(Round $round): Relation
62
    {
63
        return $round->sessions()->when($this->filterActive,
64
            fn(Builder $query) => $query->active());
65
    }
66
67
    /**
68
     * Find a session.
69
     *
70
     * @param Round $round
71
     * @param int $sessionId    The session id
72
     *
73
     * @return Session|null
74
     */
75
    public function getSession(Round $round, int $sessionId): ?Session
76
    {
77
        return tap($this->getSessionsQuery($round),
78
            fn($query) => $this->addWith($query))->find($sessionId);
79
    }
80
81
    /**
82
     * Get the number of sessions in the selected round.
83
     *
84
     * @param Round $round
85
     *
86
     * @return int
87
     */
88
    public function getSessionCount(Round $round): int
89
    {
90
        return $this->getSessionsQuery($round)->count();
91
    }
92
93
    /**
94
     * Get a paginated list of sessions in the selected round.
95
     *
96
     * @param Round $round
97
     * @param int $page
98
     * @param bool $orderAsc
99
     *
100
     * @return Collection
101
     */
102
    public function getSessions(Round $round, int $page = 0, bool $orderAsc = true): Collection
103
    {
104
        return tap($this->getSessionsQuery($round),
105
                fn($query) => $this->addWith($query))
106
            ->orderBy('day_date', $orderAsc ? 'asc' : 'desc')
107
            ->page($page, $this->tenantService->getLimit())
108
            ->get();
109
    }
110
111
    /**
112
     * Open a session.
113
     *
114
     * @param Session $session
115
     *
116
     * @return void
117
     */
118
    public function openSession(Session $session)
119
    {
120
        $session->update(['status' => Session::STATUS_OPENED]);
121
    }
122
123
    /**
124
     * Close a session.
125
     *
126
     * @param Session $session
127
     *
128
     * @return void
129
     */
130
    public function closeSession(Session $session)
131
    {
132
        $session->update(['status' => Session::STATUS_CLOSED]);
133
    }
134
135
    /**
136
     * Find the prev session.
137
     *
138
     * @param Round $round
139
     * @param Session $session
140
     *
141
     * @return Session|null
142
     */
143
    public function getPrevSession(Round $round, Session $session): ?Session
144
    {
145
        return $round->sessions()->active()
146
            ->where('day_date', '<', $session->day_date)
147
            ->orderBy('day_date', 'desc')
148
            ->first();
149
    }
150
151
    /**
152
     * Find the next session.
153
     *
154
     * @param Round $round
155
     * @param Session $session
156
     *
157
     * @return Session|null
158
     */
159
    public function getNextSession(Round $round, Session $session): ?Session
160
    {
161
        return $round->sessions()->active()
162
            ->where('day_date', '>', $session->day_date)
163
            ->orderBy('day_date', 'asc')
164
            ->first();
165
    }
166
167
    /**
168
     * Update a session agenda.
169
     *
170
     * @param Session $session
171
     * @param string $agenda
172
     *
173
     * @return void
174
     */
175
    public function saveAgenda(Session $session, string $agenda): void
176
    {
177
        $session->update(['agenda' => $agenda]);
178
    }
179
180
    /**
181
     * Update a session report.
182
     *
183
     * @param Session $session
184
     * @param string $report
185
     *
186
     * @return void
187
     */
188
    public function saveReport(Session $session, string $report): void
189
    {
190
        $session->update(['report' => $report]);
191
    }
192
}
193