Passed
Push — main ( 52a9ba...b89683 )
by PRATIK
04:37 queued 14s
created

CategoryRepository::uploadImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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