Passed
Push — main ( 198534...6a0396 )
by Thierry
05:52
created

SessionService::resyncSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Siak\Tontine\Service\Meeting;
4
5
use Illuminate\Support\Facades\DB;
6
use Siak\Tontine\Exception\MessageException;
7
use Siak\Tontine\Model\Pool;
8
use Siak\Tontine\Model\Round;
9
use Siak\Tontine\Model\Session;
10
use Siak\Tontine\Service\TenantService;
11
use Siak\Tontine\Service\Traits\EventTrait;
12
use Siak\Tontine\Service\Traits\SessionTrait;
13
14
use function trans;
15
16
class SessionService
17
{
18
    use EventTrait;
0 ignored issues
show
introduced by
The trait Siak\Tontine\Service\Traits\EventTrait requires some properties which are not provided by Siak\Tontine\Service\Meeting\SessionService: $name, $lendable, $tontine_bills, $amount, $round_bills, $id, $session_bills, $round, $period_once
Loading history...
19
    use SessionTrait;
0 ignored issues
show
Bug introduced by
The trait Siak\Tontine\Service\Traits\SessionTrait requires the property $start_at which is not provided by Siak\Tontine\Service\Meeting\SessionService.
Loading history...
20
21
    /**
22
     * @param TenantService $tenantService
23
     */
24
    public function __construct(private TenantService $tenantService)
25
    {}
26
27
    /**
28
     * Check if some pools still have no subscriptions.
29
     *
30
     * @return void
31
     */
32
    public function checkPoolsSubscriptions()
33
    {
34
        if($this->tenantService->round()->pools()->whereDoesntHave('subscriptions')->count() > 0)
35
        {
36
            throw new MessageException(trans('tontine.errors.action') .
37
                '<br/>' . trans('tontine.pool.errors.no_subscription'));
38
        }
39
    }
40
41
    /**
42
     * Open a round.
43
     *
44
     * @param Round $round
45
     *
46
     * @return void
47
     */
48
    private function openRound(Round $round)
0 ignored issues
show
Unused Code introduced by
The method openRound() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
49
    {
50
        $round->update(['status' => Round::STATUS_OPENED]);
51
    }
52
53
    /**
54
     * Close a round.
55
     *
56
     * @param Round $round
57
     *
58
     * @return void
59
     */
60
    public function closeRound(Round $round)
61
    {
62
        $round->update(['status' => Round::STATUS_CLOSED]);
63
    }
64
65
    /**
66
     * Sync a session with new members or new subscriptions.
67
     *
68
     * @param Session $session
69
     *
70
     * @return void
71
     */
72
    private function syncSession(Session $session)
73
    {
74
        DB::transaction(function() use($session) {
75
            // Don't sync a round if there are pools with no subscription.
76
            // $this->checkPoolsSubscriptions();
77
78
            // Sync the round.
79
            $session->round->update(['status' => Round::STATUS_OPENED]);
80
            $this->roundSynced($this->tenantService->tontine(), $session->round);
0 ignored issues
show
Bug introduced by
It seems like $this->tenantService->tontine() can also be of type null; however, parameter $tontine of Siak\Tontine\Service\Mee...nService::roundSynced() does only seem to accept Siak\Tontine\Model\Tontine, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            $this->roundSynced(/** @scrutinizer ignore-type */ $this->tenantService->tontine(), $session->round);
Loading history...
81
82
            // Sync the session
83
            $session->update(['status' => Session::STATUS_OPENED]);
84
            $this->sessionSynced($this->tenantService->tontine(), $session);
0 ignored issues
show
Bug introduced by
It seems like $this->tenantService->tontine() can also be of type null; however, parameter $tontine of Siak\Tontine\Service\Mee...ervice::sessionSynced() does only seem to accept Siak\Tontine\Model\Tontine, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            $this->sessionSynced(/** @scrutinizer ignore-type */ $this->tenantService->tontine(), $session);
Loading history...
85
        });
86
    }
87
88
    /**
89
     * Open a session.
90
     *
91
     * @param Session $session
92
     *
93
     * @return void
94
     */
95
    public function openSession(Session $session)
96
    {
97
        if($session->pending)
98
        {
99
            // If the session is getting opened for the first time, then
100
            // its also needs to get synced with charges and subscriptions.
101
            $this->syncSession($session);
102
            return;
103
        }
104
        if(!$session->opened)
105
        {
106
            // Open the session
107
            $session->update(['status' => Session::STATUS_OPENED]);
108
        }
109
    }
110
111
    /**
112
     * Close a session.
113
     *
114
     * @param Session $session
115
     *
116
     * @return void
117
     */
118
    public function closeSession(Session $session)
119
    {
120
        $session->update(['status' => Session::STATUS_CLOSED]);
121
    }
122
123
    /**
124
     * Resync all the already opened sessions.
125
     *
126
     * @return void
127
     */
128
    public function resyncSessions()
129
    {
130
        $this->tenantService->round()->sessions()->opened()
131
            ->get()
132
            ->each(fn($session) => $this->syncSession($session));
133
    }
134
135
    /**
136
     * Update a session agenda.
137
     *
138
     * @param Session $session
139
     * @param string $agenda
140
     *
141
     * @return void
142
     */
143
    public function saveAgenda(Session $session, string $agenda): void
144
    {
145
        $session->update(['agenda' => $agenda]);
146
    }
147
148
    /**
149
     * Update a session report.
150
     *
151
     * @param Session $session
152
     * @param string $report
153
     *
154
     * @return void
155
     */
156
    public function saveReport(Session $session, string $report): void
157
    {
158
        $session->update(['report' => $report]);
159
    }
160
161
    /**
162
     * Find the unique receivable for a pool and a session.
163
     *
164
     * @param Pool $pool The pool
165
     * @param Session $session The session
166
     * @param int $receivableId
167
     * @param string $notes
168
     *
169
     * @return int
170
     */
171
    public function saveReceivableNotes(Pool $pool, Session $session, int $receivableId, string $notes): int
172
    {
173
        return $session->receivables()
174
            ->where('id', $receivableId)
175
            ->whereIn('subscription_id', $pool->subscriptions()->pluck('id'))
176
            ->update(['notes' => $notes]);
177
    }
178
}
179