Passed
Pull Request — main (#65)
by Thierry
10:34
created

PresenceService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 131
rs 10
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A togglePresence() 0 5 2
A getSession() 0 5 1
A __construct() 0 3 1
A getMemberCount() 0 3 1
A getSessionCount() 0 3 1
A getMembers() 0 6 1
A getMember() 0 6 1
A getSessionAbsences() 0 3 1
A getSessions() 0 5 1
A getMemberAbsences() 0 5 1
1
<?php
2
3
namespace Siak\Tontine\Service\Presence;
4
5
use Illuminate\Support\Collection;
6
use Siak\Tontine\Model\Member;
7
use Siak\Tontine\Model\Round;
8
use Siak\Tontine\Model\Session;
9
use Siak\Tontine\Service\Meeting\Member\MemberService;
10
use Siak\Tontine\Service\Meeting\Session\SessionService;
11
use Siak\Tontine\Service\TenantService;
12
13
class PresenceService
14
{
15
    /**
16
     * @param TenantService $tenantService
17
     */
18
    public function __construct(private TenantService $tenantService,
19
        private MemberService $memberService, private SessionService $sessionService)
20
    {}
21
22
    /**
23
     * @param Session $session
24
     *
25
     * @return Collection
26
     */
27
    public function getSessionAbsences(Session $session): Collection
28
    {
29
        return $session->absences()->get()->pluck('name', 'id');
30
    }
31
32
    /**
33
     * @param Round $round
34
     * @param Member $member
35
     *
36
     * @return Collection
37
     */
38
    public function getMemberAbsences(Round $round, Member $member): Collection
39
    {
40
        return $member->absences()
41
            ->where('round_id', $round->id)
42
            ->pluck('title', 'id');
43
    }
44
45
    /**
46
     * @param Session $session
47
     * @param Member $member
48
     *
49
     * @return void
50
     */
51
    public function togglePresence(Session $session, Member $member)
52
    {
53
        !$session->absences()->find($member->id) ?
54
            $session->absences()->attach($member->id) :
55
            $session->absences()->detach($member->id);
56
    }
57
58
    /**
59
     * Get the number of sessions in the selected round.
60
     *
61
     * @param Round $round
62
     *
63
     * @return int
64
     */
65
    public function getSessionCount(Round $round): int
66
    {
67
        return $this->sessionService->active()->getSessionCount($round);
68
    }
69
70
    /**
71
     * Get a paginated list of sessions in the selected round.
72
     *
73
     * @param Round $round
74
     * @param int $page
75
     *
76
     * @return Collection
77
     */
78
    public function getSessions(Round $round, int $page = 0): Collection
79
    {
80
        return $this->sessionService->active()
81
            ->withCount(['absences'])
82
            ->getSessions($round, $page, true);
83
    }
84
85
    /**
86
     * Find a session.
87
     *
88
     * @param Round $round
89
     * @param int $sessionId
90
     *
91
     * @return Session|null
92
     */
93
    public function getSession(Round $round, int $sessionId): ?Session
94
    {
95
        return $this->sessionService->active()
96
            ->withCount(['absences'])
97
            ->getSession($round, $sessionId);
98
    }
99
100
    /**
101
     * Get the number of members.
102
     *
103
     * @param Round $round
104
     * @param string $search
105
     *
106
     * @return int
107
     */
108
    public function getMemberCount(Round $round, string $search = ''): int
109
    {
110
        return $this->memberService->getMemberCount($round, $search);
111
    }
112
113
    /**
114
     * Get a paginated list of members.
115
     *
116
     * @param Round $round
117
     * @param string $search
118
     * @param int $page
119
     *
120
     * @return Collection
121
     */
122
    public function getMembers(Round $round, string $search, int $page = 0): Collection
123
    {
124
        return $this->memberService
125
            ->withCount(['absences' => fn($query) =>
126
                $query->where('round_id', $round->id)])
127
            ->getMembers($round, $search, $page);
128
    }
129
130
    /**
131
     * Find a member.
132
     *
133
     * @param Round $round
134
     * @param int $memberId
135
     *
136
     * @return Member|null
137
     */
138
    public function getMember(Round $round, int $memberId): ?Member
139
    {
140
        return $this->memberService
141
            ->withCount(['absences' => fn($query) =>
142
                $query->where('round_id', $round->id)])
143
            ->getMember($round, $memberId);
144
    }
145
}
146