Passed
Pull Request — main (#54)
by Thierry
13:54
created

Session::_showSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Report;
4
5
use App\Ajax\CallableClass;
6
use Siak\Tontine\Model\Session as SessionModel;
7
use Siak\Tontine\Service\Report\ReportService;
8
use Siak\Tontine\Service\TenantService;
9
use Siak\Tontine\Service\Tontine\TontineService;
10
11
use function compact;
12
use function count;
13
use function Jaxon\pm;
14
15
/**
16
 * @databag report
17
 */
18
class Session extends CallableClass
19
{
20
    /**
21
     * @param TenantService $tenantService
22
     * @param TontineService $tontineService
23
     * @param ReportService $reportService
24
     */
25
    public function __construct(protected TenantService $tenantService,
26
        protected TontineService $tontineService, protected ReportService $reportService)
27
    {}
28
29
    /**
30
     * @after hideMenuOnMobile
31
     */
32
    public function home()
33
    {
34
        // Don't show the page if there is no session or no member.
35
        $sessions = $this->tenantService->getSessions(orderAsc: false)
36
            ->filter(fn($session) => $session->opened || $session->closed);
37
        if($sessions->count() === 0)
38
        {
39
            return $this->response;
40
        }
41
        $members = $this->tontineService->getMembers();
42
        if($members->count() === 0)
43
        {
44
            return $this->response;
45
        }
46
        $members->prepend('', 0);
47
48
        $this->response->html('section-title', trans('tontine.menus.report'));
49
        $html = $this->render('pages.report.session.home',
50
            ['sessions' => $sessions->pluck('title', 'id'), 'members' => $members]);
51
        $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

51
        $this->response->html('content-home', /** @scrutinizer ignore-type */ $html);
Loading history...
52
53
        $this->jq('#btn-members-refresh')->click($this->rq()->home());
54
        $sessionId = pm()->select('select-session')->toInt();
55
        $memberId = pm()->select('select-member')->toInt();
56
        $this->jq('#btn-session-select')->click($this->rq()->showSession($sessionId));
57
        $this->jq('#btn-member-select')->click($this->rq()->showMember($sessionId, $memberId));
58
59
        return $this->_showSession($sessions->first());
60
    }
61
62
    private function showReportButtons(SessionModel $session)
63
    {
64
        $closings = $this->reportService->getClosings($session);
65
        $html = $this->render('pages.report.session.exports',
66
            ['sessionId' => $session->id, 'hasClosing' => count($closings) > 0]);
67
        $this->response->html('session-reports-export', $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

67
        $this->response->html('session-reports-export', /** @scrutinizer ignore-type */ $html);
Loading history...
68
69
        return $this->response;
70
    }
71
72
    private function _showSession(SessionModel $session)
73
    {
74
        $this->response->html('session-report-title', $session->title);
75
        $this->cl(Session\Session::class)->show($session);
76
77
        return $this->showReportButtons($session);
78
    }
79
80
    public function showSession(int $sessionId)
81
    {
82
        if(!($session = $this->tenantService->getSession($sessionId)))
83
        {
84
            return $this->response;
85
        }
86
87
        return $this->_showSession($session);
88
    }
89
90
    public function showMember(int $sessionId, int $memberId)
91
    {
92
        if(!($session = $this->tenantService->getSession($sessionId)))
93
        {
94
            return $this->response;
95
        }
96
        if(!($member = $this->tontineService->getMember($memberId)))
97
        {
98
            return $this->response;
99
        }
100
101
        $this->response->html('session-report-title', $session->title . ' - ' . $member->name);
102
        $this->cl(Session\Member::class)->show($session, $member);
103
104
        return $this->showReportButtons($session);
105
    }
106
}
107