Passed
Push — main ( 6ca2bb...bc39e3 )
by Thierry
05:43
created

Home::pool()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 21
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Planning\Subscription;
4
5
use App\Ajax\CallableClass;
6
use Siak\Tontine\Model\Pool as PoolModel;
7
use Siak\Tontine\Service\LocaleService;
8
use Siak\Tontine\Service\Planning\PoolService;
9
use Siak\Tontine\Service\Planning\SubscriptionService;
10
use Siak\Tontine\Service\Planning\SummaryService;
11
12
use function Jaxon\jq;
13
use function Jaxon\pm;
14
use function trans;
15
16
/**
17
 * @databag subscription
18
 * @before getPool
19
 */
20
class Home extends CallableClass
21
{
22
    /**
23
     * @di
24
     * @var LocaleService
25
     */
26
    protected LocaleService $localeService;
27
28
    /**
29
     * @di
30
     * @var PoolService
31
     */
32
    protected PoolService $poolService;
33
34
    /**
35
     * @var SummaryService
36
     */
37
    public SummaryService $summaryService;
38
39
    /**
40
     * @var SubscriptionService
41
     */
42
    public SubscriptionService $subscriptionService;
43
44
    /**
45
     * @var PoolModel|null
46
     */
47
    protected ?PoolModel $pool = null;
48
49
    /**
50
     * @return void
51
     */
52
    protected function getPool()
53
    {
54
        if($this->target()->method() === 'home')
55
        {
56
            return;
57
        }
58
59
        $poolId = $this->target()->method() === 'pool' ? $this->target()->args()[0] :
60
            intval($this->bag('subscription')->get('pool.id'));
61
        $this->pool = $this->poolService->getPool($poolId);
62
    }
63
64
    /**
65
     * @before checkGuestAccess ["planning", "subscriptions"]
66
     * @before checkRoundPools
67
     * @after hideMenuOnMobile
68
     */
69
    public function home()
70
    {
71
        $html = $this->renderView('pages.planning.subscription.home');
72
        $this->response->html('section-title', trans('tontine.menus.planning'));
73
        $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

73
        $this->response->html('content-home', /** @scrutinizer ignore-type */ $html);
Loading history...
74
75
        $poolId = intval($this->bag('subscription')->get('pool.id', 0));
76
        $pools = $this->poolService->getRoundPools();
77
        $pool = $pools->firstWhere('id', $poolId) ?? ($pools->count() > 0 ? $pools[0] : null);
78
        if($pool === null)
79
        {
80
            return $this->response;
81
        }
82
83
        $this->pool = $pool;
84
        $this->response->call('setSmScreenHandler', 'pool-subscription-sm-screens');
85
86
        return $this->pool();
87
    }
88
89
    public function pool(int $poolId = 0)
90
    {
91
        if($this->pool === null)
92
        {
93
            return $this->response;
94
        }
95
96
        $this->bag('subscription')->set('pool.id', $this->pool->id);
97
98
        $this->cl(Member::class)->show($this->pool);
99
        $this->cl(Session::class)->show($this->pool);
100
101
        if($poolId > 0)
102
        {
103
            $message = trans('tontine.pool.messages.selected', [
104
                'tontine' => $this->pool->title,
105
            ]);
106
            $this->response->dialog->info($message, trans('common.titles.info'));
0 ignored issues
show
Bug introduced by
The method info() does not exist on Jaxon\Plugin\ResponsePlugin. It seems like you code against a sub-type of Jaxon\Plugin\ResponsePlugin such as Jaxon\Plugin\Response\Dialog\DialogPlugin. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            $this->response->dialog->/** @scrutinizer ignore-call */ 
107
                                     info($message, trans('common.titles.info'));
Loading history...
Bug introduced by
The method info() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            $this->response->dialog->/** @scrutinizer ignore-call */ 
107
                                     info($message, trans('common.titles.info'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        }
108
109
        return $this->response;
110
    }
111
112
    /**
113
     * @di $summaryService
114
     */
115
    public function planning()
116
    {
117
        if(!$this->pool || !$this->pool->remit_planned)
118
        {
119
            return $this->response;
120
        }
121
122
        $receivables = $this->summaryService->getReceivables($this->pool);
123
        $this->view()->shareValues($receivables);
124
        $html = $this->renderView('pages.planning.subscription.planning', [
125
            'pool' => $this->pool,
126
        ]);
127
        $this->response->html('content-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

127
        $this->response->html('content-page', /** @scrutinizer ignore-type */ $html);
Loading history...
128
        $this->response->call('makeTableResponsive', 'content-page');
129
130
        $this->jq('#btn-subscription-beneficiaries')->click($this->rq()->beneficiaries());
131
        $this->jq('#btn-subscription-refresh')->click($this->rq()->planning());
132
        $this->jq('#btn-subscription-back')->click($this->rq()->home());
133
134
        return $this->response;
135
    }
136
137
    /**
138
     * @di $summaryService
139
     * @di $subscriptionService
140
     */
141
    public function beneficiaries()
142
    {
143
        if(!$this->pool || !$this->pool->remit_planned)
144
        {
145
            return $this->response;
146
        }
147
148
        $this->response->html('section-title', trans('tontine.menus.planning'));
149
        $payables = $this->summaryService->getPayables($this->pool);
150
        $this->view()->shareValues($payables);
151
        $html = $this->renderView('pages.planning.subscription.beneficiaries', [
152
            'pool' => $this->pool,
153
            'pools' => $this->subscriptionService->getPools(),
154
        ]);
155
        $this->response->html('content-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

155
        $this->response->html('content-page', /** @scrutinizer ignore-type */ $html);
Loading history...
156
        $this->response->call('makeTableResponsive', 'content-page');
157
158
        $this->jq('#btn-subscription-planning')->click($this->rq()->planning());
159
        $this->jq('#btn-pool-select')->click($this->rq()->select(pm()->select('select-pool')->toInt()));
160
        $this->jq('#btn-subscription-refresh')->click($this->rq()->beneficiaries());
161
        $this->jq('#btn-subscription-back')->click($this->rq()->home());
162
        $this->jq('.select-beneficiary')->change($this->rq()
163
            ->saveBeneficiary(jq()->attr('data-session-id')->toInt(), jq()->val()->toInt(),
164
                jq()->attr('data-subscription-id')->toInt()));
165
166
        return $this->response;
167
    }
168
169
    /**
170
     * @di $summaryService
171
     * @di $subscriptionService
172
     */
173
    public function saveBeneficiary(int $sessionId, int $nextSubscriptionId, int $currSubscriptionId)
174
    {
175
        if(!$this->pool || !$this->pool->remit_planned || $this->pool->remit_auction)
176
        {
177
            return $this->response;
178
        }
179
180
        if(!$this->subscriptionService->saveBeneficiary($this->pool, $sessionId,
181
            $currSubscriptionId, $nextSubscriptionId))
182
        {
183
            $message = trans('tontine.beneficiary.errors.cant_change');
184
            $this->response->dialog->error($message, trans('common.titles.error'));
0 ignored issues
show
Bug introduced by
The method error() does not exist on Jaxon\Plugin\ResponsePlugin. It seems like you code against a sub-type of Jaxon\Plugin\ResponsePlugin such as Jaxon\Plugin\Response\Dialog\DialogPlugin. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
            $this->response->dialog->/** @scrutinizer ignore-call */ 
185
                                     error($message, trans('common.titles.error'));
Loading history...
185
        }
186
187
        return $this->beneficiaries();
188
    }
189
}
190