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

Closing::deleteClosing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 14
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Saving;
4
5
use App\Ajax\CallableSessionClass;
6
use App\Ajax\Web\Report\Session\Saving;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Ajax\Web\Meeting\Saving\Saving. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Siak\Tontine\Model\Session as SessionModel;
8
use Siak\Tontine\Service\Meeting\Saving\SavingService;
9
use Siak\Tontine\Service\Tontine\FundService;
10
use Siak\Tontine\Validation\Meeting\ClosingValidator;
11
12
use function Jaxon\jq;
13
use function Jaxon\pm;
14
use function Jaxon\rq;
15
use function trans;
16
17
class Closing extends CallableSessionClass
18
{
19
    /**
20
     * @var ClosingValidator
21
     */
22
    protected ClosingValidator $validator;
23
24
    /**
25
     * The constructor
26
     *
27
     * @param SavingService $savingService
28
     * @param FundService $fundService
29
     */
30
    public function __construct(protected SavingService $savingService,
31
        protected FundService $fundService)
32
    {}
33
34
    /**
35
     * @exclude
36
     */
37
    public function show(SessionModel $session)
38
    {
39
        $this->session = $session;
40
41
        return $this->home();
42
    }
43
44
    /**
45
     * @databag report
46
     */
47
    public function home()
48
    {
49
        $html = $this->render('pages.meeting.closing.home', [
50
            'session' => $this->session,
51
            'closings' => $this->savingService->getSessionClosings($this->session),
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...e::getSessionClosings() 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

51
            'closings' => $this->savingService->getSessionClosings(/** @scrutinizer ignore-type */ $this->session),
Loading history...
52
            'funds' => $this->fundService->getFundList(),
53
        ]);
54
        $this->response->html('meeting-closings', $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-closings', /** @scrutinizer ignore-type */ $html);
Loading history...
55
        $this->response->call('makeTableResponsive', 'meeting-closings');
56
57
        // Sending an Ajax request to the Saving class needs to set
58
        // the session id in the report databag.
59
        $this->bag('report')->set('session.id', $this->session->id);
60
61
        $this->jq('#btn-closings-refresh')->click($this->rq()->home());
62
63
        $fundId = pm()->select('closings-fund-id')->toInt();
64
        $this->jq('#btn-closing-edit')->click($this->rq()->editClosing($fundId));
65
        $this->jq('#btn-fund-savings-show')->click($this->rq()->showSavings($fundId));
66
67
        $fundId = jq()->parent()->attr('data-fund-id')->toInt();
68
        $this->jq('.btn-closing-edit')->click($this->rq()->editClosing($fundId));
69
        $this->jq('.btn-fund-savings-show')->click($this->rq()->showSavings($fundId));
70
        $this->jq('.btn-closing-delete')->click($this->rq()->deleteClosing($fundId)
71
            ->confirm(trans('meeting.closing.questions.delete')));
72
73
        return $this->response;
74
    }
75
76
    /**
77
     * @databag report
78
     */
79
    public function showSavings(int $fundId)
80
    {
81
        $this->cl(Saving::class)->show($this->session, $fundId);
82
83
        $this->response->call('showSmScreen', 'report-fund-savings', 'session-savings');
84
        $this->jq('#btn-presence-sessions-back')->click(rq('.')
85
            ->showSmScreen('meeting-closings', 'session-savings'));
86
87
        return $this->response;
88
    }
89
90
    public function editClosing(int $fundId)
91
    {
92
        if($this->session->closed)
93
        {
94
            $this->notify->warning(trans('meeting.warnings.session.closed'));
95
            return $this->response;
96
        }
97
        $funds = $this->fundService->getFundList();
98
        if(!isset($funds[$fundId]))
99
        {
100
            return $this->response;
101
        }
102
103
        $title = trans('meeting.saving.titles.closing', ['fund' => $funds[$fundId]]);
104
        $content = $this->render('pages.meeting.closing.edit', [
105
            'hasClosing' => $this->savingService->hasFundClosing($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::hasFundClosing() 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

105
            'hasClosing' => $this->savingService->hasFundClosing(/** @scrutinizer ignore-type */ $this->session, $fundId),
Loading history...
106
            'profitAmount' => $this->savingService->getProfitAmount($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...vice::getProfitAmount() 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
            'profitAmount' => $this->savingService->getProfitAmount(/** @scrutinizer ignore-type */ $this->session, $fundId),
Loading history...
107
        ]);
108
        $buttons = [[
109
            'title' => trans('common.actions.close'),
110
            'class' => 'btn btn-tertiary',
111
            'click' => 'close',
112
        ], [
113
            'title' => trans('common.actions.save'),
114
            'class' => 'btn btn-primary',
115
            'click' => $this->rq()->saveClosing($fundId, pm()->form('closing-form')),
116
        ]];
117
        $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

117
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
118
119
        return $this->response;
120
    }
121
122
    /**
123
     * @di $validator
124
     */
125
    public function saveClosing(int $fundId, array $formValues)
126
    {
127
        $funds = $this->fundService->getFundList();
128
        if(!isset($funds[$fundId]))
129
        {
130
            return $this->response;
131
        }
132
133
        $values = $this->validator->validateItem($formValues);
134
        $this->savingService->saveFundClosing($this->session, $fundId, $values['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...vice::saveFundClosing() 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

134
        $this->savingService->saveFundClosing(/** @scrutinizer ignore-type */ $this->session, $fundId, $values['amount']);
Loading history...
135
136
        $this->dialog->hide();
137
        $this->notify->success(trans('meeting.messages.profit.saved'), trans('common.titles.success'));
138
139
        return $this->home();
140
    }
141
142
    public function deleteClosing(int $fundId)
143
    {
144
        $funds = $this->fundService->getFundList();
145
        if(!isset($funds[$fundId]))
146
        {
147
            return $this->response;
148
        }
149
150
        $this->savingService->deleteFundClosing($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...ce::deleteFundClosing() 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->deleteFundClosing(/** @scrutinizer ignore-type */ $this->session, $fundId);
Loading history...
151
152
        $this->dialog->hide();
153
        $this->notify->success(trans('meeting.messages.profit.deleted'), trans('common.titles.success'));
154
155
        return $this->home();
156
    }
157
}
158