Passed
Push — main ( 936852...10149e )
by Thierry
15:09
created

CallableClass   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 163
rs 10
c 4
b 0
f 0
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A checkRoundPools() 0 8 3
A hideMenuOnMobile() 0 5 1
A selectRound() 0 14 1
A setTenantService() 0 4 1
A selectTontine() 0 13 1
A checkOpenedSessions() 0 13 6
A pageNumber() 0 16 4
A render() 0 3 1
A checkRoundSessions() 0 6 3
1
<?php
2
3
namespace App\Ajax;
4
5
use App\Ajax\Web\Meeting\Session as MeetingSession;
6
use App\Ajax\Web\Planning\Pool;
7
use App\Ajax\Web\Planning\Round as PlanningRound;
8
use App\Ajax\Web\Planning\Subscription;
9
use App\Ajax\Web\Report\Round as RoundReport;
10
use App\Ajax\Web\Report\Session as SessionReport;
11
use App\Ajax\Web\Tontine\Member;
12
use App\Ajax\Web\Tontine\Options;
13
use Siak\Tontine\Model\Round;
14
use Siak\Tontine\Model\Tontine;
15
use Jaxon\App\CallableClass as JaxonCallableClass;
16
use Jaxon\App\Dialog\MessageInterface;
17
use Jaxon\App\Dialog\ModalInterface;
18
use Jaxon\App\View\Store;
19
use Siak\Tontine\Exception\PlanningPoolException;
20
use Siak\Tontine\Exception\PlanningRoundException;
21
use Siak\Tontine\Exception\TontineMemberException;
22
use Siak\Tontine\Service\TenantService;
23
24
use function floor;
25
26
class CallableClass extends JaxonCallableClass
27
{
28
    /**
29
     * @di
30
     * @var TenantService
31
     */
32
    protected TenantService $tenantService;
33
34
    /**
35
     * @var ModalInterface
36
     */
37
    public $dialog;
38
39
    /**
40
     * @var MessageInterface
41
     */
42
    public $notify;
43
44
    /**
45
     * Get the page number to show
46
     *
47
     * @param int $pageNumber
48
     * @param int $itemCount
49
     * @param string $bagName
50
     * @param string $attrName
51
     *
52
     * @return array
53
     */
54
    protected function pageNumber(int $pageNumber, int $itemCount,
55
        string $bagName, string $attrName = 'page'): array
56
    {
57
        $perPage = 10;
58
        $pageCount = (int)floor($itemCount / $perPage) + ($itemCount % $perPage > 0 ? 1 : 0);
59
        if($pageNumber < 1)
60
        {
61
            $pageNumber = $this->bag($bagName)->get($attrName, 1);
62
        }
63
        if($pageNumber > $pageCount)
64
        {
65
            $pageNumber = $pageCount;
66
        }
67
        $this->bag($bagName)->set($attrName, $pageNumber);
68
69
        return [$pageNumber, 10 /*$this->tenantService->getLimit()*/];
70
    }
71
72
    /**
73
     * @param Tontine $tontine
74
     *
75
     * @return void
76
     */
77
    protected function selectTontine(Tontine $tontine)
78
    {
79
        $this->response->html('section-tontine-name', $tontine->name);
80
81
        // Set the tontine sidebar menu
82
        $this->response->html('sidebar-menu-tontine', $this->render('parts.sidebar.tontine'));
0 ignored issues
show
Bug introduced by
It seems like $this->render('parts.sidebar.tontine') 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

82
        $this->response->html('sidebar-menu-tontine', /** @scrutinizer ignore-type */ $this->render('parts.sidebar.tontine'));
Loading history...
83
        $this->jq('a', '#sidebar-menu-tontine')->css('color', '#6777ef');
84
85
        $this->jq('#tontine-menu-members')->click($this->cl(Member::class)->rq()->home());
86
        $this->jq('#tontine-menu-options')->click($this->cl(Options::class)->rq()->home());
87
88
        // Reset the round sidebar menu
89
        $this->response->html('sidebar-menu-round', $this->render('parts.sidebar.round'));
90
    }
91
92
    /**
93
     * @param Round $round
94
     *
95
     * @return void
96
     */
97
    protected function selectRound(Round $round)
98
    {
99
        $this->response->html('section-tontine-name', $round->tontine->name . ' - ' . $round->title);
100
101
        // Set the round sidebar menu
102
        $this->response->html('sidebar-menu-round', $this->render('parts.sidebar.round'));
0 ignored issues
show
Bug introduced by
It seems like $this->render('parts.sidebar.round') 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

102
        $this->response->html('sidebar-menu-round', /** @scrutinizer ignore-type */ $this->render('parts.sidebar.round'));
Loading history...
103
        $this->jq('a', '#sidebar-menu-round')->css('color', '#6777ef');
104
105
        $this->jq('#planning-menu-sessions')->click($this->cl(PlanningRound::class)->rq()->home());
106
        $this->jq('#planning-menu-pools')->click($this->cl(Pool::class)->rq()->home());
107
        $this->jq('#planning-menu-subscriptions')->click($this->cl(Subscription::class)->rq()->home());
108
        $this->jq('#meeting-menu-sessions')->click($this->cl(MeetingSession::class)->rq()->home());
109
        $this->jq('#report-menu-session')->click($this->cl(SessionReport::class)->rq()->home());
110
        $this->jq('#report-menu-round')->click($this->cl(RoundReport::class)->rq()->home());
111
    }
112
113
    /**
114
     * @return void
115
     */
116
    protected function hideMenuOnMobile()
117
    {
118
        // The current template main menu doesn't hide automatically
119
        // after a click on mobile devices. We need to do that manually.
120
        $this->jq('body')->trigger('touchend');
121
    }
122
123
    /**
124
     * Render a view
125
     *
126
     * @param string $view
127
     * @param array $viewData
128
     *
129
     * @return null|Store
130
     */
131
    protected function render(string $view, array $viewData = []): ?Store
132
    {
133
        return $this->view()->render('tontine.app.default.' . $view, $viewData);
134
    }
135
136
    /**
137
     * @param TenantService $tenantService
138
     *
139
     * @return $this
140
     */
141
    public function setTenantService(TenantService $tenantService)
142
    {
143
        $this->tenantService = $tenantService;
144
        return $this;
145
    }
146
147
    /**
148
     * @return void
149
     */
150
    protected function checkRoundSessions()
151
    {
152
        $round = $this->tenantService->round();
153
        if(!$round || $round->sessions()->count() === 0)
154
        {
155
            throw new PlanningRoundException(trans('tontine.errors.checks.sessions'));
156
        }
157
    }
158
159
    /**
160
     * @return void
161
     */
162
    protected function checkOpenedSessions()
163
    {
164
        $tontine = $this->tenantService->tontine();
165
        if(!$tontine || $tontine->members()->active()->count() === 0)
166
        {
167
            throw new TontineMemberException(trans('tontine.errors.checks.members'));
168
        }
169
170
        $round = $this->tenantService->round();
171
        if(!$round || $round->sessions->filter(fn($session) =>
172
            ($session->opened || $session->closed))->count() === 0)
173
        {
174
            throw new PlanningRoundException(trans('tontine.errors.checks.sessions'));
175
        }
176
    }
177
178
    /**
179
     * @return void
180
     */
181
    protected function checkRoundPools()
182
    {
183
        $this->checkRoundSessions();
184
185
        $round = $this->tenantService->round();
186
        if(!$round || $round->pools()->count() === 0)
187
        {
188
            throw new PlanningPoolException(trans('tontine.errors.checks.pools'));
189
        }
190
    }
191
}
192