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

SessionService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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 an already opened session.
125
     *
126
     * @param Session $session
127
     *
128
     * @return void
129
     */
130
    public function resyncSession(Session $session)
131
    {
132
        if($session->opened)
133
        {
134
            $this->syncSession($session);
135
        }
136
    }
137
138
    /**
139
     * Update a session agenda.
140
     *
141
     * @param Session $session
142
     * @param string $agenda
143
     *
144
     * @return void
145
     */
146
    public function saveAgenda(Session $session, string $agenda): void
147
    {
148
        $session->update(['agenda' => $agenda]);
149
    }
150
151
    /**
152
     * Update a session report.
153
     *
154
     * @param Session $session
155
     * @param string $report
156
     *
157
     * @return void
158
     */
159
    public function saveReport(Session $session, string $report): void
160
    {
161
        $session->update(['report' => $report]);
162
    }
163
164
    /**
165
     * Find the unique receivable for a pool and a session.
166
     *
167
     * @param Pool $pool The pool
168
     * @param Session $session The session
169
     * @param int $receivableId
170
     * @param string $notes
171
     *
172
     * @return int
173
     */
174
    public function saveReceivableNotes(Pool $pool, Session $session, int $receivableId, string $notes): int
175
    {
176
        return $session->receivables()
177
            ->where('id', $receivableId)
178
            ->whereIn('subscription_id', $pool->subscriptions()->pluck('id'))
179
            ->update(['notes' => $notes]);
180
    }
181
}
182