Passed
Push — main ( 936852...10149e )
by Thierry
15:09
created

PoolRound::saveRound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Planning;
4
5
use App\Ajax\CallableClass;
6
use App\Ajax\Web\Planning\Pool;
7
use Siak\Tontine\Model\Pool as PoolModel;
8
use Siak\Tontine\Model\Session as SessionModel;
9
use Siak\Tontine\Service\Planning\SessionService;
10
use Siak\Tontine\Service\Planning\PoolService;
11
use Siak\Tontine\Validation\Planning\PoolRoundValidator;
12
13
use function Jaxon\pm;
14
use function trans;
15
16
/**
17
 * @databag pool.round
18
 * @before getPool
19
 */
20
class PoolRound extends CallableClass
21
{
22
    /**
23
     * @var PoolRoundValidator
24
     */
25
    protected PoolRoundValidator $validator;
26
27
    /**
28
     * @var PoolModel|null
29
     */
30
    protected ?PoolModel $pool = null;
31
32
    /**
33
     * The constructor
34
     *
35
     * @param SessionService $sessionService
36
     * @param PoolService $poolService
37
     */
38
    public function __construct(private SessionService $sessionService,
39
        private PoolService $poolService)
40
    {}
41
42
    /**
43
     * @return void
44
     */
45
    protected function getPool()
46
    {
47
        $poolId = $this->target()->method() === 'home' ? $this->target()->args()[0] :
48
            (int)$this->bag('pool.round')->get('pool.id');
49
        $this->pool = $this->poolService->getPool($poolId);
50
    }
51
52
    private function showTitles()
53
    {
54
        $html = $this->render('pages.planning.pool.round.actions', [
55
            'pool' => $this->pool,
56
        ]);
57
        $this->response->html('pool-round-actions', $html);
0 ignored issues
show
Bug introduced by
It seems like $html can also be of type null; however, parameter $sData of Jaxon\Response\Response::html() does only seem to accept string, 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

57
        $this->response->html('pool-round-actions', /** @scrutinizer ignore-type */ $html);
Loading history...
58
        $this->jq('#btn-pool-round-back')->click($this->cl(Pool::class)->rq()->home());
59
        $this->jq('#btn-pool-round-save')->click($this->rq()->saveRound(pm()->form('round-form')));
60
        if($this->pool->pool_round)
61
        {
62
            $this->jq('#btn-pool-round-delete')->click($this->rq()->deleteRound()
63
                ->confirm(trans('tontine.pool_round.questions.delete')));
64
        }
65
66
        $defaultLabel = trans('tontine.pool_round.labels.default');
67
        $startSession = !$this->pool->pool_round ? $defaultLabel : $this->pool->start_date;
68
        $endSession =  !$this->pool->pool_round ? $defaultLabel : $this->pool->end_date;
69
        $this->response->html('pool-round-start-session-title',
70
            trans('tontine.pool_round.titles.start_session', ['session' => $startSession]));
71
        $this->response->html('pool-round-end-session-title',
72
            trans('tontine.pool_round.titles.end_session', ['session' => $endSession]));
73
    }
74
75
    private function getSessionPageNumber(SessionModel $session): int
76
    {
77
        $sessionCount = $this->sessionService->getTontineSessionCount($session, true, false);
78
        return (int)($sessionCount / $this->tenantService->getLimit()) + 1;
79
    }
80
81
    public function home(int $poolId)
82
    {
83
        if(!$this->pool)
84
        {
85
            return $this->response;
86
        }
87
88
        $html = $this->render('pages.planning.pool.round.home', [
89
            'pool' => $this->pool,
90
        ]);
91
        $this->response->html('content-home', $html);
0 ignored issues
show
Bug introduced by
It seems like $html can also be of type null; however, parameter $sData of Jaxon\Response\Response::html() does only seem to accept string, 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

91
        $this->response->html('content-home', /** @scrutinizer ignore-type */ $html);
Loading history...
92
        $this->showTitles();
93
        $this->jq('#btn-show-start-session-page')->click($this->rq()->showStartSessionPage());
94
        $this->jq('#btn-show-end-session-page')->click($this->rq()->showEndSessionPage());
95
96
        $this->bag('pool.round')->set('pool.id', $poolId);
97
98
        $startPageNumber = 1;
99
        $endPageNumber = 1;
100
        if($this->pool->pool_round !== null)
101
        {
102
            // Go to the pages of the round start and end sessions.
103
            $startPageNumber = $this->getSessionPageNumber($this->pool->pool_round->start_session);
104
            $endPageNumber = $this->getSessionPageNumber($this->pool->pool_round->end_session);
105
        }
106
        $this->showStartSession($startPageNumber);
107
        $this->showEndSession($endPageNumber);
108
109
        return $this->response;
110
    }
111
112
    /**
113
     * @di $validator
114
     */
115
    public function saveRound(array $formValues)
116
    {
117
        $values = $this->validator->validateItem($formValues);
118
        $this->poolService->saveRound($this->pool, $values);
0 ignored issues
show
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Pla...oolService::saveRound() does only seem to accept Siak\Tontine\Model\Pool, 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

118
        $this->poolService->saveRound(/** @scrutinizer ignore-type */ $this->pool, $values);
Loading history...
119
120
        // Reload the pool
121
        $this->getPool();
122
        $this->showTitles();
123
        $this->notify->info(trans('tontine.pool_round.messages.saved'));
124
125
        return $this->response;
126
    }
127
128
    public function deleteRound()
129
    {
130
        $this->poolService->deleteRound($this->pool);
0 ignored issues
show
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Pla...lService::deleteRound() does only seem to accept Siak\Tontine\Model\Pool, 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

130
        $this->poolService->deleteRound(/** @scrutinizer ignore-type */ $this->pool);
Loading history...
131
132
        // Reload the pool
133
        $this->getPool();
134
        $this->home($this->pool->id);
135
        $this->notify->info(trans('tontine.pool_round.messages.deleted'));
136
137
        return $this->response;
138
    }
139
140
    private function showSession($request, int $sessionId, int $pageNumber, string $field)
141
    {
142
        $sessionCount = $this->sessionService->getTontineSessionCount();
143
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $sessionCount,
144
            'pool.round', "session.$field.page");
145
        $sessions = $this->sessionService->getTontineSessions($pageNumber, false);
146
        $pagination = $request->paginate($pageNumber, $perPage, $sessionCount);
147
148
        $html = $this->render('pages.planning.pool.round.sessions', [
149
            'field' => $field,
150
            'sessions' => $sessions,
151
            'sessionId' => $sessionId,
152
            'pagination' => $pagination,
153
        ]);
154
        $this->response->html("pool-round-sessions-$field", $html);
0 ignored issues
show
Bug introduced by
It seems like $html can also be of type null; however, parameter $sData of Jaxon\Response\Response::html() does only seem to accept string, 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

154
        $this->response->html("pool-round-sessions-$field", /** @scrutinizer ignore-type */ $html);
Loading history...
155
156
        return $this->response;
157
    }
158
159
    public function showStartSession(int $pageNumber = 0)
160
    {
161
        $sessionId = $this->pool->pool_round ? $this->pool->pool_round->start_session_id : 0;
162
        $request = $this->rq()->showStartSession();
163
        return $this->showSession($request, $sessionId, $pageNumber, 'start');
164
    }
165
166
    public function showEndSession(int $pageNumber = 0)
167
    {
168
        $sessionId = $this->pool->pool_round ? $this->pool->pool_round->end_session_id : 0;
169
        $request = $this->rq()->showEndSession();
170
        return $this->showSession($request, $sessionId, $pageNumber, 'end');
171
    }
172
173
    public function showStartSessionPage()
174
    {
175
        if(!$this->pool->pool_round)
176
        {
177
            return $this->response;
178
        }
179
180
        $sessionId = $this->pool->pool_round->start_session_id;
181
        $pageNumber = $this->getSessionPageNumber($this->pool->pool_round->start_session);
182
        $request = $this->rq()->showStartSession();
183
        return $this->showSession($request, $sessionId, $pageNumber, 'start');
184
    }
185
186
    public function showEndSessionPage()
187
    {
188
        if(!$this->pool->pool_round)
189
        {
190
            return $this->response;
191
        }
192
193
        $sessionId = $this->pool->pool_round->end_session_id;
194
        $pageNumber = $this->getSessionPageNumber($this->pool->pool_round->end_session);
195
        $request = $this->rq()->showEndSession();
196
        return $this->showSession($request, $sessionId, $pageNumber, 'end');
197
    }
198
}
199