Product   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 85
dl 0
loc 199
rs 9.84
c 0
b 0
f 0
wmc 32

21 Methods

Rating   Name   Duplication   Size   Complexity  
A setDiscountEndDateAttribute() 0 8 2
A productImages() 0 3 1
B getPriceDetails() 0 55 7
A setRankAttribute() 0 4 2
A productCategory() 0 3 1
A shop() 0 3 1
A relatedProducts() 0 3 1
A taxRate() 0 3 1
A getIndexName() 0 3 1
A getDiscountEndDateAttribute() 0 8 2
A extraFields() 0 3 1
A setDiscountStartDateAttribute() 0 8 2
A amountSeries() 0 3 1
A attributes() 0 3 1
A amountOptions() 0 3 1
A relatedProductsActive() 0 5 1
A sluggable() 0 5 1
A subcategories() 0 3 1
A attributeGroup() 0 3 1
A brand() 0 3 1
A getDiscountStartDateAttribute() 0 8 2
1
<?php 
2
3
namespace Hideyo\Ecommerce\Framework\Services\Product\Entity;
4
5
use Hideyo\Ecommerce\Framework\Services\BaseModel;
6
use Cviebrock\EloquentSluggable\Sluggable;
7
use Carbon\Carbon;
8
use Elasticquent\ElasticquentTrait;
0 ignored issues
show
Bug introduced by
The type Elasticquent\ElasticquentTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class Product extends BaseModel
11
{
12
    use ElasticquentTrait, Sluggable;
13
14
    /**
15
     * The database table used by the model.
16
     *
17
     * @var string
18
     */    
19
    protected $table = 'product';
20
21
    // Add the 'avatar' attachment to the fillable array so that it's mass-assignable on this model.
22
    protected $fillable = ['active', 'discount_promotion', 'discount_type', 'discount_value', 'discount_start_date', 'discount_end_date', 'title', 'brand_id', 'product_category_id', 'reference_code', 'ean_code', 'mpn_code', 'short_description', 'description', 'ingredients', 'price', 'commercial_price', 'tax_rate_id', 'amount', 'meta_title', 'meta_description', 'meta_keywords', 'shop_id', 'modified_by_user_id', 'weight', 'leading_atrribute_group_id', 'rank'];
23
24
    public function sluggable()
25
    {
26
        return [
27
            'slug' => [
28
                'source' => 'title'
29
            ]
30
        ];
31
    }
32
33
    function getIndexName()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
    {
35
        return 'product';
36
    }
37
    
38
    public function setDiscountStartDateAttribute($value)
39
    {
40
        $this->attributes['discount_start_date'] = null;
41
42
        if ($value) {
43
            $date = explode('/', $value);
44
            $value = Carbon::createFromDate($date[2], $date[1], $date[0])->toDateTimeString();
0 ignored issues
show
Bug introduced by
$date[0] of type string is incompatible with the type integer|null expected by parameter $day of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            $value = Carbon::createFromDate($date[2], $date[1], /** @scrutinizer ignore-type */ $date[0])->toDateTimeString();
Loading history...
Bug introduced by
$date[2] of type string is incompatible with the type integer|null expected by parameter $year of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            $value = Carbon::createFromDate(/** @scrutinizer ignore-type */ $date[2], $date[1], $date[0])->toDateTimeString();
Loading history...
Bug introduced by
$date[1] of type string is incompatible with the type integer|null expected by parameter $month of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            $value = Carbon::createFromDate($date[2], /** @scrutinizer ignore-type */ $date[1], $date[0])->toDateTimeString();
Loading history...
45
            $this->attributes['discount_start_date'] = $value;
46
        }
47
    }
48
49
    public function getDiscountStartDateAttribute($value)
50
    {
51
        if ($value) {
52
            $date = explode('-', $value);
53
            return $date[2].'/'.$date[1].'/'.$date[0];
54
        }
55
        
56
        return null;
57
    }
58
59
    public function setDiscountEndDateAttribute($value)
60
    {
61
        $this->attributes['discount_end_date'] = null;    
62
        
63
        if ($value) {
64
            $date = explode('/', $value);
65
            $value = Carbon::createFromDate($date[2], $date[1], $date[0])->toDateTimeString();
0 ignored issues
show
Bug introduced by
$date[1] of type string is incompatible with the type integer|null expected by parameter $month of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            $value = Carbon::createFromDate($date[2], /** @scrutinizer ignore-type */ $date[1], $date[0])->toDateTimeString();
Loading history...
Bug introduced by
$date[0] of type string is incompatible with the type integer|null expected by parameter $day of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            $value = Carbon::createFromDate($date[2], $date[1], /** @scrutinizer ignore-type */ $date[0])->toDateTimeString();
Loading history...
Bug introduced by
$date[2] of type string is incompatible with the type integer|null expected by parameter $year of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            $value = Carbon::createFromDate(/** @scrutinizer ignore-type */ $date[2], $date[1], $date[0])->toDateTimeString();
Loading history...
66
            $this->attributes['discount_end_date'] = $value;
67
        }
68
    }
69
70
    public function setRankAttribute($value)
71
    {       
72
        if (is_null($value)) {
73
            $value = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
74
        }
75
    }
76
77
    public function getDiscountEndDateAttribute($value)
78
    {
79
        if ($value) {
80
            $date = explode('-', $value);
81
            return $date[2].'/'.$date[1].'/'.$date[0];
82
        }
83
        
84
        return null;
85
    }
86
87
    public function getPriceDetails()
88
    {
89
        if ($this->price) {
90
            $taxRate = 0;
91
            $priceInc = 0;
92
            $taxValue = 0;
93
94
            if (isset($this->taxRate->rate)) {
95
                $taxRate = $this->taxRate->rate;
96
                $priceInc = (($this->taxRate->rate / 100) * $this->price) + $this->price;
97
                $taxValue = $priceInc - $this->price;
98
            }
99
100
            $discountPriceInc = false;
101
            $discountPriceEx = false;
102
            $discountTaxRate = 0;
103
            if ($this->discount_value) {
104
                if ($this->discount_type == 'amount') {
105
                    $discountPriceInc = $priceInc - $this->discount_value;
106
                     $discountPriceEx = $discountPriceInc / 1.21;
107
                } elseif ($this->discount_type == 'percent') {
108
                    $tax = ($this->discount_value / 100) * $priceInc;
109
                    $discountPriceInc = $priceInc - $tax;
110
                    $discountPriceEx = $discountPriceInc / 1.21;
111
                }
112
                $discountTaxRate = $discountPriceInc - $discountPriceEx;
113
                $discountPriceInc = $discountPriceInc;
114
                $discountPriceEx = $discountPriceEx;
115
            }
116
117
            $commercialPrice = null;
118
            if ($this->commercial_price) {
119
                $commercialPrice = number_format($this->commercial_price, 2, '.', '');
120
            }
121
122
            return array(
123
                'original_price_ex_tax'  => $this->price,
124
                'original_price_ex_tax_number_format'  => number_format($this->price, 2, '.', ''),
125
                'original_price_inc_tax' => $priceInc,
126
                'original_price_inc_tax_number_format' => number_format($priceInc, 2, '.', ''),
127
                'commercial_price_number_format' => $commercialPrice,
128
                'tax_rate' => $taxRate,
129
                'tax_value' => $taxValue,
130
                'currency' => 'EU',
131
                'discount_price_inc' => $discountPriceInc,
132
                'discount_price_inc_number_format' => number_format($discountPriceInc, 2, '.', ''),
0 ignored issues
show
Bug introduced by
It seems like $discountPriceInc can also be of type false; however, parameter $number of number_format() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
                'discount_price_inc_number_format' => number_format(/** @scrutinizer ignore-type */ $discountPriceInc, 2, '.', ''),
Loading history...
133
                'discount_price_ex' => $discountPriceEx,
134
                'discount_price_ex_number_format' => number_format($discountPriceEx, 2, '.', ''),
135
                'discount_tax_value' => $discountTaxRate,
136
                'discount_value' => $this->discount_value,
137
                'amount' => $this->amount
138
            );
139
        }
140
        
141
        return null;    
142
    }
143
144
    public function shop()
145
    {
146
        return $this->belongsTo('Hideyo\Ecommerce\Framework\Services\Shop\Entity\Shop');
147
    }
148
149
    public function attributeGroup()
150
    {
151
        return $this->belongsTo('Hideyo\Ecommerce\Framework\Services\Attribute\Entity\AttributeGroup', 'leading_atrribute_group_id');
152
    }
153
    
154
    public function extraFields()
155
    {
156
        return $this->hasMany('Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductExtraFieldValue');
157
    }
158
159
    public function taxRate()
160
    {
161
        return $this->belongsTo('Hideyo\Ecommerce\Framework\Services\TaxRate\Entity\TaxRate');
162
    }
163
164
    public function brand()
165
    {
166
        return $this->belongsTo('Hideyo\Ecommerce\Framework\Services\Brand\Entity\Brand');
167
    }
168
169
    public function productCategory()
170
    {
171
        return $this->belongsTo('Hideyo\Ecommerce\Framework\Services\ProductCategory\Entity\ProductCategory');
172
    }
173
174
    public function subcategories()
175
    {
176
        return $this->belongsToMany('Hideyo\Ecommerce\Framework\Services\ProductCategory\Entity\ProductCategory', 'product_sub_product_category');
177
    }
178
179
    public function relatedProducts()
180
    {
181
        return $this->belongsToMany('Hideyo\Ecommerce\Framework\Services\Product\Entity\Product', 'product_related_product', 'product_id', 'related_product_id');
182
    }
183
184
    public function relatedProductsActive()
185
    {
186
        return $this->belongsToMany('Hideyo\Ecommerce\Framework\Services\Product\Entity\Product', 'product_related_product', 'product_id', 'related_product_id')->whereHas('productCategory', function ($query) {
187
            $query->where('active', '=', '1');
188
        })->where('product.active', '=', '1');
189
    }
190
191
    public function productImages()
192
    {
193
        return $this->hasMany('Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductImage');
194
    }
195
196
    public function attributes()
197
    {
198
        return $this->hasMany('Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductAttribute');
199
    }
200
201
    public function amountOptions()
202
    {
203
        return $this->hasMany('Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductAmountOption');
204
    }
205
206
    public function amountSeries()
207
    {
208
        return $this->hasMany('Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductAmountSeries');
209
    }
210
}
211