FundService::toggleFund()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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