ProductRepository   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 24
c 2
b 0
f 1
dl 0
loc 72
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A syncAttribute() 0 7 3
A updateProduct() 0 5 1
A storeProduct() 0 5 1
A indexProduct() 0 9 3
A showProduct() 0 3 1
A editProduct() 0 3 1
A createProduct() 0 2 1
A destroyProduct() 0 3 1
A uploadImage() 0 6 2
1
<?php
2
3
namespace Adminetic\Website\Repositories;
4
5
use Adminetic\Website\Contracts\ProductRepositoryInterface;
6
use Adminetic\Website\Http\Requests\ProductRequest;
7
use Adminetic\Website\Models\Admin\Product;
8
use Illuminate\Support\Facades\Cache;
9
10
class ProductRepository implements ProductRepositoryInterface
11
{
12
    // Product Index
13
    public function indexProduct()
14
    {
15
        $products = config('adminetic.caching', true)
16
            ? (Cache::has('products') ? Cache::get('products') : Cache::rememberForever('products', function () {
17
                return Product::orderBy('position')->get();
18
            }))
19
            : Product::orderBy('position')->get();
20
21
        return compact('products');
22
    }
23
24
    // Product Create
25
    public function createProduct()
26
    {
27
        //
28
    }
29
30
    // Product Store
31
    public function storeProduct(ProductRequest $request)
32
    {
33
        $product = Product::create($request->validated());
34
        $this->syncAttribute($product);
35
        $this->uploadImage($product);
36
    }
37
38
    // Product Show
39
    public function showProduct(Product $product)
40
    {
41
        return compact('product');
42
    }
43
44
    // Product Edit
45
    public function editProduct(Product $product)
46
    {
47
        return compact('product');
48
    }
49
50
    // Product Update
51
    public function updateProduct(ProductRequest $request, Product $product)
52
    {
53
        $product->update($request->validated());
54
        $this->syncAttribute($product);
55
        $this->uploadImage($product);
56
    }
57
58
    // Product Destroy
59
    public function destroyProduct(Product $product)
60
    {
61
        $product->delete();
62
    }
63
64
    // Upload Image
65
    private function uploadImage(Product $product)
66
    {
67
        if (request()->has('image')) {
68
            $product
69
                ->addFromMediaLibraryRequest(request()->image)
70
                ->toMediaCollection('image');
71
        }
72
    }
73
74
    // Sync Attribute
75
    private function syncAttribute(Product $product)
76
    {
77
        if (request()->has('product_attributes')) {
78
            $product->attributes()->detach();
79
            foreach (request()->product_attributes as $attribute_id => $values) {
80
                $product->attributes()->attach($attribute_id, [
81
                    'values' => json_encode($values),
82
                ]);
83
            }
84
        }
85
    }
86
}
87