Completed
Push — master ( 8c93ff...a1e415 )
by Scott
02:22
created

Promotion::getAmountExactAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php namespace Bedard\Shop\Models;
2
3
use Model;
4
5
/**
6
 * Promotion Model.
7
 */
8
class Promotion extends Model
9
{
10
    use \Bedard\Shop\Traits\StartEndable,
11
        \Bedard\Shop\Traits\Timeable,
12
        \October\Rain\Database\Traits\Purgeable,
13
        \October\Rain\Database\Traits\Validation;
14
15
    /**
16
     * @var string The database table used by the model.
17
     */
18
    public $table = 'bedard_shop_promotions';
19
20
    /**
21
     * @var array Default attributes
22
     */
23
    public $attributes = [
24
        'amount' => 0,
25
        'amount_exact' => 0,
26
        'amount_percentage' => 0,
27
        'is_percentage' => true,
28
    ];
29
30
    /**
31
     * @var array Guarded fields
32
     */
33
    protected $guarded = ['*'];
34
35
    /**
36
     * @var array Fillable fields
37
     */
38
    protected $fillable = [
39
        'amount_exact',
40
        'amount_percentage',
41
        'amount',
42
        'is_percentage',
43
        'message',
44
        'minimum_cart_value',
45
        'name',
46
    ];
47
48
    /**
49
     * @var array Purgeable vields
50
     */
51
    protected $purgeable = [
52
        'amount_exact',
53
        'amount_percentage',
54
    ];
55
56
    /**
57
     * @var  array Validation rules
58
     */
59
    public $rules = [
60
        'amount_exact' => 'numeric|min:0',
61
        'amount_percentage' => 'numeric|min:0|max:100',
62
        'end_at' => 'date',
63
        'minimum_cart_value' => 'numeric|min:0',
64
        'name' => 'required',
65
        'start_at' => 'date',
66
    ];
67
68
    /**
69
     * Before save.
70
     *
71
     * @return void
72
     */
73
    public function beforeSave()
74
    {
75
        $this->setAmount();
76
    }
77
78
    /**
79
     * Filter form fields.
80
     *
81
     * @param  object   $fields
82
     * @return void
83
     */
84
    public function filterFields($fields)
85
    {
86
        $fields->amount_exact->hidden = $this->is_percentage;
87
        $fields->amount_percentage->hidden = ! $this->is_percentage;
88
    }
89
90
    /**
91
     * Get the exact amount.
92
     *
93
     * @return float
94
     */
95
    public function getAmountExactAttribute()
96
    {
97
        return ! $this->is_percentage ? $this->amount : 0;
98
    }
99
100
    /**
101
     * Get the percentage amount.
102
     *
103
     * @return float
104
     */
105
    public function getAmountPercentageAttribute()
106
    {
107
        return $this->is_percentage ? $this->amount : 0;
108
    }
109
110
    /**
111
     * Set the promotion amount.
112
     *
113
     * @return  void
114
     */
115 View Code Duplication
    public function setAmount()
116
    {
117
        $exact = $this->getOriginalPurgeValue('amount_exact');
118
        $percentage = $this->getOriginalPurgeValue('amount_percentage');
119
120
        $this->amount = $this->is_percentage
121
            ? $percentage
122
            : $exact;
123
    }
124
}
125