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

Saving::createSaving()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Saving::updateSaving() 0 14 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->cl(Member::class)->rq()->home($fundId));
59
        $this->jq('#btn-savings-fund')->click($this->rq()->filter($fundId));
60
61
        return $this->page();
62
    }
63
64
    public function page(int $pageNumber = 0)
65
    {
66
        $fundId = (int)$this->bag('meeting.saving')->get('fund.id', -1);
67
        $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

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

68
        $savingSum = $this->savingService->getSavingSum(/** @scrutinizer ignore-type */ $this->session, $fundId);
Loading history...
69
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $savingCount,
70
            'meeting.saving', 'page');
71
        $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

71
        $savings = $this->savingService->getSavings(/** @scrutinizer ignore-type */ $this->session, $fundId, $pageNumber);
Loading history...
72
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $savingCount);
73
74
        $html = $this->render('pages.meeting.saving.page', [
75
            'session' => $this->session,
76
            'savings' => $savings,
77
            'savingCount' => $savingCount,
78
            'savingSum' => $savingSum,
79
            'pagination' => $pagination,
80
        ]);
81
        $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

81
        $this->response->html('meeting-savings-page', /** @scrutinizer ignore-type */ $html);
Loading history...
82
83
        $savingId = jq()->parent()->attr('data-saving-id')->toInt();
84
        $this->jq('.btn-saving-edit')->click($this->rq()->editSaving($savingId));
85
        $this->jq('.btn-saving-delete')->click($this->rq()->deleteSaving($savingId)
86
            ->confirm(trans('meeting.saving.questions.delete')));
87
88
        return $this->response;
89
    }
90
91
    public function filter(int $fundId)
92
    {
93
        $this->bag('meeting.saving')->set('fund.id', $fundId);
94
95
        return $this->page();
96
    }
97
98
    public function editSaving(int $savingId)
99
    {
100
        if($this->session->closed)
101
        {
102
            $this->notify->warning(trans('meeting.warnings.session.closed'));
103
            return $this->response;
104
        }
105
106
        $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

106
        $saving = $this->savingService->getSaving(/** @scrutinizer ignore-type */ $this->session, $savingId);
Loading history...
107
        $title = trans('meeting.saving.titles.edit');
108
        $content = $this->render('pages.meeting.saving.edit', [
109
            'saving' => $saving,
110
            'members' => $this->memberService->getMemberList(),
111
            'funds' => $this->fundService->getFundList(),
112
        ]);
113
        $buttons = [[
114
            'title' => trans('common.actions.cancel'),
115
            'class' => 'btn btn-tertiary',
116
            'click' => 'close',
117
        ],[
118
            'title' => trans('common.actions.save'),
119
            'class' => 'btn btn-primary',
120
            'click' => $this->rq()->updateSaving($savingId, pm()->form('saving-form')),
121
        ]];
122
        $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

122
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
123
124
        return $this->response;
125
    }
126
127
    /**
128
     * @di $validator
129
     * @after showBalanceAmounts
130
     */
131
    public function updateSaving(int $savingId, array $formValues)
132
    {
133
        if($this->session->closed)
134
        {
135
            $this->notify->warning(trans('meeting.warnings.session.closed'));
136
            return $this->response;
137
        }
138
139
        $values = $this->validator->validateItem($formValues);
140
        $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

140
        $this->savingService->updateSaving(/** @scrutinizer ignore-type */ $this->session, $savingId, $values);
Loading history...
141
142
        $this->dialog->hide();
143
144
        return $this->home();
145
    }
146
147
    /**
148
     * @after showBalanceAmounts
149
     */
150
    public function deleteSaving(int $savingId)
151
    {
152
        if($this->session->closed)
153
        {
154
            $this->notify->warning(trans('meeting.warnings.session.closed'));
155
            return $this->response;
156
        }
157
158
        $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

158
        $this->savingService->deleteSaving(/** @scrutinizer ignore-type */ $this->session, $savingId);
Loading history...
159
160
        return $this->home();
161
    }
162
}
163