Passed
Push — main ( 2e4b10...1f2a96 )
by Thierry
05:42
created

Member::toggleFilter()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Saving;
4
5
use App\Ajax\CallableSessionClass;
6
use Siak\Tontine\Service\LocaleService;
7
use Siak\Tontine\Service\Meeting\Saving\SavingService;
8
use Siak\Tontine\Service\Tontine\FundService;
9
use Siak\Tontine\Validation\Meeting\SavingValidator;
10
11
use function Jaxon\jq;
12
use function str_replace;
13
use function trans;
14
use function trim;
15
16
/**
17
 * @databag meeting.saving
18
 * @before getFundId
19
 */
20
class Member extends CallableSessionClass
21
{
22
    /**
23
     * @var LocaleService
24
     */
25
    protected LocaleService $localeService;
26
27
    /**
28
     * @var SavingValidator
29
     */
30
    protected SavingValidator $validator;
31
32
    /**
33
     * @var int
34
     */
35
    private $fundId = 0;
36
37
    /**
38
     * The constructor
39
     *
40
     * @param FundService $fundService
41
     * @param SavingService $savingService
42
     */
43
    public function __construct(private FundService $fundService,
44
        private SavingService $savingService)
45
    {}
46
47
    protected function getFundId()
48
    {
49
        $this->fundId = (int)($this->target()->method() === 'home' ?
50
            $this->target()->args()[0] :
51
            $this->bag('meeting.saving')->get('fund.id', 0));
52
    }
53
54
    /**
55
     * @param int $fundId
56
     *
57
     * @return mixed
58
     */
59
    public function home(int $fundId)
60
    {
61
        $fund = $fundId > 0 ? $this->fundService->getFund($fundId) : null;
62
        $fundId = !$fund ? 0 : $fund->id;
63
        $this->bag('meeting.saving')->set('fund.id', $fundId);
64
        $this->bag('meeting.saving')->set('member.filter', null);
65
66
        $html = $this->render('pages.meeting.saving.member.home', [
67
            'fund' => $fund,
68
        ]);
69
        $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

69
        $this->response->html('meeting-savings', /** @scrutinizer ignore-type */ $html);
Loading history...
70
        $this->jq('#btn-saving-back')->click($this->cl(Saving::class)->rq()->home());
71
        $this->jq('#btn-saving-filter')->click($this->rq()->toggleFilter());
72
73
        return $this->page(1);
74
    }
75
76
    /**
77
     * @param int $pageNumber
78
     *
79
     * @return mixed
80
     */
81
    public function page(int $pageNumber = 0)
82
    {
83
        $search = trim($this->bag('meeting.saving')->get('member.search', ''));
84
        $filter = $this->bag('meeting.saving')->get('member.filter', null);
85
        $memberCount = $this->savingService->getMemberCount($this->session, $this->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::getMemberCount() 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

85
        $memberCount = $this->savingService->getMemberCount(/** @scrutinizer ignore-type */ $this->session, $this->fundId,
Loading history...
86
            $search, $filter);
87
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $memberCount,
88
            'meeting', 'member.page');
89
        $members = $this->savingService->getMembers($this->session, $this->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...ngService::getMembers() 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

89
        $members = $this->savingService->getMembers(/** @scrutinizer ignore-type */ $this->session, $this->fundId,
Loading history...
90
            $search, $filter, $pageNumber);
91
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $memberCount);
92
93
        $savingCount = $this->savingService->getSavingCount($this->session, $this->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

93
        $savingCount = $this->savingService->getSavingCount(/** @scrutinizer ignore-type */ $this->session, $this->fundId);
Loading history...
94
        $savingSum = $this->savingService->getSavingSum($this->session, $this->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...Service::getSavingSum() 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

94
        $savingSum = $this->savingService->getSavingSum(/** @scrutinizer ignore-type */ $this->session, $this->fundId);
Loading history...
95
        $html = $this->render('pages.meeting.saving.member.page', [
96
            'search' => $search,
97
            'session' => $this->session,
98
            'members' => $members,
99
            'savingCount' => $savingCount,
100
            'savingSum' => $savingSum,
101
            'pagination' => $pagination,
102
        ]);
103
        $this->response->html('meeting-saving-members', $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

103
        $this->response->html('meeting-saving-members', /** @scrutinizer ignore-type */ $html);
Loading history...
104
105
        $memberId = jq()->parent()->attr('data-member-id')->toInt();
106
        $amount = jq('input', jq()->parent()->parent())->val()->toInt();
107
        $this->jq('.btn-save-saving')->click($this->rq()->saveSaving($memberId, $amount));
108
        $this->jq('.btn-edit-saving')->click($this->rq()->editSaving($memberId));
109
        $this->jq('#btn-saving-search')
110
            ->click($this->rq()->search(jq('#txt-fee-member-search')->val()));
111
112
        return $this->response;
113
    }
114
115
    public function search(string $search)
116
    {
117
        $this->bag('meeting.saving')->set('member.search', trim($search));
118
119
        return $this->page();
120
    }
121
122
    public function toggleFilter()
123
    {
124
        $filter = $this->bag('meeting.saving')->get('member.filter', null);
125
        // Switch between null, true and false
126
        $filter = $filter === null ? true : ($filter === true ? false : null);
127
        $this->bag('meeting.saving')->set('member.filter', $filter);
128
129
        return $this->page();
130
    }
131
132
    /**
133
     * @di $localeService
134
     * @param int $memberId
135
     *
136
     * @return mixed
137
     */
138
    public function editSaving(int $memberId)
139
    {
140
        if($this->session->closed)
141
        {
142
            $this->notify->warning(trans('meeting.warnings.session.closed'));
143
            return $this->response;
144
        }
145
        $saving = $this->savingService->findSaving($this->session, $this->fundId, $memberId);
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::findSaving() 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

145
        $saving = $this->savingService->findSaving(/** @scrutinizer ignore-type */ $this->session, $this->fundId, $memberId);
Loading history...
146
        $amount = !$saving ? '' : $this->localeService->getMoneyValue($saving->amount);
147
148
        $html = $this->render('pages.meeting.saving.member.edit', [
149
            'memberId' => $memberId,
150
            'amount' => $amount,
151
        ]);
152
        $fieldId = 'saving-member-' . $memberId;
153
        $this->response->html($fieldId, $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

153
        $this->response->html($fieldId, /** @scrutinizer ignore-type */ $html);
Loading history...
154
155
        $memberId = jq()->parent()->attr('data-member-id')->toInt();
156
        $amount = jq('input', jq()->parent()->parent())->val();
157
        $this->jq('.btn-save-saving', "#$fieldId")->click($this->rq()->saveSaving($memberId, $amount));
158
159
        return $this->response;
160
    }
161
162
    /**
163
     * @di $validator
164
     * @after showBalanceAmounts
165
     *
166
     * @param int $memberId
167
     * @param string $amount
168
     *
169
     * @return mixed
170
     */
171
    public function saveSaving(int $memberId, string $amount)
172
    {
173
        if($this->session->closed)
174
        {
175
            $this->notify->warning(trans('meeting.warnings.session.closed'));
176
            return $this->response;
177
        }
178
179
        $amount = str_replace(',', '.', trim($amount));
180
        if($amount === '')
181
        {
182
            $this->savingService->deleteSaving($this->session, 0, $memberId);
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

182
            $this->savingService->deleteSaving(/** @scrutinizer ignore-type */ $this->session, 0, $memberId);
Loading history...
183
184
            $this->notify->success(trans('meeting.messages.deleted'));
185
            return $this->page();
186
        }
187
188
        $values = ['member' => $memberId, 'amount' => $amount, 'fund' => $this->fundId];
189
        $values = $this->validator->validateItem($values);
190
        $amount = $values['amount'];
191
        $this->savingService->saveSaving($this->session, $this->fundId, $memberId, $amount);
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::saveSaving() 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

191
        $this->savingService->saveSaving(/** @scrutinizer ignore-type */ $this->session, $this->fundId, $memberId, $amount);
Loading history...
192
193
        $this->notify->success(trans('meeting.messages.saved'));
194
        return $this->page();
195
    }
196
}
197