Completed
Push — master ( 992249...82a150 )
by Ben
02:40
created

Product::categories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace RuffleLabs\ProductCore\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Illuminate\Support\Facades\DB;
8
use RuffleLabs\ProductCore\Traits\ImageConversions;
9
use RuffleLabs\ProductCore\Traits\PublishedTrait;
10
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
11
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;
12
13
class Product extends Model implements HasMediaConversions
14
{
15
    use HasMediaTrait;
16
    use PublishedTrait;
17
    use SoftDeletes;
18
    use ImageConversions;
19
20
    protected $dates = ['deleted_at'];
21
22
    protected $table = 'products';
23
24
    protected $fillable = ['title', 'description'];
25
26
    protected $with = ['categories'];
27
28
    private $categoryService;
0 ignored issues
show
introduced by
The private property $categoryService is not used, and could be removed.
Loading history...
29
30
    public function costs()
31
    {
32
        return $this->hasOne('RuffleLabs\ProductCore\Models\ProductCost');
33
    }
34
35
    public function variantGroups()
36
    {
37
        return $this->hasMany('RuffleLabs\ProductCore\Models\ProductVariantGroups');
38
    }
39
40
    public function categories()
41
    {
42
        return $this->morphToMany('RuffleLabs\ProductCore\Models\ProductCategory',
43
            'product',
44
            'product_categories_xref',
45
            'product_id',
46
            'category_id')
47
            ->where('parent_id', '=', NULL);
48
    }
49
50
    public function subcategories()
51
    {
52
        return $this->morphToMany('RuffleLabs\ProductCore\Models\ProductCategory',
53
            'product',
54
            'product_categories_xref',
55
            'product_id',
56
            'category_id')
57
            ->whereNotNull('parent_id');
58
    }
59
60
    public function images()
61
    {
62
        return $this->getMedia('product-images');
63
    }
64
65
    public function getPriceAttribute()
66
    {
67
        return $this->costs->currency . $this->costs->price;
68
    }
69
70
    public function getPriceRawAttribute()
71
    {
72
        return $this->costs->price;
73
    }
74
75
    public function setPriceAttribute($price)
76
    {
77
        $this->costs->price = $price;
78
        $this->costs->save();
79
    }
80
81
    public function hasCategory()
82
    {
83
        if($this->categories()->count() > 0){
84
            return true;
85
        }
86
        return false;
87
    }
88
89
    public function removeAllCategories()
90
    {
91
        DB::table('product_categories_xref')
92
            ->where('product_id', $this->id)
93
            ->delete();
94
    }
95
96
    public function assignCategories(array $categories = [])
97
    {
98
        foreach($categories as $categoryId) {
99
            $category = ProductCategory::find($categoryId);
100
            $this->assignCategory($category);
101
        }
102
    }
103
104
    public function assignCategory(ProductCategory $category)
105
    {
106
        if(is_null($category->parent_id)){
0 ignored issues
show
introduced by
The condition is_null($category->parent_id) can never be true.
Loading history...
107
            return $this->assignSubCategory($category);
108
        }
109
        return $this->categories()->save($category);
110
    }
111
112
    public function assignSubCategory(ProductCategory $category)
113
    {
114
        $this->subcategories()->save($category);
115
    }
116
117
118
}
119