Passed
Push — main ( 3c28f3...bb1b4c )
by Thierry
06:38 queued 28s
created

Invite::refuse()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Ajax\Web\Tontine\Guest;
4
5
use App\Ajax\CallableClass;
6
use Siak\Tontine\Service\Tontine\GuestService;
7
use Siak\Tontine\Validation\Tontine\GuestInviteValidator;
8
9
use function Jaxon\jq;
10
use function Jaxon\pm;
11
use function trans;
12
13
/**
14
 * @databag invite
15
 */
16
class Invite extends CallableClass
17
{
18
    /**
19
     * @var GuestInviteValidator
20
     */
21
    protected GuestInviteValidator $validator;
22
23
    /**
24
     * @param GuestService $guestService
25
     */
26
    public function __construct(private GuestService $guestService)
27
    {}
28
29
    /**
30
     * @after hideMenuOnMobile
31
     */
32
    public function home()
33
    {
34
        $this->response->html('section-title', trans('tontine.menus.tontines'));
35
        $this->response->html('content-home', $this->render('pages.invite.home'));
0 ignored issues
show
Bug introduced by
It seems like $this->render('pages.invite.home') 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

35
        $this->response->html('content-home', /** @scrutinizer ignore-type */ $this->render('pages.invite.home'));
Loading history...
36
37
        $this->jq('#btn-host-invite-create')->click($this->rq()->add());
38
        $this->jq('#btn-host-invites-refresh')->click($this->rq()->hosts());
39
        $this->jq('#btn-guest-invites-refresh')->click($this->rq()->guests());
40
41
        $this->hosts();
42
        $this->guests();
43
44
        return $this->response;
45
    }
46
47
    public function hosts(int $pageNumber = 0)
48
    {
49
        $inviteCount = $this->guestService->getHostInviteCount();
50
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $inviteCount, 'invite', 'host.page');
51
        $invites = $this->guestService->getHostInvites($pageNumber);
52
        $pagination = $this->rq()->hosts()->paginate($pageNumber, $perPage, $inviteCount);
53
54
        $html = $this->render('pages.invite.host.page', [
55
            'invites' => $invites,
56
            'pagination' => $pagination,
57
        ]);
58
        $this->response->html('content-host-invites-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

58
        $this->response->html('content-host-invites-page', /** @scrutinizer ignore-type */ $html);
Loading history...
59
60
        $inviteId = jq()->parent()->attr('data-invite-id')->toInt();
61
        $this->jq('.btn-host-invite-access')->click($this->rq(Access::class)->home($inviteId));
62
        $this->jq('.btn-host-invite-cancel')->click($this->rq()->cancel($inviteId)
63
            ->confirm(trans('tontine.invite.questions.cancel')));
64
        $this->jq('.btn-host-invite-delete')->click($this->rq()->hostDelete($inviteId)
65
            ->confirm(trans('tontine.invite.questions.delete')));
66
67
        return $this->response;
68
    }
69
70
    public function guests(int $pageNumber = 0)
71
    {
72
        $inviteCount = $this->guestService->getGuestInviteCount();
73
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $inviteCount, 'invite', 'guest.page');
74
        $invites = $this->guestService->getGuestInvites($pageNumber);
75
        $pagination = $this->rq()->guests()->paginate($pageNumber, $perPage, $inviteCount);
76
77
        $html = $this->render('pages.invite.guest.page', [
78
            'invites' => $invites,
79
            'pagination' => $pagination,
80
        ]);
81
        $this->response->html('content-guest-invites-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

81
        $this->response->html('content-guest-invites-page', /** @scrutinizer ignore-type */ $html);
Loading history...
82
83
        $inviteId = jq()->parent()->attr('data-invite-id')->toInt();
84
        $this->jq('.btn-guest-invite-accept')->click($this->rq()->accept($inviteId)
85
            ->confirm(trans('tontine.invite.questions.accept')));
86
        $this->jq('.btn-guest-invite-refuse')->click($this->rq()->refuse($inviteId)
87
            ->confirm(trans('tontine.invite.questions.refuse')));
88
        $this->jq('.btn-guest-invite-delete')->click($this->rq()->guestDelete($inviteId)
89
            ->confirm(trans('tontine.invite.questions.delete')));
90
91
        return $this->response;
92
    }
93
94
    public function add()
95
    {
96
        $title = trans('tontine.invite.titles.add');
97
        $content = $this->render('pages.invite.host.add');
98
        $buttons = [[
99
            'title' => trans('common.actions.cancel'),
100
            'class' => 'btn btn-tertiary',
101
            'click' => 'close',
102
        ],[
103
            'title' => trans('common.actions.save'),
104
            'class' => 'btn btn-primary',
105
            'click' => $this->rq()->create(pm()->form('invite-form')),
106
        ]];
107
108
        $this->dialog->hide();
109
        $this->dialog->show($title, $content, $buttons);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $sContent of Jaxon\App\Dialog\ModalInterface::show() 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

109
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
110
111
        return $this->response;
112
    }
113
114
    /**
115
     * @di $validator
116
     */
117
    public function create(array $formValues)
118
    {
119
        $values = $this->validator->validateItem($formValues);
120
        $this->guestService->createInvite($values['email']);
121
122
        $this->dialog->hide();
123
        $this->notify->success(trans('tontine.invite.messages.sent'), trans('common.titles.success'));
124
125
        return $this->hosts();
126
    }
127
128
    public function accept(int $inviteId)
129
    {
130
        $this->guestService->acceptInvite($inviteId);
131
        $this->notify->success(trans('tontine.invite.messages.accepted'), trans('common.titles.success'));
132
133
        return $this->guests();
134
    }
135
136
    public function refuse(int $inviteId)
137
    {
138
        $this->guestService->refuseInvite($inviteId);
139
        $this->notify->success(trans('tontine.invite.messages.refused'), trans('common.titles.success'));
140
141
        return $this->guests();
142
    }
143
144
    public function cancel(int $inviteId)
145
    {
146
        $this->guestService->cancelInvite($inviteId);
147
        $this->notify->success(trans('tontine.invite.messages.cancelled'), trans('common.titles.success'));
148
149
        return $this->hosts();
150
    }
151
152
    public function hostDelete(int $inviteId)
153
    {
154
        $this->guestService->deleteHostInvite($inviteId);
155
        $this->notify->success(trans('tontine.invite.messages.deleted'), trans('common.titles.success'));
156
157
        return $this->hosts();
158
    }
159
160
    public function guestDelete(int $inviteId)
161
    {
162
        if($this->guestService->deleteGuestInvite($inviteId))
163
        {
164
            // The active tontine invite is deleted. Reload the page.
165
            $this->response->redirect('/');
166
            return $this->response;
167
        }
168
169
        $this->notify->success(trans('tontine.invite.messages.deleted'), trans('common.titles.success'));
170
        return $this->guests();
171
    }
172
}
173