Passed
Pull Request — main (#57)
by Thierry
15:28 queued 13s
created

Saving   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 5 1
A home() 0 15 1
A __construct() 0 3 1
A page() 0 25 1
A showTotal() 0 8 1
A updateSaving() 0 14 2
A editSaving() 0 27 2
A filter() 0 5 1
A deleteSaving() 0 11 2
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Saving;
4
5
use App\Ajax\CallableSessionClass;
6
use Siak\Tontine\Model\Session as SessionModel;
7
use Siak\Tontine\Service\Meeting\Saving\SavingService;
8
use Siak\Tontine\Service\Tontine\FundService;
9
use Siak\Tontine\Service\Tontine\MemberService;
10
use Siak\Tontine\Validation\Meeting\SavingValidator;
11
12
use function Jaxon\jq;
13
use function Jaxon\pm;
14
use function trans;
15
16
/**
17
 * @databag meeting.saving
18
 */
19
class Saving extends CallableSessionClass
20
{
21
    /**
22
     * @var SavingValidator
23
     */
24
    protected SavingValidator $validator;
25
26
    /**
27
     * The constructor
28
     *
29
     * @param SavingService $savingService
30
     * @param FundService $fundService
31
     * @param MemberService $memberService
32
     */
33
    public function __construct(protected SavingService $savingService,
34
        protected FundService $fundService, protected MemberService $memberService)
35
    {}
36
37
    /**
38
     * @exclude
39
     */
40
    public function show(SessionModel $session)
41
    {
42
        $this->session = $session;
43
44
        return $this->home();
45
    }
46
47
    public function home()
48
    {
49
        $html = $this->render('pages.meeting.saving.home', [
50
            'session' => $this->session,
51
            'fundId' => (int)$this->bag('meeting.saving')->get('fund.id', -1),
52
            'funds' => $this->fundService->getFundList()->prepend('', -1),
53
        ]);
54
        $this->response->html('meeting-savings', $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

54
        $this->response->html('meeting-savings', /** @scrutinizer ignore-type */ $html);
Loading history...
55
56
        $this->jq('#btn-savings-refresh')->click($this->rq()->home());
57
        $fundId = pm()->select('savings-fund-id')->toInt();
58
        $this->jq('#btn-savings-edit')->click($this->rq(Member::class)->home($fundId));
59
        $this->jq('#btn-savings-fund')->click($this->rq()->filter($fundId));
60
61
        return $this->page();
62
    }
63
64
    private function showTotal(int $fundId, int $savingCount)
65
    {
66
        $savingTotal = $this->savingService->getSavingTotal($this->session, $fundId);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...rvice::getSavingTotal() does only seem to accept Siak\Tontine\Model\Session, 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

66
        $savingTotal = $this->savingService->getSavingTotal(/** @scrutinizer ignore-type */ $this->session, $fundId);
Loading history...
67
        $html = $this->render('pages.meeting.saving.total', [
68
            'savingCount' => $savingCount,
69
            'savingTotal' => $savingTotal,
70
        ]);
71
        $this->response->html('meeting-savings-total', $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

71
        $this->response->html('meeting-savings-total', /** @scrutinizer ignore-type */ $html);
Loading history...
72
    }
73
74
    public function page(int $pageNumber = 0)
75
    {
76
        $fundId = (int)$this->bag('meeting.saving')->get('fund.id', -1);
77
        $savingCount = $this->savingService->getSavingCount($this->session, $fundId);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...rvice::getSavingCount() does only seem to accept Siak\Tontine\Model\Session, 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

77
        $savingCount = $this->savingService->getSavingCount(/** @scrutinizer ignore-type */ $this->session, $fundId);
Loading history...
78
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $savingCount,
79
            'meeting.saving', 'page');
80
        $savings = $this->savingService->getSavings($this->session, $fundId, $pageNumber);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...ngService::getSavings() does only seem to accept Siak\Tontine\Model\Session, 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
        $savings = $this->savingService->getSavings(/** @scrutinizer ignore-type */ $this->session, $fundId, $pageNumber);
Loading history...
81
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $savingCount);
82
83
        $this->showTotal($fundId, $savingCount);
84
85
        $html = $this->render('pages.meeting.saving.page', [
86
            'session' => $this->session,
87
            'savings' => $savings,
88
            'pagination' => $pagination,
89
        ]);
90
        $this->response->html('meeting-savings-page', $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

90
        $this->response->html('meeting-savings-page', /** @scrutinizer ignore-type */ $html);
Loading history...
91
        $this->response->call('makeTableResponsive', 'meeting-savings-page');
92
93
        $savingId = jq()->parent()->attr('data-saving-id')->toInt();
94
        $this->jq('.btn-saving-edit')->click($this->rq()->editSaving($savingId));
95
        $this->jq('.btn-saving-delete')->click($this->rq()->deleteSaving($savingId)
96
            ->confirm(trans('meeting.saving.questions.delete')));
97
98
        return $this->response;
99
    }
100
101
    public function filter(int $fundId)
102
    {
103
        $this->bag('meeting.saving')->set('fund.id', $fundId);
104
105
        return $this->page();
106
    }
107
108
    public function editSaving(int $savingId)
109
    {
110
        if($this->session->closed)
111
        {
112
            $this->notify->warning(trans('meeting.warnings.session.closed'));
113
            return $this->response;
114
        }
115
116
        $saving = $this->savingService->getSaving($this->session, $savingId);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...ingService::getSaving() does only seem to accept Siak\Tontine\Model\Session, 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

116
        $saving = $this->savingService->getSaving(/** @scrutinizer ignore-type */ $this->session, $savingId);
Loading history...
117
        $title = trans('meeting.saving.titles.edit');
118
        $content = $this->render('pages.meeting.saving.edit', [
119
            'saving' => $saving,
120
            'members' => $this->memberService->getMemberList(),
121
            'funds' => $this->fundService->getFundList(),
122
        ]);
123
        $buttons = [[
124
            'title' => trans('common.actions.cancel'),
125
            'class' => 'btn btn-tertiary',
126
            'click' => 'close',
127
        ],[
128
            'title' => trans('common.actions.save'),
129
            'class' => 'btn btn-primary',
130
            'click' => $this->rq()->updateSaving($savingId, pm()->form('saving-form')),
131
        ]];
132
        $this->dialog->show($title, $content, $buttons);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $sContent of Jaxon\App\Dialog\ModalInterface::show() 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

132
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
133
134
        return $this->response;
135
    }
136
137
    /**
138
     * @di $validator
139
     * @after showBalanceAmounts
140
     */
141
    public function updateSaving(int $savingId, array $formValues)
142
    {
143
        if($this->session->closed)
144
        {
145
            $this->notify->warning(trans('meeting.warnings.session.closed'));
146
            return $this->response;
147
        }
148
149
        $values = $this->validator->validateItem($formValues);
150
        $this->savingService->updateSaving($this->session, $savingId, $values);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...Service::updateSaving() does only seem to accept Siak\Tontine\Model\Session, 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

150
        $this->savingService->updateSaving(/** @scrutinizer ignore-type */ $this->session, $savingId, $values);
Loading history...
151
152
        $this->dialog->hide();
153
154
        return $this->home();
155
    }
156
157
    /**
158
     * @after showBalanceAmounts
159
     */
160
    public function deleteSaving(int $savingId)
161
    {
162
        if($this->session->closed)
163
        {
164
            $this->notify->warning(trans('meeting.warnings.session.closed'));
165
            return $this->response;
166
        }
167
168
        $this->savingService->deleteSaving($this->session, $savingId);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...Service::deleteSaving() does only seem to accept Siak\Tontine\Model\Session, 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

168
        $this->savingService->deleteSaving(/** @scrutinizer ignore-type */ $this->session, $savingId);
Loading history...
169
170
        return $this->home();
171
    }
172
}
173