Completed
Push — master ( a1e415...8aec58 )
by Scott
02:48
created

Promotion::setAmount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
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\Amountable,
11
        \Bedard\Shop\Traits\StartEndable,
12
        \Bedard\Shop\Traits\Timeable,
13
        \October\Rain\Database\Traits\Purgeable,
14
        \October\Rain\Database\Traits\Validation;
15
16
    /**
17
     * @var string The database table used by the model.
18
     */
19
    public $table = 'bedard_shop_promotions';
20
21
    /**
22
     * @var array Default attributes
23
     */
24
    public $attributes = [
25
        'amount' => 0,
26
        'is_percentage' => true,
27
        'minimum_cart_value' => 0,
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',
40
        'is_percentage',
41
        'message',
42
        'minimum_cart_value',
43
        'name',
44
    ];
45
46
    /**
47
     * @var array Purgeable vields
48
     */
49
    protected $purgeable = [];
50
51
    /**
52
     * @var  array Validation rules
53
     */
54
    public $rules = [
55
        'end_at' => 'date',
56
        'minimum_cart_value' => 'numeric|min:0',
57
        'name' => 'required',
58
        'start_at' => 'date',
59
    ];
60
61
    /**
62
     * Before save.
63
     *
64
     * @return void
65
     */
66
    public function beforeSave()
67
    {
68
        $this->setAmount();
69
    }
70
71
    /**
72
     * Filter form fields.
73
     *
74
     * @param  object   $fields
75
     * @return void
76
     */
77
    public function filterFields($fields)
78
    {
79
        $fields->amount_exact->hidden = $this->is_percentage;
80
        $fields->amount_percentage->hidden = ! $this->is_percentage;
81
    }
82
83
}
84