Passed
Pull Request — main (#55)
by Thierry
05:38
created

Session::getMember()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Presence;
4
5
use App\Ajax\CallableClass;
6
use App\Ajax\Web\Meeting\Presence;
7
use Siak\Tontine\Model\Member as MemberModel;
8
use Siak\Tontine\Service\Meeting\PresenceService;
9
use Siak\Tontine\Service\Meeting\SessionService;
10
11
use function Jaxon\jq;
12
13
/**
14
 * @databag presence
15
 * @before getMember
16
 */
17
class Session extends CallableClass
18
{
19
    /**
20
     * @var bool
21
     */
22
    private $fromHome = false;
23
24
    /**
25
     * @var MemberModel|null
26
     */
27
    protected ?MemberModel $member = null;
28
29
    /**
30
     * @param SessionService $sessionService
31
     * @param PresenceService $presenceService
32
     */
33
    public function __construct(private SessionService $sessionService,
34
        private PresenceService $presenceService)
35
    {}
36
37
    protected function getMember()
38
    {
39
        if($this->target()->method() === 'home' ||
40
            ($memberId = $this->bag('presence')->get('member.id')) === 0)
41
        {
42
            return;
43
        }
44
        $this->member = $this->presenceService->getMember($memberId);
0 ignored issues
show
Bug introduced by
It seems like $memberId can also be of type null; however, parameter $memberId of Siak\Tontine\Service\Mee...nceService::getMember() does only seem to accept integer, 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

44
        $this->member = $this->presenceService->getMember(/** @scrutinizer ignore-type */ $memberId);
Loading history...
45
    }
46
47
    /**
48
     * @exclude
49
     */
50
    public function show(MemberModel $member)
51
    {
52
        $this->member = $member;
53
        $this->bag('presence')->set('member.id', $this->member->id);
54
        $this->bag('presence')->set('session.page', 1);
55
56
        return $this->_home();
57
    }
58
59
    public function home()
60
    {
61
        $this->fromHome = true;
62
        $this->bag('presence')->set('member.id', 0);
63
        $this->bag('presence')->set('session.page', 1);
64
65
        return $this->_home();
66
    }
67
68
    private function _home()
69
    {
70
        $html = $this->render('pages.meeting.presence.session.home', [
71
            'member' => $this->member, // Is null when showing presences by sessions.
72
        ]);
73
        $this->response->html('content-home-sessions', $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-sessions', /** @scrutinizer ignore-type */ $html);
Loading history...
74
75
        $this->jq('#btn-presence-sessions-refresh')->click($this->rq()->page());
76
77
        return $this->page();
78
    }
79
80
    public function page(int $pageNumber = 0)
81
    {
82
        $sessionCount = $this->presenceService->getSessionCount();
83
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $sessionCount,
84
            'presence', 'session.page');
85
        $sessions = $this->presenceService->getSessions($pageNumber);
86
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $sessionCount);
87
        $absences = !$this->member ? null :
88
            $this->presenceService->getMemberAbsences($this->member);
89
90
        $html = $this->render('pages.meeting.presence.session.page', [
91
            'member' => $this->member, // Is null when showing presences by sessions.
92
            'sessions' => $sessions,
93
            'absences' => $absences,
94
            'pagination' => $pagination,
95
            'statuses' => $this->sessionService->getSessionStatuses(),
96
            'memberCount' => $this->presenceService->getMemberCount(),
97
            'sessionCount' => $sessionCount,
98
        ]);
99
        $this->response->html('content-page-sessions', $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

99
        $this->response->html('content-page-sessions', /** @scrutinizer ignore-type */ $html);
Loading history...
100
101
        $sessionId = jq()->parent()->attr('data-session-id')->toInt();
102
        $this->jq('.btn-toggle-session-presence')->click($this->rq()->togglePresence($sessionId));
103
        $this->jq('.btn-show-session-presences')
104
            ->click($this->cl(Presence::class)->rq()->selectSession($sessionId));
105
106
        if($this->fromHome && $sessions->count() > 0)
107
        {
108
            $session = $sessions->first();
109
            $this->cl(Member::class)->show($session);
110
        }
111
112
        return $this->response;
113
    }
114
115
    public function togglePresence(int $sessionId)
116
    {
117
        $session = $this->sessionService->getSession($sessionId);
118
        if(!$session)
119
        {
120
            return $this->response;
121
        }
122
123
        $this->presenceService->togglePresence($session, $this->member);
0 ignored issues
show
Bug introduced by
It seems like $this->member can also be of type null; however, parameter $member of Siak\Tontine\Service\Mee...rvice::togglePresence() does only seem to accept Siak\Tontine\Model\Member, 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

123
        $this->presenceService->togglePresence($session, /** @scrutinizer ignore-type */ $this->member);
Loading history...
124
        // Refresh the member counters
125
        $this->member = $this->presenceService->getMember($this->member->id);
126
127
        $this->cl(Member::class)->page();
128
        return $this->page();
129
    }
130
}
131