Passed
Push — main ( 7be313...891842 )
by Thierry
19:01 queued 53s
created

CallableClass::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax;
4
5
use App\Ajax\Web\Meeting\Presence;
6
use App\Ajax\Web\Meeting\Session as MeetingSession;
7
use App\Ajax\Web\Planning\Pool;
8
use App\Ajax\Web\Planning\Round as PlanningRound;
9
use App\Ajax\Web\Planning\Subscription;
10
use App\Ajax\Web\Report\Round as ReportRound;
11
use App\Ajax\Web\Report\Session as ReportSession;
12
use App\Ajax\Web\Tontine\Member;
13
use App\Ajax\Web\Tontine\Options;
14
use Siak\Tontine\Model\Round;
15
use Siak\Tontine\Model\Tontine;
16
use Jaxon\App\CallableClass as JaxonCallableClass;
17
use Jaxon\App\Dialog\MessageInterface;
18
use Jaxon\App\Dialog\ModalInterface;
19
use Jaxon\App\View\Store;
20
use Siak\Tontine\Exception\MeetingRoundException;
21
use Siak\Tontine\Exception\PlanningPoolException;
22
use Siak\Tontine\Exception\PlanningRoundException;
23
use Siak\Tontine\Exception\TontineMemberException;
24
use Siak\Tontine\Service\TenantService;
25
26
use function floor;
27
28
class CallableClass extends JaxonCallableClass
29
{
30
    /**
31
     * @di
32
     * @var TenantService
33
     */
34
    protected TenantService $tenantService;
35
36
    /**
37
     * @var string
38
     */
39
    protected static $templateDir = 'tontine.app.default.';
40
41
    /**
42
     * @var ModalInterface
43
     */
44
    public $dialog;
45
46
    /**
47
     * @var MessageInterface
48
     */
49
    public $notify;
50
51
    /**
52
     * Get the page number to show
53
     *
54
     * @param int $pageNumber
55
     * @param int $itemCount
56
     * @param string $bagName
57
     * @param string $attrName
58
     *
59
     * @return array
60
     */
61
    protected function pageNumber(int $pageNumber, int $itemCount,
62
        string $bagName, string $attrName = 'page'): array
63
    {
64
        $perPage = 10;
65
        $pageCount = (int)floor($itemCount / $perPage) + ($itemCount % $perPage > 0 ? 1 : 0);
66
        if($pageNumber < 1)
67
        {
68
            $pageNumber = $this->bag($bagName)->get($attrName, 1);
69
        }
70
        if($pageNumber > $pageCount)
71
        {
72
            $pageNumber = $pageCount;
73
        }
74
        $this->bag($bagName)->set($attrName, $pageNumber);
75
76
        return [$pageNumber, $this->tenantService->getLimit()];
77
    }
78
79
    /**
80
     * @param Tontine $tontine
81
     *
82
     * @return void
83
     */
84
    protected function selectTontine(Tontine $tontine)
85
    {
86
        $this->response->html('section-tontine-name', $tontine->name);
87
88
        // Set the tontine sidebar menu
89
        $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

89
        $this->response->html('sidebar-menu-tontine', /** @scrutinizer ignore-type */ $this->render('parts.sidebar.tontine'));
Loading history...
90
        $this->jq('a', '#sidebar-menu-tontine')->css('color', '#6777ef');
91
92
        $this->jq('#tontine-menu-members')->click($this->rq(Member::class)->home());
93
        $this->jq('#tontine-menu-categories')->click($this->rq(Options::class)->home());
94
        $this->jq('#planning-menu-sessions')->click($this->rq(PlanningRound::class)->home());
95
96
        // Reset the round sidebar menu
97
        $this->response->html('sidebar-menu-round', $this->render('parts.sidebar.round'));
98
    }
99
100
    /**
101
     * @param Round $round
102
     *
103
     * @return void
104
     */
105
    protected function selectRound(Round $round)
106
    {
107
        $this->response->html('section-tontine-name', $round->tontine->name . ' - ' . $round->title);
108
109
        // Set the round sidebar menu
110
        $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

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