AccountService::toggleAccount()   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\Support\Collection;
6
use Siak\Tontine\Model\Category;
7
use Siak\Tontine\Model\Guild;
8
use Siak\Tontine\Service\TenantService;
9
10
class AccountService
11
{
12
    /**
13
     * @param TenantService $tenantService
14
     */
15
    public function __construct(protected TenantService $tenantService)
16
    {}
17
18
    /**
19
     * Get a paginated list of categories.
20
     *
21
     * @param Guild $guild
22
     * @param int $page
23
     *
24
     * @return Collection
25
     */
26
    public function getAccounts(Guild $guild, int $page = 0): Collection
27
    {
28
        return $guild->categories()
29
            ->outflow()
30
            ->page($page, $this->tenantService->getLimit())
31
            ->get();
32
    }
33
34
    /**
35
     * Get the number of categories.
36
     *
37
     * @param Guild $guild
38
     *
39
     * @return int
40
     */
41
    public function getCategoryCount(Guild $guild): int
42
    {
43
        return $guild->categories()->outflow()->count();
44
    }
45
46
    /**
47
     * Get a single category.
48
     *
49
     * @param Guild $guild
50
     * @param int $categoryId
51
     *
52
     * @return Category|null
53
     */
54
    public function getAccount(Guild $guild, int $categoryId): ?Category
55
    {
56
        return $guild->categories()->outflow()->find($categoryId);
57
    }
58
59
    /**
60
     * Add new category.
61
     *
62
     * @param Guild $guild
63
     * @param array $values
64
     *
65
     * @return bool
66
     */
67
    public function createAccount(Guild $guild, array $values): bool
68
    {
69
        $guild->categories()->create($values);
70
        return true;
71
    }
72
73
    /**
74
     * Update a category.
75
     *
76
     * @param Category $category
77
     * @param array $values
78
     *
79
     * @return bool
80
     */
81
    public function updateAccount(Category $category, array $values): bool
82
    {
83
        return $category->update($values);
84
    }
85
86
    /**
87
     * Toggle a category.
88
     *
89
     * @param Category $category
90
     *
91
     * @return void
92
     */
93
    public function toggleAccount(Category $category)
94
    {
95
        $category->update(['active' => !$category->active]);
96
    }
97
98
    /**
99
     * Delete a category.
100
     *
101
     * @param Category $category
102
     *
103
     * @return void
104
     */
105
    public function deleteAccount(Category $category)
106
    {
107
        $category->delete();
108
    }
109
}
110