CategoriesService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 18
dl 0
loc 78
rs 10
c 1
b 0
f 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 7 1
A create() 0 8 1
A update() 0 9 1
A findBySlug() 0 3 1
A find() 0 3 1
A indexPaginated() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace WebDevEtc\BlogEtc\Services;
4
5
use Exception;
6
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
7
use WebDevEtc\BlogEtc\Events\CategoryAdded;
8
use WebDevEtc\BlogEtc\Events\CategoryEdited;
9
use WebDevEtc\BlogEtc\Events\CategoryWillBeDeleted;
10
use WebDevEtc\BlogEtc\Models\Category;
11
use WebDevEtc\BlogEtc\Repositories\CategoriesRepository;
12
13
/**
14
 * Class BlogEtcCategoriesService.
15
 *
16
 * Service class to handle most logic relating to BlogEtcCategory entries.
17
 */
18
class CategoriesService
19
{
20
    /**
21
     * @var CategoriesRepository
22
     */
23
    private $repository;
24
25
    /**
26
     * CategoriesService constructor.
27
     */
28
    public function __construct(CategoriesRepository $repository)
29
    {
30
        $this->repository = $repository;
31
    }
32
33
    /**
34
     * Return paginated collection of categories.
35
     */
36
    public function indexPaginated(int $perPage = 25): LengthAwarePaginator
37
    {
38
        return $this->repository->indexPaginated($perPage);
39
    }
40
41
    /**
42
     * Find and return a blog etc category, based on its slug.
43
     */
44
    public function findBySlug(string $categorySlug): Category
45
    {
46
        return $this->repository->findBySlug($categorySlug);
47
    }
48
49
    /**
50
     * Create a new Category entry.
51
     */
52
    public function create(array $attributes): Category
53
    {
54
        $newCategory = new Category($attributes);
55
        $newCategory->save();
56
57
        event(new CategoryAdded($newCategory));
58
59
        return $newCategory;
60
    }
61
62
    /**
63
     * Update an existing Category entry.
64
     */
65
    public function update(int $categoryID, array $attributes): Category
66
    {
67
        $category = $this->find($categoryID);
68
        $category->fill($attributes);
69
        $category->save();
70
71
        event(new CategoryEdited($category));
72
73
        return $category;
74
    }
75
76
    /**
77
     * Find and return a blog etc category from it's ID.
78
     */
79
    public function find(int $categoryID): Category
80
    {
81
        return $this->repository->find($categoryID);
82
    }
83
84
    /**
85
     * Delete a BlogEtcCategory.
86
     *
87
     * @throws Exception
88
     */
89
    public function delete(int $categoryID): void
90
    {
91
        $category = $this->find($categoryID);
92
93
        event(new CategoryWillBeDeleted($category));
94
95
        $category->delete();
96
    }
97
}
98