Passed
Push — main ( 1c458a...5a987a )
by Thierry
10:56 queued 05:26
created

MemberService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 82
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getMember() 0 4 1
A getQuery() 0 4 1
A getMembers() 0 7 1
A getMemberList() 0 6 1
A getMemberCount() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace Siak\Tontine\Service\Meeting\Member;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Illuminate\Support\Collection;
8
use Siak\Tontine\Model\Round;
9
use Siak\Tontine\Model\Member;
10
use Siak\Tontine\Service\TenantService;
11
use Siak\Tontine\Service\Traits\WithTrait;
12
use Siak\Tontine\Validation\SearchSanitizer;
13
14
use function tap;
15
16
class MemberService
17
{
18
    use WithTrait;
19
20
    /**
21
     * @param TenantService $tenantService
22
     * @param SearchSanitizer $searchSanitizer
23
     */
24
    public function __construct(private TenantService $tenantService,
25
        private SearchSanitizer $searchSanitizer)
26
    {}
27
28
    /**
29
     * @param Round $round
30
     * @param string $search
31
     *
32
     * @return Builder|Relation
33
     */
34
    private function getQuery(Round $round, string $search): Builder|Relation
35
    {
36
        return $round->members()
37
            ->search($this->searchSanitizer->sanitize($search));
38
    }
39
40
    /**
41
     * Get a paginated list of members.
42
     *
43
     * @param Round $round
44
     * @param string $search
45
     * @param int $page
46
     *
47
     * @return Collection
48
     */
49
    public function getMembers(Round $round, string $search = '', int $page = 0): Collection
50
    {
51
        return tap($this->getQuery($round, $search),
52
                fn($query) => $this->addWith($query))
53
            ->page($page, $this->tenantService->getLimit())
54
            ->orderBy('name', 'asc')
55
            ->get();
56
    }
57
58
    /**
59
     * Get the number of members.
60
     *
61
     * @param Round $round
62
     * @param string $search
63
     *
64
     * @return int
65
     */
66
    public function getMemberCount(Round $round, string $search = ''): int
67
    {
68
        return $this->getQuery($round, $search)->count();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getQuery($round, $search)->count() could return the type Illuminate\Database\Eloq...uent\Relations\Relation which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
69
    }
70
71
    /**
72
     * Get a list of members for dropdown.
73
     *
74
     * @param Round $round
75
     *
76
     * @return Collection
77
     */
78
    public function getMemberList(Round $round): Collection
79
    {
80
        return $round->members()
81
            ->orderBy('name', 'asc')
82
            ->get()
83
            ->pluck('name', 'id');
84
    }
85
86
    /**
87
     * Get a single member.
88
     *
89
     * @param Round $round
90
     * @param int $memberId
91
     *
92
     * @return Member|null
93
     */
94
    public function getMember(Round $round, int $memberId): ?Member
95
    {
96
        return tap($round->members(), fn($query) => $this
97
            ->addWith($query))->find($memberId);
98
    }
99
}
100