Passed
Push — main ( 1c458a...5a987a )
by Thierry
10:56 queued 05:26
created

SessionService::chechSessionDates()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 11
rs 9.6111
1
<?php
2
3
namespace Siak\Tontine\Service\Guild;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\DB;
7
use Siak\Tontine\Exception\MessageException;
8
use Siak\Tontine\Model\Guild;
9
use Siak\Tontine\Model\Round;
10
use Siak\Tontine\Model\Session;
11
use Siak\Tontine\Service\Planning\BillSyncService;
12
use Siak\Tontine\Service\Planning\FundSyncService;
13
use Siak\Tontine\Service\Planning\PoolSyncService;
14
use stdClass;
15
16
use function now;
17
use function trans;
18
19
class SessionService
20
{
21
    /**
22
     * @param BillSyncService $billSyncService
23
     * @param PoolSyncService $poolSyncService
24
     * @param FundSyncService $fundSyncService
25
     */
26
    public function __construct(private BillSyncService $billSyncService,
27
        private PoolSyncService $poolSyncService, private FundSyncService $fundSyncService)
28
    {}
29
30
    /**
31
     * Get the session statuses
32
     *
33
     * @return array
34
     */
35
    public function getSessionStatuses(): array
36
    {
37
        return [
38
            Session::STATUS_PENDING => trans('tontine.session.status.pending'),
39
            Session::STATUS_OPENED => trans('tontine.session.status.opened'),
40
            Session::STATUS_CLOSED => trans('tontine.session.status.closed'),
41
        ];
42
    }
43
44
    /**
45
     * Get a session.
46
     *
47
     * @param Guild $guild
48
     * @param int $sessionId
49
     *
50
     * @return Session|null
51
     */
52
    public function getSession(Guild $guild, int $sessionId): ?Session
53
    {
54
        return $guild->sessions()->find($sessionId);
55
    }
56
57
    /**
58
     * Add a new session.
59
     *
60
     * @param Round $round
61
     * @param array $values
62
     *
63
     * @return bool
64
     */
65
    public function createSession(Round $round, array $values): bool
66
    {
67
        DB::transaction(function() use($round, $values) {
68
            /** @var Session */
69
            $session = $round->sessions()->create($values);
70
71
            $this->billSyncService->sessionsCreated($round, [$session]);
72
            $this->poolSyncService->sessionsCreated($round, [$session]);
73
            $this->fundSyncService->sessionCreated($round, [$session]);
74
        });
75
        return true;
76
    }
77
78
    /**
79
     * Add a new session.
80
     *
81
     * @param Round $round
82
     * @param array $values
83
     *
84
     * @return bool
85
     */
86
    public function createSessions(Round $round, array $values): bool
87
    {
88
        DB::transaction(function() use($round, $values) {
89
            $sessions = $round->sessions()->createMany($values);
90
91
            $this->billSyncService->sessionsCreated($round, $sessions);
92
            $this->poolSyncService->sessionsCreated($round, $sessions);
93
            $this->fundSyncService->sessionCreated($round, $sessions);
94
        });
95
        return true;
96
    }
97
98
    /**
99
     * Find the prev session.
100
     *
101
     * @param Guild $guild
102
     * @param Session $session
103
     *
104
     * @return Session|null
105
     */
106
    private function getPrevSession(Guild $guild, Session $session): ?Session
107
    {
108
        return $guild->sessions()
109
            ->where('day_date', '<', $session->day_date)
110
            ->orderBy('day_date', 'desc')
111
            ->first();
112
    }
113
114
    /**
115
     * Find the next session.
116
     *
117
     * @param Guild $guild
118
     * @param Session $session
119
     *
120
     * @return Session|null
121
     */
122
    private function getNextSession(Guild $guild, Session $session): ?Session
123
    {
124
        return $guild->sessions()
125
            ->where('day_date', '>', $session->day_date)
126
            ->orderBy('day_date', 'asc')
127
            ->first();
128
    }
129
130
    /**
131
     * Called before a session is updated
132
     *
133
     * @param Guild $guild
134
     * @param Session $session
135
     * @param array $values
136
     *
137
     * @return void
138
     */
139
    private function chechSessionDates(Guild $guild, Session $session, array $values): void
140
    {
141
        // Check that the sessions date sorting is not modified.
142
        $date = Carbon::createFromFormat('Y-m-d', $values['day_date']);
143
        $prevSession = $this->getPrevSession($guild, $session);
144
        $nextSession = $this->getNextSession($guild, $session);
145
        if(($prevSession !== null && $prevSession->day_date->gte($date)) ||
146
            ($nextSession !== null && $nextSession->day_date->lte($date)))
147
        {
148
            throw new MessageException(trans('tontine.errors.action') .
149
                '<br/>' . trans('tontine.session.errors.sorting'));
150
        }
151
    }
152
153
    /**
154
     * Update a session.
155
     *
156
     * @param Guild $guild
157
     * @param Session $session
158
     * @param array $values
159
     *
160
     * @return void
161
     */
162
    public function updateSession(Guild $guild, Session $session, array $values): void
163
    {
164
        $this->chechSessionDates($guild, $session, $values);
165
166
        DB::transaction(function() use($session, $values) {
167
            $session->update($values);
168
169
            // Not necessary
170
            // $this->fundSyncService->sessionUpdated($session->round);
171
        });
172
    }
173
174
    /**
175
     * Update a session,venue.
176
     *
177
     * @param Session $session
178
     * @param array $values
179
     *
180
     * @return bool
181
     */
182
    public function saveSessionVenue(Session $session, array $values): bool
183
    {
184
        return $session->update($values);
185
    }
186
187
    /**
188
     * Delete a session.
189
     *
190
     * @param Session $session
191
     *
192
     * @return void
193
     */
194
    public function deleteSession(Session $session)
195
    {
196
        DB::transaction(function() use($session) {
197
            $this->billSyncService->sessionDeleted($session);
198
            $this->poolSyncService->sessionDeleted($session);
199
            $this->fundSyncService->sessionDeleted($session);
200
201
            $session->delete();
202
        });
203
    }
204
205
    /**
206
     * @return array
207
     */
208
    public function getYearSessions(): array
209
    {
210
        $year = now()->format('Y');
211
        $date = Carbon::createFromDate($year, 1, 5, 'Africa/Douala');
212
        $sessions = [];
213
        for($i = 0; $i < 12; $i++)
214
        {
215
            $session = new stdClass();
216
            $session->title = trans('tontine.session.labels.title', [
217
                'date' => $date->translatedFormat(trans('tontine.date.format_my')),
218
            ]);
219
            $session->date = $date->format('Y-m-d');
220
            $session->start = '00:00';
221
            $session->end = '00:00';
222
            $sessions[] = $session;
223
            $date->addMonth(1);
224
        }
225
        return $sessions;
226
    }
227
}
228