Passed
Push — main ( b3a018...5357dd )
by PRATIK
04:02
created

CategoryRepository::editCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Adminetic\Website\Repositories;
4
5
use Adminetic\Website\Contracts\CategoryRepositoryInterface;
6
use Adminetic\Website\Http\Requests\CategoryRequest;
7
use Adminetic\Website\Models\Admin\Category;
8
use Illuminate\Support\Facades\Cache;
9
10
class CategoryRepository implements CategoryRepositoryInterface
11
{
12
    // Category Index
13
    public function indexCategory()
14
    {
15
        $categories = config('adminetic.caching', true)
16
            ? (Cache::has('categories') ? Cache::get('categories') : Cache::rememberForever('categories', function () {
17
                return Category::whereNull('parent_id')->with('childrenCategories')->orderBy('position')->get();
18
            }))
19
            : Category::whereNull('parent_id')->with('childrenCategories')->orderBy('position')->get();
20
21
        return compact('categories');
22
    }
23
24
    // Category Create
25
    public function createCategory()
26
    {
27
        $parentcategories = Category::whereNull('parent_id')->with('childrenCategories')->get();
28
29
        return compact('parentcategories');
30
    }
31
32
    // Category Store
33
    public function storeCategory(CategoryRequest $request)
34
    {
35
        $category = Category::create($request->validated());
36
        $this->uploadImage($category);
37
    }
38
39
    // Category Show
40
    public function showCategory(Category $category)
41
    {
42
        return compact('category');
43
    }
44
45
    // Category Edit
46
    public function editCategory(Category $category)
47
    {
48
        $parentcategories = Category::whereNull('parent_id')->with('childrenCategories')->get();
49
50
        return compact('category', 'parentcategories');
51
    }
52
53
    // Category Update
54
    public function updateCategory(CategoryRequest $request, Category $category)
55
    {
56
        $category->update($request->validated());
57
        $this->uploadImage($category);
58
    }
59
60
    // Category Destroy
61
    public function destroyCategory(Category $category)
62
    {
63
        $category->hardDelete('image');
64
        $category->delete();
65
    }
66
67
    // Upload Image
68
    protected function uploadImage(Category $category)
69
    {
70
        if (request()->image) {
71
            $thumbnails = [
72
                'storage' => 'category/category',
73
                'width' => '512',
74
                'height' => '512',
75
                'quality' => '90',
76
                'thumbnails' => [
77
                    [
78
                        'thumbnail-name' => 'small',
79
                        'thumbnail-width' => '100',
80
                        'thumbnail-height' => '100',
81
                        'thumbnail-quality' => '50',
82
                    ],
83
                ],
84
            ];
85
            $category->makeThumbnail('image', $thumbnails);
86
        }
87
    }
88
}
89