Passed
Push — main ( aad503...73e694 )
by Thierry
08:30
created

FundService::getFund()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Service\Tontine;
4
5
use Illuminate\Support\Collection;
6
use Siak\Tontine\Model\Fund;
7
use Siak\Tontine\Service\TenantService;
8
9
use function trans;
10
11
class FundService
12
{
13
    /**
14
     * @param TenantService $tenantService
15
     */
16
    public function __construct(protected TenantService $tenantService)
17
    {}
18
19
    /**
20
     * Get all active funds, included the default one.
21
     *
22
     * @return Collection
23
     */
24
    public function getActiveFunds(): Collection
25
    {
26
        $defaultFund = new Fund();
27
        $defaultFund->id = 0;
28
        $defaultFund->title = trans('tontine.fund.labels.default');
29
        $defaultFund->active = true;
30
        return $this->tenantService->tontine()->funds()->active()
31
            ->get()
32
            ->prepend($defaultFund);
33
    }
34
35
    /**
36
     * Get a list of funds for the dropdown select component.
37
     *
38
     * @return Collection
39
     */
40
    public function getFundList(): Collection
41
    {
42
        return $this->getActiveFunds()->pluck('title', 'id');
43
    }
44
45
    /**
46
     * Get a paginated list of funds.
47
     *
48
     * @param int $page
49
     *
50
     * @return Collection
51
     */
52
    public function getFunds(int $page = 0): Collection
53
    {
54
        return $this->tenantService->tontine()->funds()
55
            ->page($page, $this->tenantService->getLimit())
56
            ->get();
57
    }
58
59
    /**
60
     * Get the number of funds.
61
     *
62
     * @return int
63
     */
64
    public function getFundCount(): int
65
    {
66
        return $this->tenantService->tontine()->funds()->count();
67
    }
68
69
    /**
70
     * Get a single fund.
71
     *
72
     * @param int $fundId    The fund id
73
     *
74
     * @return Fund|null
75
     */
76
    public function getFund(int $fundId): ?Fund
77
    {
78
        return $this->tenantService->tontine()->funds()->find($fundId);
79
    }
80
81
    /**
82
     * Add new fund.
83
     *
84
     * @param array $values
85
     *
86
     * @return bool
87
     */
88
    public function createFund(array $values): bool
89
    {
90
        $this->tenantService->tontine()->funds()->create($values);
91
        return true;
92
    }
93
94
    /**
95
     * Update a fund.
96
     *
97
     * @param Fund $fund
98
     * @param array $values
99
     *
100
     * @return bool
101
     */
102
    public function updateFund(Fund $fund, array $values): bool
103
    {
104
        return $fund->update($values);
105
    }
106
107
    /**
108
     * Toggle a fund.
109
     *
110
     * @param Fund $fund
111
     *
112
     * @return void
113
     */
114
    public function toggleFund(Fund $fund)
115
    {
116
        $fund->update(['active' => !$fund->active]);
117
    }
118
119
    /**
120
     * Delete a fund.
121
     *
122
     * @param Fund $fund
123
     *
124
     * @return void
125
     */
126
    public function deleteFund(Fund $fund)
127
    {
128
        $fund->delete();
129
    }
130
}
131